forked from malcolmgroves/FluentQuery
-
Notifications
You must be signed in to change notification settings - Fork 4
/
FluentQuery.Components.pas
402 lines (359 loc) · 16.1 KB
/
FluentQuery.Components.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
unit FluentQuery.Components;
interface
uses
FluentQuery.Core.Types,
System.SysUtils,
FluentQuery.Core.EnumerationStrategies,
FluentQuery.Core.Enumerators,
System.Classes,
System.TypInfo,
FluentQuery.Integers,
FluentQuery.Strings;
type
IUnboundComponentQuery<T : TComponent> = interface;
IBoundComponentQuery<T : TComponent> = interface(IBaseQuery<T>)
function GetEnumerator: IBoundComponentQuery<T>;
// query operations
function IsA(AClass : TClass) : IBoundComponentQuery<T>;
function Map(Transformer : TProc<T>) : IBoundComponentQuery<T>;
function SkipWhile(Predicate : TPredicate<T>) : IBoundComponentQuery<T>; overload;
function TakeWhile(Predicate : TPredicate<T>): IBoundComponentQuery<T>; overload;
function Where(Predicate : TPredicate<T>) : IBoundComponentQuery<T>;
function HasProperty(const Name : string; PropertyType : TTypeKind) : IBoundComponentQuery<T>;
function IntegerProperty(const Name : string; const Value : Integer) : IBoundComponentQuery<T>; overload;
function IntegerProperty(const Name : string; Query : IUnboundIntegerQuery) : IBoundComponentQuery<T>; overload;
function StringProperty(const Name : string; const Value : String; IgnoreCase : Boolean = True) : IBoundComponentQuery<T>; overload;
function StringProperty(const Name : string; Query : IUnboundStringQuery) : IBoundComponentQuery<T>; overload;
function BooleanProperty(const Name : string; const Value : Boolean) : IBoundComponentQuery<T>;
function Skip(Count : Integer): IBoundComponentQuery<T>;
function SkipWhile(UnboundQuery : IUnboundComponentQuery<T>) : IBoundComponentQuery<T>; overload;
function TagEquals(const TagValue : NativeInt) : IBoundComponentQuery<T>;
function Take(Count : Integer): IBoundComponentQuery<T>;
function TakeWhile(UnboundQuery : IUnboundComponentQuery<T>): IBoundComponentQuery<T>; overload;
function WhereNot(UnboundQuery : IUnboundComponentQuery<T>) : IBoundComponentQuery<T>; overload;
function WhereNot(Predicate : TPredicate<T>) : IBoundComponentQuery<T>; overload;
// terminating operations
function First : T;
function Count : Integer;
end;
IUnboundComponentQuery<T : TComponent> = interface(IBaseQuery<T>)
function GetEnumerator: IUnboundComponentQuery<T>;
function From(Owner : TComponent) : IBoundComponentQuery<T>;
// query operations
function IsA(AClass : TClass) : IUnboundComponentQuery<T>;
function Map(Transformer : TProc<T>) : IUnboundComponentQuery<T>;
function SkipWhile(Predicate : TPredicate<T>) : IUnboundComponentQuery<T>; overload;
function TakeWhile(Predicate : TPredicate<T>): IUnboundComponentQuery<T>; overload;
function Where(Predicate : TPredicate<T>) : IUnboundComponentQuery<T>;
function HasProperty(const Name : string; PropertyType : TTypeKind) : IUnboundComponentQuery<T>;
function IntegerProperty(const Name : string; const Value : Integer) : IUnboundComponentQuery<T>; overload;
function IntegerProperty(const Name : string; Query : IUnboundIntegerQuery) : IUnboundComponentQuery<T>; overload;
function StringProperty(const Name : string; const Value : String; IgnoreCase : Boolean = True) : IUnboundComponentQuery<T>; overload;
function StringProperty(const Name : string; Query : IUnboundStringQuery) : IUnboundComponentQuery<T>; overload;
function BooleanProperty(const Name : string; const Value : Boolean) : IUnboundComponentQuery<T>;
function Skip(Count : Integer): IUnboundComponentQuery<T>;
function SkipWhile(UnboundQuery : IUnboundComponentQuery<T>) : IUnboundComponentQuery<T>; overload;
function TagEquals(const TagValue : NativeInt) : IUnboundComponentQuery<T>;
function Take(Count : Integer): IUnboundComponentQuery<T>;
function TakeWhile(UnboundQuery : IUnboundComponentQuery<T>): IUnboundComponentQuery<T>; overload;
function WhereNot(UnboundQuery : IUnboundComponentQuery<T>) : IUnboundComponentQuery<T>; overload;
function WhereNot(Predicate : TPredicate<T>) : IUnboundComponentQuery<T>; overload;
// terminating operations
function Predicate : TPredicate<T>;
end;
TComponentQuery<T : TComponent> = class(TBaseQuery<T>,
IBoundComponentQuery<T>,
IUnboundComponentQuery<T>)
protected
type
TComponentQueryImpl<TReturnType : IBaseQuery<T>> = class
private
FQuery : TComponentQuery<T>;
public
constructor Create(Query : TComponentQuery<T>); virtual;
function GetEnumerator: TReturnType;
{$IFDEF DEBUG}
function GetOperationName : String;
function GetOperationPath : String;
property OperationName : string read GetOperationName;
property OperationPath : string read GetOperationPath;
{$ENDIF}
function From(Owner : TComponent) : IBoundComponentQuery<T>;
// Primitive Operations
function Map(Transformer : TProc<T>) : TReturnType;
function SkipWhile(Predicate : TPredicate<T>) : TReturnType; overload;
function TakeWhile(Predicate : TPredicate<T>): TReturnType; overload;
function Where(Predicate : TPredicate<T>) : TReturnType;
// Derivative Operations
function HasProperty(const Name : string; PropertyType : TTypeKind) : TReturnType;
function IntegerProperty(const Name : string; const Value : Integer) : TReturnType; overload;
function IntegerProperty(const Name : string; Query : IUnboundIntegerQuery) : TReturnType; overload;
function StringProperty(const Name : string; const Value : String; IgnoreCase : Boolean = True) : TReturnType; overload;
function StringProperty(const Name : string; Query : IUnboundStringQuery) : TReturnType; overload;
function BooleanProperty(const Name : string; const Value : Boolean) : TReturnType;
function IsA(AClass : TClass) : TReturnType;
function Skip(Count : Integer): TReturnType;
function SkipWhile(UnboundQuery : IUnboundComponentQuery<T>) : TReturnType; overload;
function TagEquals(const TagValue : NativeInt) : TReturnType;
function Take(Count : Integer): TReturnType;
function TakeWhile(UnboundQuery : IUnboundComponentQuery<T>): TReturnType; overload;
function WhereNot(UnboundQuery : IUnboundComponentQuery<T>) : TReturnType; overload;
function WhereNot(Predicate : TPredicate<T>) : TReturnType; overload;
// Terminating Operations
function Predicate : TPredicate<T>;
function First : T;
function Count : Integer;
end;
protected
FBoundQuery : TComponentQueryImpl<IBoundComponentQuery<T>>;
FUnboundQuery : TComponentQueryImpl<IUnboundComponentQuery<T>>;
public
constructor Create(EnumerationStrategy : TEnumerationStrategy<T>;
UpstreamQuery : IBaseQuery<T> = nil;
SourceData : IMinimalEnumerator<T> = nil); override;
destructor Destroy; override;
property BoundQuery : TComponentQueryImpl<IBoundComponentQuery<T>>
read FBoundQuery implements IBoundComponentQuery<T>;
property UnboundQuery : TComponentQueryImpl<IUnboundComponentQuery<T>>
read FUnboundQuery implements IUnboundComponentQuery<T>;
end;
ComponentQuery<T : TComponent> = class
public
class function Select : IUnboundComponentQuery<T>;
end;
implementation
uses
FluentQuery.Components.MethodFactories, FluentQuery.GenericObjects, FluentQuery.Integers.MethodFactories,
FluentQuery.Strings.MethodFactories, FluentQuery.Core.Reduce;
{ TComponentQueryEnumerator<T> }
constructor TComponentQuery<T>.Create(
EnumerationStrategy: TEnumerationStrategy<T>;
UpstreamQuery: IBaseQuery<T>; SourceData: IMinimalEnumerator<T>);
begin
inherited Create(EnumerationStrategy, UpstreamQuery, SourceData);
FBoundQuery := TComponentQueryImpl<IBoundComponentQuery<T>>.Create(self);
FUnboundQuery := TComponentQueryImpl<IUnboundComponentQuery<T>>.Create(self);
end;
destructor TComponentQuery<T>.Destroy;
begin
FBoundQuery.Free;
FUnboundQuery.Free;
inherited;
end;
{ TComponentQueryEnumerator<T>.TComponentQueryEnumeratorImpl<TReturnType> }
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.BooleanProperty(
const Name: string; const Value: Boolean): TReturnType;
begin
Result := Where(TComponentMethodFactory<T>.BooleanPropertyNamedWithValue(Name, Value));
{$IFDEF DEBUG}
Result.OperationName := 'BooleanProperty(Name, Value)';
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.Count: Integer;
begin
Result := TReducer<T,Integer>.Reduce(FQuery,
0,
function(Accumulator : Integer; NextValue : T): Integer
begin
Result := Accumulator + 1;
end);
end;
constructor TComponentQuery<T>.TComponentQueryImpl<TReturnType>.Create(
Query: TComponentQuery<T>);
begin
FQuery := Query;
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.First: T;
begin
if FQuery.MoveNext then
Result := FQuery.GetCurrent
else
raise EEmptyResultSetException.Create('Can''t call First on an empty Result Set');
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.From(
Owner: TComponent): IBoundComponentQuery<T>;
var
LEnumeratorWrapper : IMinimalEnumerator<TComponent>;
LSuperTypeAdapter : TSuperTypeEnumeratorAdapter<TComponent, T>;
begin
LEnumeratorWrapper := TComponentEnumeratorAdapter.Create(Owner.GetEnumerator) as IMinimalEnumerator<TComponent>;
LSuperTypeAdapter := TSuperTypeEnumeratorAdapter<TComponent, T>.Create(
ObjectQuery<TComponent>.Select.From(LEnumeratorWrapper).IsA(T));
Result := TComponentQuery<T>.Create(TEnumerationStrategy<T>.Create,
IBaseQuery<T>(FQuery),
LSuperTypeAdapter);
{$IFDEF DEBUG}
Result.OperationName := Format('From(%s)', [Owner.Name]);
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.GetEnumerator: TReturnType;
begin
Result := FQuery;
end;
{$IFDEF DEBUG}
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.GetOperationName: String;
begin
Result := FQuery.OperationName;
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.GetOperationPath: String;
begin
Result := FQuery.OperationPath;
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.HasProperty(
const Name: string; PropertyType: TTypeKind): TReturnType;
begin
Result := Where(TComponentMethodFactory<T>.PropertyNamedOfType(Name, PropertyType));
{$IFDEF DEBUG}
Result.OperationName := 'HasProperty(Name, PropertyType)';
{$ENDIF}
end;
{$ENDIF}
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.IntegerProperty(
const Name: string; const Value: Integer): TReturnType;
begin
Result := Where(TComponentMethodFactory<T>.IntegerPropertyNamedWithValue(Name, TIntegerMethodFactory.Equals(Value)));
{$IFDEF DEBUG}
Result.OperationName := 'IntegerProperty(Name, Value)';
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.IntegerProperty(
const Name: string; Query: IUnboundIntegerQuery): TReturnType;
begin
Result := Where(TComponentMethodFactory<T>.IntegerPropertyNamedWithValue(Name, Query.Predicate));
{$IFDEF DEBUG}
Result.OperationName := 'IntegerProperty(Name, ' + Query.OperationPath + ')';
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.IsA(
AClass: TClass): TReturnType;
begin
Result := Where(TComponentMethodFactory<T>.IsA(AClass));
{$IFDEF DEBUG}
Result.OperationName := 'IsA';
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.Map(
Transformer: TProc<T>): TReturnType;
begin
Result := TComponentQuery<T>.Create(
TIsomorphicTransformEnumerationStrategy<T>.Create(
TComponentMethodFactory<T>.InPlaceTransformer(Transformer)),
IBaseQuery<T>(FQuery));
{$IFDEF DEBUG}
Result.OperationName := 'Map(Transformer)';
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.Predicate: TPredicate<T>;
begin
Result := TComponentMethodFactory<T>.QuerySingleValue(FQuery);
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.SkipWhile(
UnboundQuery: IUnboundComponentQuery<T>): TReturnType;
begin
Result := SkipWhile(UnboundQuery.Predicate);
{$IFDEF DEBUG}
Result.OperationName := Format('SkipWhile(%s)', [UnboundQuery.OperationPath]);
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.StringProperty(
const Name: string; Query: IUnboundStringQuery): TReturnType;
begin
Result := Where(TComponentMethodFactory<T>.StringPropertyNamedWithValue(Name,
Query.Predicate));
{$IFDEF DEBUG}
Result.OperationName := 'StringProperty(Name, Value)';
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.StringProperty(
const Name, Value: String; IgnoreCase : Boolean): TReturnType;
begin
Result := Where(TComponentMethodFactory<T>.StringPropertyNamedWithValue(Name,
TStringMethodFactory.Matches(Value, IgnoreCase)));
{$IFDEF DEBUG}
Result.OperationName := 'StringProperty(Name, Value)';
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.SkipWhile(
Predicate: TPredicate<T>): TReturnType;
begin
Result := TComponentQuery<T>.Create(TSkipWhileEnumerationStrategy<T>.Create(Predicate),
IBaseQuery<T>(FQuery));
{$IFDEF DEBUG}
Result.OperationName := 'SkipWhile(Predicate)';
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.TakeWhile(
Predicate: TPredicate<T>): TReturnType;
begin
Result := TComponentQuery<T>.Create(TTakeWhileEnumerationStrategy<T>.Create(Predicate),
IBaseQuery<T>(FQuery));
{$IFDEF DEBUG}
Result.OperationName := 'TakeWhile(Predicate)';
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.Where(
Predicate: TPredicate<T>): TReturnType;
begin
Result := TComponentQuery<T>.Create(TWhereEnumerationStrategy<T>.Create(Predicate),
IBaseQuery<T>(FQuery));
{$IFDEF DEBUG}
Result.OperationName := 'Where(Predicate)';
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.Skip(
Count: Integer): TReturnType;
begin
Result := SkipWhile(TComponentMethodFactory<T>.UpToNumberOfTimes(Count));
{$IFDEF DEBUG}
Result.OperationName := Format('Skip(%d)', [Count]);
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.TagEquals(
const TagValue: NativeInt): TReturnType;
begin
Result := Where(TComponentMethodFactory<T>.TagEquals(TagValue));
{$IFDEF DEBUG}
Result.OperationName := Format('Skip(%d)', [TagValue]);
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.Take(
Count: Integer): TReturnType;
begin
Result := TakeWhile(TComponentMethodFactory<T>.UpToNumberOfTimes(Count));
{$IFDEF DEBUG}
Result.OperationName := Format('Take(%d)', [Count]);
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.TakeWhile(
UnboundQuery: IUnboundComponentQuery<T>): TReturnType;
begin
Result := TakeWhile(UnboundQuery.Predicate);
{$IFDEF DEBUG}
Result.OperationName := Format('TakeWhile', [UnboundQuery.OperationPath]);
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.WhereNot(
UnboundQuery: IUnboundComponentQuery<T>): TReturnType;
begin
Result := WhereNot(UnboundQuery.Predicate);
{$IFDEF DEBUG}
Result.OperationName := Format('WhereNot(%s)', [UnboundQuery.OperationPath]);
{$ENDIF}
end;
function TComponentQuery<T>.TComponentQueryImpl<TReturnType>.WhereNot(
Predicate: TPredicate<T>): TReturnType;
begin
Result := Where(TComponentMethodFactory<T>.InvertPredicate(Predicate));
{$IFDEF DEBUG}
Result.OperationName := 'WhereNot(Predicate)';
{$ENDIF}
end;
{ ComponentQuery<T> }
class function ComponentQuery<T>.Select: IUnboundComponentQuery<T>;
begin
Result := TComponentQuery<T>.Create(TEnumerationStrategy<T>.Create);
{$IFDEF DEBUG}
Result.OperationName := 'ComponentQuery.Select<T>';
{$ENDIF}
end;
end.