forked from malcolmgroves/FluentQuery
-
Notifications
You must be signed in to change notification settings - Fork 4
/
FluentQuery.Generics.pas
executable file
·322 lines (282 loc) · 11.3 KB
/
FluentQuery.Generics.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
{****************************************************}
{ }
{ 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.Generics;
interface
uses
FluentQuery.Core.Types,
System.SysUtils,
System.Generics.Collections,
FluentQuery.Core.EnumerationStrategies,
FluentQuery.Core.Enumerators;
type
IUnboundQuery<T> = interface;
IBoundQuery<T> = interface(IBaseBoundQuery<T>)
function GetEnumerator: IBoundQuery<T>;
// query operations
function Map(Transformer : TFunc<T, T>) : IBoundQuery<T>;
function Skip(Count : Integer): IBoundQuery<T>;
function SkipWhile(Predicate : TPredicate<T>) : IBoundQuery<T>; overload;
function SkipWhile(UnboundQuery : IUnboundQuery<T>) : IBoundQuery<T>; overload;
function Take(Count : Integer): IBoundQuery<T>;
function TakeWhile(Predicate : TPredicate<T>): IBoundQuery<T>; overload;
function TakeWhile(UnboundQuery : IUnboundQuery<T>): IBoundQuery<T>; overload;
function Where(Predicate : TPredicate<T>) : IBoundQuery<T>;
function WhereNot(UnboundQuery : IUnboundQuery<T>) : IBoundQuery<T>; overload;
function WhereNot(Predicate : TPredicate<T>) : IBoundQuery<T>; overload;
// terminating operations
function AsTList : TList<T>;
end;
IUnboundQuery<T> = interface(IBaseUnboundQuery<T>)
function GetEnumerator: IUnboundQuery<T>;
function From(Container : TEnumerable<T>) : IBoundQuery<T>;
// query operations
function Map(Transformer : TFunc<T, T>) : IUnboundQuery<T>;
function Skip(Count : Integer): IUnboundQuery<T>;
function SkipWhile(Predicate : TPredicate<T>) : IUnboundQuery<T>; overload;
function SkipWhile(UnboundQuery : IUnboundQuery<T>) : IUnboundQuery<T>; overload;
function Take(Count : Integer): IUnboundQuery<T>;
function TakeWhile(Predicate : TPredicate<T>): IUnboundQuery<T>; overload;
function TakeWhile(UnboundQuery : IUnboundQuery<T>): IUnboundQuery<T>; overload;
function Where(Predicate : TPredicate<T>) : IUnboundQuery<T>;
function WhereNot(UnboundQuery : IUnboundQuery<T>) : IUnboundQuery<T>; overload;
function WhereNot(Predicate : TPredicate<T>) : IUnboundQuery<T>; overload;
end;
TQuery<T> = class(TBaseQuery<T>,
IBoundQuery<T>,
IUnboundQuery<T>)
protected
type
TQueryImpl<TReturnType : IBaseQuery<T>> = class
private
FQuery : TQuery<T>;
public
constructor Create(Query : TQuery<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(Container : TEnumerable<T>) : IBoundQuery<T>;
// Primitive Operations
function Map(Transformer : TFunc<T, 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 Skip(Count : Integer): TReturnType;
function SkipWhile(UnboundQuery : IUnboundQuery<T>) : TReturnType; overload;
function Take(Count : Integer): TReturnType;
function TakeWhile(UnboundQuery : IUnboundQuery<T>): TReturnType; overload;
function WhereNot(UnboundQuery : IUnboundQuery<T>) : TReturnType; overload;
function WhereNot(Predicate : TPredicate<T>) : TReturnType; overload;
// Terminating Operations
function Predicate : TPredicate<T>;
function AsTList : TList<T>;
function First : T;
function Count : Integer;
end;
protected
FBoundQuery : TQueryImpl<IBoundQuery<T>>;
FUnboundQuery : TQueryImpl<IUnboundQuery<T>>;
public
constructor Create(EnumerationStrategy : TEnumerationStrategy<T>;
UpstreamQuery : IBaseQuery<T> = nil;
SourceData : IMinimalEnumerator<T> = nil); override;
destructor Destroy; override;
property BoundQuery : TQueryImpl<IBoundQuery<T>>
read FBoundQuery implements IBoundQuery<T>;
property UnboundQuery : TQueryImpl<IUnboundQuery<T>>
read FUnboundQuery implements IUnboundQuery<T>;
end;
GenericQuery<T> = class
public
class function Select : IUnboundQuery<T>;
end;
implementation
uses FluentQuery.Core.MethodFactories, FluentQuery.Core.Reduce;
class function GenericQuery<T>.Select: IUnboundQuery<T>;
begin
Result := TQuery<T>.Create(TEnumerationStrategy<T>.Create);
{$IFDEF DEBUG}
Result.OperationName := 'GenericQuery<T>.Select';
{$ENDIF}
end;
{ TQueryEnumerator<T> }
function TQuery<T>.TQueryImpl<TReturnType>.Count: Integer;
begin
Result := TReducer<T,Integer>.Reduce(FQuery,
0,
function(Accumulator : Integer; NextValue : T): Integer
begin
Result := Accumulator + 1;
end);
end;
constructor TQuery<T>.TQueryImpl<TReturnType>.Create(
Query: TQuery<T>);
begin
FQuery := Query;
end;
function TQuery<T>.TQueryImpl<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 TQuery<T>.TQueryImpl<TReturnType>.From(
Container: TEnumerable<T>): IBoundQuery<T>;
var
EnumeratorWrapper : IMinimalEnumerator<T>;
begin
EnumeratorWrapper := TGenericEnumeratorAdapter<T>.Create(Container.GetEnumerator) as IMinimalEnumerator<T>;
Result := TQuery<T>.Create(TEnumerationStrategy<T>.Create,
IBaseQuery<T>(FQuery),
EnumeratorWrapper);
{$IFDEF DEBUG}
Result.OperationName := Format('From(%s)', [Container.ToString]);
{$ENDIF}
end;
function TQuery<T>.TQueryImpl<TReturnType>.GetEnumerator: TReturnType;
begin
Result := FQuery;
end;
{$IFDEF DEBUG}
function TQuery<T>.TQueryImpl<TReturnType>.GetOperationName: String;
begin
Result := FQuery.OperationName;
end;
function TQuery<T>.TQueryImpl<TReturnType>.GetOperationPath: String;
begin
Result := FQuery.OperationPath;
end;
{$ENDIF}
function TQuery<T>.TQueryImpl<TReturnType>.Map(
Transformer: TFunc<T, T>): TReturnType;
begin
Result := TQuery<T>.Create(TIsomorphicTransformEnumerationStrategy<T>.Create(Transformer),
IBaseQuery<T>(FQuery));
{$IFDEF DEBUG}
Result.OperationName := 'Map(Transformer)';
{$ENDIF}
end;
function TQuery<T>.TQueryImpl<TReturnType>.Predicate: TPredicate<T>;
begin
Result := TMethodFactory<T>.QuerySingleValue(FQuery);
end;
function TQuery<T>.TQueryImpl<TReturnType>.Skip(Count: Integer): TReturnType;
begin
Result := SkipWhile(TMethodFactory<T>.UpToNumberOfTimes(Count));
{$IFDEF DEBUG}
Result.OperationName := Format('Skip(%d)', [Count]);
{$ENDIF}
end;
function TQuery<T>.TQueryImpl<TReturnType>.SkipWhile(
UnboundQuery: IUnboundQuery<T>): TReturnType;
begin
Result := SkipWhile(UnboundQuery.Predicate);
{$IFDEF DEBUG}
Result.OperationName := Format('SkipWhile', [UnboundQuery.OperationPath]);
{$ENDIF}
end;
function TQuery<T>.TQueryImpl<TReturnType>.SkipWhile(
Predicate: TPredicate<T>): TReturnType;
begin
Result := TQuery<T>.Create(TSkipWhileEnumerationStrategy<T>.Create(Predicate),
IBaseQuery<T>(FQuery));
{$IFDEF DEBUG}
Result.OperationName := 'SkipWhile(Predicate)';
{$ENDIF}
end;
function TQuery<T>.TQueryImpl<TReturnType>.Take(Count: Integer): TReturnType;
begin
Result := TakeWhile(TMethodFactory<T>.UpToNumberOfTimes(Count));
{$IFDEF DEBUG}
Result.OperationName := Format('Take(%d)', [Count]);
{$ENDIF}
end;
function TQuery<T>.TQueryImpl<TReturnType>.TakeWhile(
UnboundQuery: IUnboundQuery<T>): TReturnType;
begin
Result := TakeWhile(UnboundQuery.Predicate);
{$IFDEF DEBUG}
Result.OperationName := Format('TakeWhile', [UnboundQuery.OperationPath]);
{$ENDIF}
end;
function TQuery<T>.TQueryImpl<TReturnType>.TakeWhile(
Predicate: TPredicate<T>): TReturnType;
begin
Result := TQuery<T>.Create(TTakeWhileEnumerationStrategy<T>.Create(Predicate),
IBaseQuery<T>(FQuery));
{$IFDEF DEBUG}
Result.OperationName := 'TakeWhile(Predicate)';
{$ENDIF}
end;
function TQuery<T>.TQueryImpl<TReturnType>.AsTList: TList<T>;
begin
Result := TReducer<T,TList<T>>.Reduce(FQuery,
TList<T>.Create,
function(Accumulator : TList<T>; NextValue : T): TList<T>
begin
Accumulator.Add(NextValue);
Result := Accumulator;
end);
end;
function TQuery<T>.TQueryImpl<TReturnType>.Where(
Predicate: TPredicate<T>): TReturnType;
begin
Result := TQuery<T>.Create(TWhereEnumerationStrategy<T>.Create(Predicate),
IBaseQuery<T>(FQuery));
{$IFDEF DEBUG}
Result.OperationName := 'Where(Predicate)';
{$ENDIF}
end;
function TQuery<T>.TQueryImpl<TReturnType>.WhereNot(
UnboundQuery: IUnboundQuery<T>): TReturnType;
begin
Result := WhereNot(UnboundQuery.Predicate);
{$IFDEF DEBUG}
Result.OperationName := Format('WhereNot(%s)', [UnboundQuery.OperationPath]);
{$ENDIF}
end;
function TQuery<T>.TQueryImpl<TReturnType>.WhereNot(
Predicate: TPredicate<T>): TReturnType;
begin
Result := Where(TMethodFactory<T>.InvertPredicate(Predicate));
{$IFDEF DEBUG}
Result.OperationName := 'WhereNot(Predicate)';
{$ENDIF}
end;
{ TQueryEnumerator<T> }
constructor TQuery<T>.Create(
EnumerationStrategy: TEnumerationStrategy<T>;
UpstreamQuery: IBaseQuery<T>; SourceData: IMinimalEnumerator<T>);
begin
inherited Create(EnumerationStrategy, UpstreamQuery, SourceData);
FBoundQuery := TQueryImpl<IBoundQuery<T>>.Create(self);
FUnboundQuery := TQueryImpl<IUnboundQuery<T>>.Create(self);
end;
destructor TQuery<T>.Destroy;
begin
FBoundQuery.Free;
FUnboundQuery.Free;
inherited;
end;
end.