forked from malcolmgroves/FluentQuery
-
Notifications
You must be signed in to change notification settings - Fork 4
/
FluentQuery.Core.MethodFactories.pas
83 lines (72 loc) · 2.61 KB
/
FluentQuery.Core.MethodFactories.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
{****************************************************}
{ }
{ 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.MethodFactories;
interface
uses
System.SysUtils, FluentQuery.Core.Types;
type
TMethodFactory<T> = class
public
class function UpToNumberOfTimes(Count : Integer) : TPredicate<T>;
class function QuerySingleValue(UnboundQuery : IBaseQuery<T>) : TPredicate<T>;
class function InvertPredicate(Predicate : TPredicate<T>) : TPredicate<T>;
class function InPlaceTransformer(TransformProc : TProc<T>) : TFunc<T, T>;
end;
implementation
uses FluentQuery.Core.Enumerators;
{ TPredicateFactory<T> }
class function TMethodFactory<T>.InPlaceTransformer(
TransformProc: TProc<T>): TFunc<T, T>;
begin
Result := function (Value : T) : T
begin
TransformProc(Value);
Result := Value;
end;
end;
class function TMethodFactory<T>.InvertPredicate(
Predicate: TPredicate<T>): TPredicate<T>;
begin
Result := function (CurrentValue : T) : Boolean
begin
Result := not Predicate(CurrentValue);
end;
end;
class function TMethodFactory<T>.UpToNumberOfTimes(
Count: Integer): TPredicate<T>;
var
LCount : Integer;
begin
LCOunt := 0;
Result := function (Value : T) : boolean
begin
Inc(LCount);
Result := LCount <= Count;
end;
end;
class function TMethodFactory<T>.QuerySingleValue(
UnboundQuery: IBaseQuery<T>): TPredicate<T>;
begin
Result := function (CurrentValue : T) : Boolean
begin
UnboundQuery.SetSourceData(TSingleValueAdapter<T>.Create(CurrentValue));
Result := UnboundQuery.MoveNext;
end;
end;
end.