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
|
type
TCheckType = (all, atLeastOne, OnlyOne);
Test<T> = record
private
datas : T;
function check(aCheckType : TCheckType; conditions: TArray<boolean>; ifTrue, ifFalse : T):T;
public
constructor Create(const source : T);
function AllAreTrue(conditions: TArray<boolean>; ifTrue, ifFalse : T): T;
function AtLeastOneIsTrue(conditions: TArray<boolean>; ifTrue, ifFalse : T): T;
function OnlyOneIsTrue(conditions: TArray<boolean>; ifTrue, ifFalse : T): T;
end;
implementation
{$R *.fmx}
constructor Test<T>.Create(const source : T);
begin
inherited;
datas := source;
end;
function Test<T>.AllAreTrue(conditions: TArray<boolean>; ifTrue, ifFalse: T): T;
begin
result := check(TCheckType.all, conditions, ifTrue, ifFalse);
end;
function Test<T>.AtLeastOneIsTrue(conditions: TArray<boolean>; ifTrue, ifFalse: T): T;
begin
result := check(TCheckType.atLeastOne, conditions, ifTrue, ifFalse);
end;
function Test<T>.OnlyOneIsTrue(conditions: TArray<boolean>; ifTrue, ifFalse: T): T;
begin
result := check(TCheckType.OnlyOne, conditions, ifTrue, ifFalse);
end;
function Test<T>.check(aCheckType: TCheckType; conditions: TArray<boolean>; ifTrue, ifFalse : T): T;
begin
if length(conditions) = 0 then exit(Default(T));
var isOk := true;
var count := 0;
for var aCondition in conditions do begin
case aCheckType of
TCheckType.all: begin
if not(aCondition) then begin
isOk := false;
break;
end;
end;
TCheckType.atLeastOne: begin
if aCondition then begin
isOk := true;
break;
end else isOk := false;
end;
TCheckType.OnlyOne: begin
if aCondition then begin
inc(count);
if count > 1 then isOk := false;
end;
end;
end;
end;
if isOk then result := ifTrue
else result := ifFalse;
end; |
Partager