|
| 1 | +unit C4D.Validate.Components.ListPair; |
| 2 | + |
| 3 | +interface |
| 4 | + |
| 5 | +uses |
| 6 | + System.Types, |
| 7 | + System.SysUtils, |
| 8 | + System.Classes, |
| 9 | + System.Generics.Defaults, |
| 10 | + System.Generics.Collections; |
| 11 | + |
| 12 | +type |
| 13 | + TListPair<TKey, TValue> = class(TList<TPair<TKey, TValue>>) |
| 14 | + protected |
| 15 | + FKeyComparer: IComparer<TKey>; |
| 16 | + FValueComparer: IComparer<TValue>; |
| 17 | + function GetValue(Key: TKey): TValue; |
| 18 | + procedure SetValue(Key: TKey; const Value: TValue); |
| 19 | + function ComparePair(const Left, Right: TPair<TKey, TValue>): Integer; |
| 20 | + public |
| 21 | + constructor Create; overload; |
| 22 | + procedure Add(const AKey: TKey; const aValue: TValue); overload; |
| 23 | + function IndexOfKey(const AKey: TKey): Integer; |
| 24 | + function ContainsKey(const AKey: TKey): Boolean; inline; |
| 25 | + property Values[Key: TKey]: TValue read GetValue write SetValue; |
| 26 | + end; |
| 27 | + |
| 28 | + TListPairStr = class(TListPair<string, string>); |
| 29 | + |
| 30 | + TListPairStrHelper = class helper for TListPairStr |
| 31 | + procedure FillStringsWithValues(const AStrings: TStrings); |
| 32 | + function GetStringsValues: string; |
| 33 | + end; |
| 34 | + |
| 35 | +implementation |
| 36 | + |
| 37 | +constructor TListPair<TKey, TValue>.Create; |
| 38 | +begin |
| 39 | + if(FKeyComparer = nil)then |
| 40 | + FKeyComparer := TComparer<TKey>.Default; |
| 41 | + if(FValueComparer = nil)then |
| 42 | + FValueComparer := TComparer<TValue>.Default; |
| 43 | + inherited Create(TDelegatedComparer <TPair<TKey, TValue>>.Create(ComparePair)); |
| 44 | +end; |
| 45 | + |
| 46 | +function TListPair<TKey, TValue>.ComparePair(const Left, Right: TPair<TKey, TValue>): Integer; |
| 47 | +begin |
| 48 | + Result := FKeyComparer.Compare(Left.Key, Right.Key); |
| 49 | + if(Result = 0)then |
| 50 | + Result := FValueComparer.Compare(Left.Value, Right.Value); |
| 51 | +end; |
| 52 | + |
| 53 | +function TListPair<TKey, TValue>.IndexOfKey(const AKey: TKey): Integer; |
| 54 | +var |
| 55 | + i: Integer; |
| 56 | +begin |
| 57 | + Result := -1; |
| 58 | + for i := 0 to Count - 1 do |
| 59 | + begin |
| 60 | + if(FKeyComparer.Compare(Items[i].Key, AKey) = 0)then |
| 61 | + begin |
| 62 | + Result := i; |
| 63 | + break; |
| 64 | + end; |
| 65 | + end; |
| 66 | +end; |
| 67 | + |
| 68 | +function TListPair<TKey, TValue>.ContainsKey(const AKey: TKey): Boolean; |
| 69 | +begin |
| 70 | + Result := IndexOfKey(AKey) >= 0; |
| 71 | +end; |
| 72 | + |
| 73 | +function TListPair<TKey, TValue>.GetValue(Key: TKey): TValue; |
| 74 | +var |
| 75 | + i: Integer; |
| 76 | +begin |
| 77 | + i := IndexOfKey(Key); |
| 78 | + if(i >= 0)then |
| 79 | + Result := Items[i].Value |
| 80 | + else |
| 81 | + Result := default(TValue); |
| 82 | +end; |
| 83 | + |
| 84 | +procedure TListPair<TKey, TValue>.Add(const AKey: TKey; const aValue: TValue); |
| 85 | +begin |
| 86 | + SetValue(AKey, aValue); |
| 87 | +end; |
| 88 | + |
| 89 | +procedure TListPair<TKey, TValue>.SetValue(Key: TKey; const Value: TValue); |
| 90 | +begin |
| 91 | + inherited Add(TPair<TKey, TValue>.Create(Key, Value)); |
| 92 | +end; |
| 93 | + |
| 94 | +{ TListPairStrHelper } |
| 95 | +procedure TListPairStrHelper.FillStringsWithValues(const AStrings: TStrings); |
| 96 | +var |
| 97 | + LPairErro: TPair<string, string>; |
| 98 | +begin |
| 99 | + if(Self.Count > 0)then |
| 100 | + for LPairErro in Self do |
| 101 | + AStrings.Add('* ' + LPairErro.Value); |
| 102 | +end; |
| 103 | + |
| 104 | +function TListPairStrHelper.GetStringsValues: string; |
| 105 | +var |
| 106 | + LPairErro: TPair<string, string>; |
| 107 | +begin |
| 108 | + Result := ''; |
| 109 | + if(Self.Count > 0)then |
| 110 | + for LPairErro in Self do |
| 111 | + Result := Result + '* ' + LPairErro.Value + sLineBreak; |
| 112 | +end; |
| 113 | + |
| 114 | +end. |
0 commit comments