forked from malcolmgroves/FluentQuery
-
Notifications
You must be signed in to change notification settings - Fork 4
/
FluentQuery.Core.Enumerators.pas
executable file
·435 lines (359 loc) · 11.1 KB
/
FluentQuery.Core.Enumerators.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
{****************************************************}
{ }
{ FluentQuery }
{ }
{ Copyright (C) 2013 Malcolm Groves }
{ }
{ http://www.malcolmgroves.com }
{ }
{****************************************************}
{ }
{ This Source Code Form is subject to the terms of }
{ the Mozilla Public License, v. 2.0. If a copy of }
{ the MPL was not distributed with this file, You }
{ can obtain one at }
{ }
{ http://mozilla.org/MPL/2.0/ }
{ }
{****************************************************}
unit FluentQuery.Core.Enumerators;
interface
uses
System.Generics.Collections,
System.Classes,
FluentQuery.Core.Types,
FluentQuery.Core.EnumerationStrategies;
type
TBaseQuery<T> = class(TInterfacedObject, IBaseQuery<T>)
protected
FUpstreamQuery : IBaseQuery<T>;
FEnumerationStrategy : TEnumerationStrategy<T>;
FSourceData : IMinimalEnumerator<T>;
FValues : TStrings;
{$IFDEF DEBUG}
FOperationName : String;
{$ENDIF}
procedure SetSourceData(SourceData : IMinimalEnumerator<T>); virtual;
procedure Execute;
function GetCurrent: T; virtual;
function MoveNext: Boolean; virtual;
procedure SetUpstreamQuery(UpstreamQuery : IBaseQuery<T>);
{$IFDEF DEBUG}
function GetOperationName : String; virtual;
procedure SetOperationName(const Name : String); virtual;
function GetOperationPath : String; virtual;
property OperationName : string read GetOperationName write SetOperationName;
property OperationPath : string read GetOperationPath;
{$ENDIF}
public
constructor Create(EnumerationStrategy : TEnumerationStrategy<T>;
UpstreamQuery : IBaseQuery<T> = nil;
SourceData : IMinimalEnumerator<T> = nil); virtual;
destructor Destroy; override;
end;
TStringsEnumeratorAdapter = class(TInterfacedObject, IMinimalEnumerator<String>)
protected
FStringsEnumerator : TStringsEnumerator;
function GetCurrent: String;
function MoveNext: Boolean;
property Current: String read GetCurrent;
public
constructor Create(StringsEnumerator : TStringsEnumerator); virtual;
destructor Destroy; override;
end;
TGenericEnumeratorAdapter<T> = class(TInterfacedObject, IMinimalEnumerator<T>)
protected
FEnumerator : TEnumerator<T>;
function GetCurrent: T;
function MoveNext: Boolean;
property Current: T read GetCurrent;
public
constructor Create(Enumerator : TEnumerator<T>); virtual;
destructor Destroy; override;
end;
TSuperTypeEnumeratorAdapter<TSuperType : class; TSubType : class> = class(TInterfacedObject, IMinimalEnumerator<TSubType>)
protected
FEnumerator : IMinimalEnumerator<TSuperType>;
function GetCurrent: TSubType;
function MoveNext: Boolean;
property Current: TSubType read GetCurrent;
public
constructor Create(Enumerator : IMinimalEnumerator<TSuperType>); virtual;
end;
TStringEnumeratorAdapter = class(TInterfacedObject, IMinimalEnumerator<Char>)
protected
FStringValue : String;
FIndex : Integer;
function GetCurrent: Char;
function MoveNext: Boolean;
property Current: Char read GetCurrent;
public
constructor Create(StringValue : String); virtual;
end;
TSingleValueAdapter<T> = class(TInterfacedObject, IMinimalEnumerator<T>)
private
FMoveCount: Integer;
FValue : T;
function GetCurrent: T;
function MoveNext: Boolean;
public
constructor Create(Value : T);
end;
TListEnumeratorAdapter = class(TinterfacedObject, IMinimalEnumerator<Pointer>)
FListEnumerator : TListEnumerator;
function GetCurrent: Pointer;
function MoveNext: Boolean;
property Current: Pointer read GetCurrent;
public
constructor Create(ListEnumerator : TListEnumerator); virtual;
destructor Destroy; override;
end;
TComponentEnumeratorAdapter = class(TinterfacedObject, IMinimalEnumerator<TComponent>)
FEnumerator : TComponentEnumerator;
function GetCurrent: TComponent;
function MoveNext: Boolean;
property Current: TComponent read GetCurrent;
public
constructor Create(Enumerator : TComponentEnumerator); virtual;
destructor Destroy; override;
end;
TIntegerRangeEnumerator = class(TInterfacedObject, IMinimalEnumerator<Integer>)
private
FFinish : Integer;
FCurrent : Integer;
protected
function MoveNext: Boolean;
function GetCurrent: Integer;
public
constructor Create(Start : Integer = 0; Finish : Integer = MaxInt);
end;
TIntegerRangeReverseEnumerator = class(TInterfacedObject, IMinimalEnumerator<Integer>)
private
FFinish : Integer;
FCurrent : Integer;
protected
function MoveNext: Boolean;
function GetCurrent: Integer;
public
constructor Create(Start : Integer = 0; Finish : Integer = MaxInt);
end;
implementation
{ TStringsEnumeratorWrapper }
constructor TStringsEnumeratorAdapter.Create(StringsEnumerator : TStringsEnumerator);
begin
FStringsEnumerator := StringsEnumerator;
end;
destructor TStringsEnumeratorAdapter.Destroy;
begin
FStringsEnumerator.Free;
inherited;
end;
function TStringsEnumeratorAdapter.GetCurrent: String;
begin
Result := FStringsEnumerator.Current;
end;
function TStringsEnumeratorAdapter.MoveNext: Boolean;
begin
Result := FStringsEnumerator.MoveNext;
end;
{ TGenericEnumeratorWrapper<T> }
constructor TGenericEnumeratorAdapter<T>.Create(Enumerator: TEnumerator<T>);
begin
FEnumerator := Enumerator;
end;
destructor TGenericEnumeratorAdapter<T>.Destroy;
begin
FEnumerator.Free;
inherited;
end;
function TGenericEnumeratorAdapter<T>.GetCurrent: T;
begin
Result := FEnumerator.Current;
end;
function TGenericEnumeratorAdapter<T>.MoveNext: Boolean;
begin
Result := FEnumerator.MoveNext;
end;
{ TStringEnumeratorWrapper }
constructor TStringEnumeratorAdapter.Create(StringValue: String);
begin
FStringValue := StringValue;
FIndex := Low(StringValue) - 1;
end;
function TStringEnumeratorAdapter.GetCurrent: Char;
begin
Result := FStringValue[FIndex];
end;
function TStringEnumeratorAdapter.MoveNext: Boolean;
begin
Inc(FIndex);
Result := FIndex <= High(FStringValue);
end;
{ TSingleValueAdapter<T> }
constructor TSingleValueAdapter<T>.Create(Value: T);
begin
FValue := Value;
end;
function TSingleValueAdapter<T>.GetCurrent: T;
begin
Result := FValue;
end;
function TSingleValueAdapter<T>.MoveNext: Boolean;
begin
Inc(FMoveCount);
Result := FMoveCount = 1;
end;
{ TListEnumeratorAdapter }
constructor TListEnumeratorAdapter.Create(ListEnumerator: TListEnumerator);
begin
FListEnumerator := ListEnumerator;
end;
destructor TListEnumeratorAdapter.Destroy;
begin
FListEnumerator.Free;
inherited;
end;
function TListEnumeratorAdapter.GetCurrent: Pointer;
begin
Result := FListEnumerator.Current;
end;
function TListEnumeratorAdapter.MoveNext: Boolean;
begin
Result := FListEnumerator.MoveNext;
end;
{ TBaseQueryEnumerator<T> }
constructor TBaseQuery<T>.Create(
EnumerationStrategy: TEnumerationStrategy<T>;
UpstreamQuery: IBaseQuery<T>;
SourceData: IMinimalEnumerator<T>);
begin
SetUpstreamQuery(UpstreamQuery);
SetSourceData(SourceData);
FEnumerationStrategy := EnumerationStrategy;
end;
destructor TBaseQuery<T>.Destroy;
begin
FEnumerationStrategy.Free;
FValues.Free;
inherited;
end;
procedure TBaseQuery<T>.Execute;
begin
while MoveNext do
GetCurrent;
end;
function TBaseQuery<T>.GetCurrent: T;
begin
if Assigned(FUpstreamQuery) then
Result := FEnumerationStrategy.GetCurrent(FUpstreamQuery)
else
Result := FEnumerationStrategy.GetCurrent(FSourceData);;
end;
{$IFDEF DEBUG}
function TBaseQuery<T>.GetOperationName: String;
begin
Result := FOperationName;
end;
procedure TBaseQuery<T>.SetOperationName(const Name: String);
begin
FOperationName := Name;
end;
function TBaseQuery<T>.GetOperationPath: String;
begin
if Assigned(FUpstreamQuery) then
Result := FUpstreamQuery.GetOperationPath + '.' + GetOperationName
else
Result := GetOperationName;
end;
{$ENDIF}
function TBaseQuery<T>.MoveNext: Boolean;
begin
if Assigned(FUpstreamQuery) then
Result := FEnumerationStrategy.MoveNext(FUpstreamQuery)
else
Result := FEnumerationStrategy.MoveNext(FSourceData);
end;
procedure TBaseQuery<T>.SetSourceData(
SourceData: IMinimalEnumerator<T>);
begin
if Assigned(SourceData) then
begin
if Assigned(FUpstreamQuery) then
FUpstreamQuery.SetSourceData(SourceData)
else
FSourceData := SourceData;
end;
end;
procedure TBaseQuery<T>.SetUpstreamQuery(
UpstreamQuery: IBaseQuery<T>);
begin
FUpstreamQuery := UpstreamQuery;
end;
{ TIntegerRangeEnumerator }
constructor TIntegerRangeEnumerator.Create(Start, Finish: Integer);
begin
FFinish := Finish;
FCurrent := Start - 1;
end;
function TIntegerRangeEnumerator.GetCurrent: Integer;
begin
Result := FCurrent;
end;
function TIntegerRangeEnumerator.MoveNext: Boolean;
begin
if FCurrent = MaxInt then
Exit(False); //avoid wraparound
Inc(FCurrent);
Result := FCurrent <= FFinish;
end;
{ TIntegerRangeReverseEnumerator }
constructor TIntegerRangeReverseEnumerator.Create(Start, Finish: Integer);
begin
FFinish := Finish;
FCurrent := Start + 1;
end;
function TIntegerRangeReverseEnumerator.GetCurrent: Integer;
begin
Result := FCurrent;
end;
function TIntegerRangeReverseEnumerator.MoveNext: Boolean;
begin
if FCurrent = -MaxInt then
Exit(False); //avoid wraparound
Dec(FCurrent);
Result := FCurrent >= FFinish;
end;
{ TSuperTypeEnumeratorAdapter<TSuperType, TSubType> }
constructor TSuperTypeEnumeratorAdapter<TSuperType, TSubType>.Create(
Enumerator: IMinimalEnumerator<TSuperType>);
begin
FEnumerator := Enumerator;
end;
function TSuperTypeEnumeratorAdapter<TSuperType, TSubType>.GetCurrent: TSubType;
begin
Result := FEnumerator.Current as TSubType;
end;
function TSuperTypeEnumeratorAdapter<TSuperType, TSubType>.MoveNext: Boolean;
begin
Result := FEnumerator.MoveNext;
end;
{ TComponentEnumeratorAdapter }
constructor TComponentEnumeratorAdapter.Create(
Enumerator: TComponentEnumerator);
begin
FEnumerator := Enumerator;
end;
destructor TComponentEnumeratorAdapter.Destroy;
begin
FEnumerator.Free;
inherited;
end;
function TComponentEnumeratorAdapter.GetCurrent: TComponent;
begin
Result := FEnumerator.Current;
end;
function TComponentEnumeratorAdapter.MoveNext: Boolean;
begin
Result := FEnumerator.MoveNext;
end;
end.