-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathService.Patterns.Nullable.pas
200 lines (158 loc) · 4.76 KB
/
Service.Patterns.Nullable.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
unit Service.Patterns.Nullable;
interface
uses
System.Generics.Defaults,
System.Rtti, System.SysUtils;
type
ENullValueException = class(Exception)
public
constructor Create;
end;
ENullConvertException<T> = class(Exception)
public
constructor Create;
end;
TNullRecord = record
end;
TNullable<T> = record
private
FValue: T;
FHasValue: Boolean;
class procedure CheckNullOperation(ALeft, ARight: TNullable<T>); static;
function GetIsNull: Boolean;
function GetValue: T;
procedure SetValue(const AValue: T);
function GetValueOrDefault: T;
public
constructor Create(AValue: T); overload;
property HasValue: Boolean read FHasValue;
property IsNull: Boolean read GetIsNull;
property Value: T read GetValue write SetValue;
property ValueOrDefault: T read GetValueOrDefault;
class operator Implicit(AValue: TNullRecord): TNullable<T>;
class operator Implicit(AValue: T): TNullable<T>;
class operator Implicit(AValue: TNullable<T>): T;
class operator Explicit(AValue: T): TNullable<T>;
class operator Explicit(AValue: TNullable<T>): T;
class operator Equal(ALeft, ARight: TNullable<T>): Boolean;
class operator NotEqual(ALeft, ARight: TNullable<T>): Boolean;
class operator GreaterThan(ALeft, ARight: TNullable<T>): Boolean;
class operator GreaterThanOrEqual(ALeft, ARight: TNullable<T>): Boolean;
class operator LessThan(ALeft, ARight: TNullable<T>): Boolean;
class operator LessThanOrEqual(ALeft, ARight: TNullable<T>): Boolean;
end;
var
SNull: TNullRecord;
implementation
{ TNullable<T> }
class procedure TNullable<T>.CheckNullOperation(ALeft, ARight: TNullable<T>);
begin
if ((not ALeft.FHasValue) or (not ARight.FHasValue)) then
raise ENullValueException.Create;
end;
constructor TNullable<T>.Create(AValue: T);
begin
FHasValue := False;
FValue := Value;
end;
class operator TNullable<T>.Implicit(AValue: T): TNullable<T>;
begin
Result.FValue := AValue;
Result.FHasValue := True;
end;
class operator TNullable<T>.Implicit(AValue: TNullable<T>): T;
begin
Result := AValue.GetValue;
end;
class operator TNullable<T>.Equal(ALeft, ARight: TNullable<T>): Boolean;
begin
if ((ALeft.FHasValue) and (ARight.FHasValue)) then
Result := TEqualityComparer<T>.Default.Equals(ALeft.FValue, ARight.FValue)
else
Result := (ALeft.FHasValue = ARight.FHasValue);
end;
class operator TNullable<T>.Explicit(AValue: T): TNullable<T>;
begin
Result.FValue := AValue;
Result.FHasValue := True;
end;
class operator TNullable<T>.Explicit(AValue: TNullable<T>): T;
begin
if (not AValue.FHasValue) then
raise ENullConvertException<T>.Create;
Result := AValue.FValue;
end;
function TNullable<T>.GetIsNull: Boolean;
begin
Result := not FHasValue;
end;
function TNullable<T>.GetValue: T;
begin
if not FHasValue then
raise ENullConvertException<T>.Create;
Result := FValue;
end;
function TNullable<T>.GetValueOrDefault: T;
begin
if FHasValue then
Result := FValue
else
Result := Default(T);
end;
class operator TNullable<T>.GreaterThan(ALeft, ARight: TNullable<T>): Boolean;
begin
CheckNullOperation(ALeft, ARight);
Result := (TComparer<T>.Default.Compare(ALeft, ARight) > 0);
end;
class operator TNullable<T>.GreaterThanOrEqual(ALeft, ARight: TNullable<T>): Boolean;
begin
CheckNullOperation(ALeft, ARight);
Result := (TComparer<T>.Default.Compare(ALeft, ARight) >= 0);
end;
class operator TNullable<T>.LessThanOrEqual(ALeft, ARight: TNullable<T>): Boolean;
begin
CheckNullOperation(ALeft, ARight);
Result := (TComparer<T>.Default.Compare(ALeft, ARight) <= 0);
end;
class operator TNullable<T>.LessThan(ALeft, ARight: TNullable<T>): Boolean;
begin
CheckNullOperation(ALeft, ARight);
Result := (TComparer<T>.Default.Compare(ALeft, ARight) < 0);
end;
class operator TNullable<T>.NotEqual(ALeft, ARight: TNullable<T>): Boolean;
begin
if ((ALeft.FHasValue) and (ARight.FHasValue)) then
Result := not TEqualityComparer<T>.Default.Equals(ALeft.FValue, ARight.FValue)
else
Result := (ALeft.FHasValue <> ARight.FHasValue);
end;
procedure TNullable<T>.SetValue(const AValue: T);
begin
FValue := AValue;
FHasValue := True;
end;
class operator TNullable<T>.Implicit(AValue: TNullRecord): TNullable<T>;
begin
Result.FHasValue := False;
Result.FValue := Default(T);
end;
{ ENullValueException }
constructor ENullValueException.Create;
begin
inherited Create('Nullable: Cannot operate with SNull value.');
end;
{ ENullConvertException<T> }
constructor ENullConvertException<T>.Create;
var
Context: TRttiContext;
StrType: string;
begin
Context := TRttiContext.Create;
try
StrType := Context.GetType(TypeInfo(T)).ToString;
finally
Context.Free;
end;
inherited Create('Nullable: Cannot convert SNull into ' + StrType + '.');
end;
end.