12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271 |
- { ***************************************************************************
- Copyright (c) 2016-2021 Kike Pérez
- Unit : Quick.Conditions
- Description : Conditions validator
- Author : Kike Pérez
- Version : 2.0
- Created : 05/05/2021
- Modified : 11/05/2021
- This file is part of QuickLib: https://github.com/exilon/QuickLib
- ***************************************************************************
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- *************************************************************************** }
- unit Quick.Conditions;
- {$i QuickLib.inc}
- interface
- uses
- System.SysUtils,
- System.StrUtils,
- Quick.Commons;
- type
- ICondition = interface
- ['{54F1E937-CE14-426A-9FC5-C6C7944915A2}']
- end;
- TCondition = class(TInterfacedObject,ICondition)
- protected
- fName : string;
- fExceptionClass : ExceptClass;
- fPostCondition : Boolean;
- public
- constructor Create;
- procedure ThrowException(const aMsg : string); overload;
- procedure ThrowException(const aMsg : string; aValues : array of const); overload;
- procedure ThrowException(aExceptionClass : ExceptClass; const aMsg : string); overload;
- end;
- IStringCondition = interface(ICondition)
- ['{B9591175-22E0-4624-94E2-B183DEE1F793}']
- function WithExceptionOnFailure(aExceptionClass : ExceptClass) : IStringCondition;
- function IsEmpty : IStringCondition; overload;
- function IsEmpty(const aCustomMessage : string) : IStringCondition; overload;
- function IsNotEmpty : IStringCondition; overload;
- function IsNotEmpty(const aCustomMessage : string) : IStringCondition; overload;
- function StartsWith(const aText : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function StartsWith(const aText, aCustomMessage : string; aIgnoreCase : Boolean = False): IStringCondition; overload;
- function DoesNotStartsWith(const aText : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function DoesNotStartsWith(const aText, aCustomMessage : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function EndsWith(const aText : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function EndsWith(const aText, aCustomMessage : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function DoesNotEndsWith(const aText : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function DoesNotEndsWith(const aText,aCustomMessage : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function Contains(const aText : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function Contains(const aText, aCustomMessage : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function DoesNotContains(const aText: string; aIgnoreCase: Boolean = False): IStringCondition; overload;
- function DoesNotContains(const aText, aCustomMessage: string; aIgnoreCase: Boolean = False): IStringCondition; overload;
- function HasLength(aLen : Integer) : IStringCondition; overload;
- function HasLength(aLen : Integer; const aCustomMessage : string) : IStringCondition; overload;
- function DoesNotHasLength(aLen : Integer) : IStringCondition; overload;
- function DoesNotHasLength(aLen : Integer; const aCustomMessage : string) : IStringCondition; overload;
- function IsLongerThan(aLen : Integer) : IStringCondition; overload;
- function IsLongerThan(aLen : Integer; const aCustomMessage : string) : IStringCondition; overload;
- function IsLongerOrEqual(aLen : Integer) : IStringCondition; overload;
- function IsLongerOrEqual(aLen : Integer; const aCustomMessage : string) : IStringCondition; overload;
- function IsShorterThan(aLen : Integer) : IStringCondition; overload;
- function IsShorterThan(aLen : Integer; const aCustomMessage : string) : IStringCondition; overload;
- function IsShorterOrEqual(aLen : Integer) : IStringCondition; overload;
- function IsShorterOrEqual(aLen : Integer; const aCustomMessage : string) : IStringCondition; overload;
- function HasLengthRange(aMin, aMax : Integer) : IStringCondition; overload;
- function HasLengthRange(aMin, aMax : Integer; const aCustomMessage : string) : IStringCondition; overload;
- function IsUpperCase : IStringCondition; overload;
- function IsUpperCase(const aCustomMessage : string) : IStringCondition; overload;
- function IsLowerCase : IStringCondition; overload;
- function IsLowerCase(const aCustomMessage : string) : IStringCondition; overload;
- function IsNotUpperCase : IStringCondition; overload;
- function IsNotUpperCase(const aCustomMessage : string) : IStringCondition; overload;
- function IsNotLowerCase : IStringCondition; overload;
- function IsNotLowerCase(const aCustomMessage : string) : IStringCondition; overload;
- function Evaluate(aExpression : Boolean) : IStringCondition; overload;
- function Evaluate(aExpression : Boolean; const aCustomMessage : string) : IStringCondition; overload;
- end;
- IIntegerCondition = interface(ICondition)
- ['{A34856DD-175B-40BB-BC64-CF131CB448C7}']
- function WithExceptionOnFailure(aExceptionClass : ExceptClass) : IIntegerCondition;
- function IsInRange(aMin, aMax : Int64) : IIntegerCondition; overload;
- function IsInRange(aMin, aMax : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsNotInRange(aMin, aMax : Int64) : IIntegerCondition; overload;
- function IsNotInRange(aMin, aMax : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsEqualTo(aValue : Int64) : IIntegerCondition; overload;
- function IsEqualTo(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsNotEqualTo(aValue : Int64) : IIntegerCondition; overload;
- function IsNotEqualTo(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsGreaterThan(aValue : Int64) : IIntegerCondition; overload;
- function IsGreaterThan(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsNotGreaterThan(aValue : Int64) : IIntegerCondition; overload;
- function IsNotGreaterThan(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsGreaterOrEqual(aValue : Int64) : IIntegerCondition; overload;
- function IsGreaterOrEqual(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsNotGreaterOrEqual(aValue : Int64) : IIntegerCondition; overload;
- function IsNotGreaterOrEqual(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsLessThan(aValue : Int64) : IIntegerCondition; overload;
- function IsLessThan(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsNotLessThan(aValue : Int64) : IIntegerCondition; overload;
- function IsNotLessThan(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsLessOrEqual(aValue : Int64) : IIntegerCondition; overload;
- function IsLessOrEqual(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsNotLessOrEqual(aValue : Int64) : IIntegerCondition; overload;
- function IsNotLessOrEqual(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function Evaluate(aExpression : Boolean) : IIntegerCondition; overload;
- function Evaluate(aExpression : Boolean; const aCustomMessage : string) : IIntegerCondition; overload;
- end;
- IFloatCondition = interface(ICondition)
- ['{D0237A24-A00F-4B96-BA7B-0FF9BE7363E5}']
- function WithExceptionOnFailure(aExceptionClass : ExceptClass) : IFloatCondition;
- function IsInRange(aMin, aMax : Extended) : IFloatCondition; overload;
- function IsInRange(aMin, aMax : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsNotInRange(aMin, aMax : Extended) : IFloatCondition; overload;
- function IsNotInRange(aMin, aMax : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsEqualTo(aValue : Extended) : IFloatCondition; overload;
- function IsEqualTo(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsNotEqualTo(aValue : Extended) : IFloatCondition; overload;
- function IsNotEqualTo(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsGreaterThan(aValue : Extended) : IFloatCondition; overload;
- function IsGreaterThan(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsNotGreaterThan(aValue : Extended) : IFloatCondition; overload;
- function IsNotGreaterThan(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsGreaterOrEqual(aValue : Extended) : IFloatCondition; overload;
- function IsGreaterOrEqual(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsNotGreaterOrEqual(aValue : Extended) : IFloatCondition; overload;
- function IsNotGreaterOrEqual(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsLessThan(aValue : Extended) : IFloatCondition; overload;
- function IsLessThan(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsNotLessThan(aValue : Extended) : IFloatCondition; overload;
- function IsNotLessThan(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsLessOrEqual(aValue : Extended) : IFloatCondition; overload;
- function IsLessOrEqual(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsNotLessOrEqual(aValue : Extended) : IFloatCondition; overload;
- function IsNotLessOrEqual(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function Evaluate(aExpression : Boolean) : IFloatCondition; overload;
- function Evaluate(aExpression : Boolean; const aCustomMessage : string) : IFloatCondition; overload;
- end;
- IObjectCondition = interface(ICondition)
- ['{497E21D2-7780-4C3B-B51E-921847491FC1}']
- function WithExceptionOnFailure(aExceptionClass : ExceptClass) : IObjectCondition;
- function IsNull : IObjectCondition; overload;
- function IsNull(const aCustomMessage : string): IObjectCondition; overload;
- function IsNotNull : IObjectCondition; overload;
- function IsNotNull(const aCustomMessage : string) : IObjectCondition; overload;
- function IsOfType(aClass : TClass) : IObjectCondition; overload;
- function IsOfType(aClass : TClass; const aCustomMessage : string) : IObjectCondition; overload;
- function DoesNotOfType(aClass : TClass) : IObjectCondition; overload;
- function DoesNotOfType(aClass : TClass; const aCustomMessage : string) : IObjectCondition; overload;
- function Evaluate(aExpression : Boolean) : IObjectCondition; overload;
- function Evaluate(aExpression : Boolean; const aCustomMessage : string) : IObjectCondition; overload;
- end;
- TStringCondition = class(TCondition,IStringCondition)
- private
- fValue : string;
- public
- constructor Create(const aValue : string; const aName : string; aPostCondition : Boolean);
- function WithExceptionOnFailure(aExceptionClass : ExceptClass) : IStringCondition;
- function IsEmpty : IStringCondition; overload;
- function IsEmpty(const aCustomMessage : string) : IStringCondition; overload;
- function IsNotEmpty : IStringCondition; overload;
- function IsNotEmpty(const aCustomMessage : string) : IStringCondition; overload;
- function StartsWith(const aText : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function StartsWith(const aText, aCustomMessage : string; aIgnoreCase : Boolean = False): IStringCondition; overload;
- function DoesNotStartsWith(const aText : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function DoesNotStartsWith(const aText, aCustomMessage : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function EndsWith(const aText : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function EndsWith(const aText, aCustomMessage : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function DoesNotEndsWith(const aText : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function DoesNotEndsWith(const aText,aCustomMessage : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function Contains(const aText : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function Contains(const aText, aCustomMessage : string; aIgnoreCase : Boolean = False) : IStringCondition; overload;
- function DoesNotContains(const aText: string; aIgnoreCase: Boolean = False): IStringCondition; overload;
- function DoesNotContains(const aText, aCustomMessage: string; aIgnoreCase: Boolean = False): IStringCondition; overload;
- function HasLength(aLen : Integer) : IStringCondition; overload;
- function HasLength(aLen : Integer; const aCustomMessage : string) : IStringCondition; overload;
- function DoesNotHasLength(aLen : Integer) : IStringCondition; overload;
- function DoesNotHasLength(aLen : Integer; const aCustomMessage : string) : IStringCondition; overload;
- function IsLongerThan(aLen : Integer) : IStringCondition; overload;
- function IsLongerThan(aLen : Integer; const aCustomMessage : string) : IStringCondition; overload;
- function IsLongerOrEqual(aLen : Integer) : IStringCondition; overload;
- function IsLongerOrEqual(aLen : Integer; const aCustomMessage : string) : IStringCondition; overload;
- function IsShorterThan(aLen : Integer) : IStringCondition; overload;
- function IsShorterThan(aLen : Integer; const aCustomMessage : string) : IStringCondition; overload;
- function IsShorterOrEqual(aLen : Integer) : IStringCondition; overload;
- function IsShorterOrEqual(aLen : Integer; const aCustomMessage : string) : IStringCondition; overload;
- function HasLengthRange(aMin, aMax : Integer) : IStringCondition; overload;
- function HasLengthRange(aMin, aMax : Integer; const aCustomMessage : string) : IStringCondition; overload;
- function IsUpperCase : IStringCondition; overload;
- function IsUpperCase(const aCustomMessage : string) : IStringCondition; overload;
- function IsLowerCase : IStringCondition; overload;
- function IsLowerCase(const aCustomMessage : string) : IStringCondition; overload;
- function IsNotUpperCase : IStringCondition; overload;
- function IsNotUpperCase(const aCustomMessage : string) : IStringCondition; overload;
- function IsNotLowerCase : IStringCondition; overload;
- function IsNotLowerCase(const aCustomMessage : string) : IStringCondition; overload;
- function Evaluate(aExpression : Boolean) : IStringCondition; overload;
- function Evaluate(aExpression : Boolean; const aCustomMessage : string) : IStringCondition; overload;
- end;
- TIntegerCondition = class(TCondition,IIntegerCondition)
- private
- fValue : Int64;
- public
- constructor Create(const aValue : Int64; const aName : string; aPostCondition : Boolean);
- function WithExceptionOnFailure(aExceptionClass : ExceptClass) : IIntegerCondition;
- function IsInRange(aMin, aMax : Int64) : IIntegerCondition; overload;
- function IsInRange(aMin, aMax : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsNotInRange(aMin, aMax : Int64) : IIntegerCondition; overload;
- function IsNotInRange(aMin, aMax : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsEqualTo(aValue : Int64) : IIntegerCondition; overload;
- function IsEqualTo(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsNotEqualTo(aValue : Int64) : IIntegerCondition; overload;
- function IsNotEqualTo(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsGreaterThan(aValue : Int64) : IIntegerCondition; overload;
- function IsGreaterThan(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsNotGreaterThan(aValue : Int64) : IIntegerCondition; overload;
- function IsNotGreaterThan(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsGreaterOrEqual(aValue : Int64) : IIntegerCondition; overload;
- function IsGreaterOrEqual(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsNotGreaterOrEqual(aValue : Int64) : IIntegerCondition; overload;
- function IsNotGreaterOrEqual(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsLessThan(aValue : Int64) : IIntegerCondition; overload;
- function IsLessThan(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsNotLessThan(aValue : Int64) : IIntegerCondition; overload;
- function IsNotLessThan(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsLessOrEqual(aValue : Int64) : IIntegerCondition; overload;
- function IsLessOrEqual(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function IsNotLessOrEqual(aValue : Int64) : IIntegerCondition; overload;
- function IsNotLessOrEqual(aValue : Int64; const aCustomMessage : string) : IIntegerCondition; overload;
- function Evaluate(aExpression : Boolean) : IIntegerCondition; overload;
- function Evaluate(aExpression : Boolean; const aCustomMessage : string) : IIntegerCondition; overload;
- end;
- TFloatCondition = class(TCondition,IFloatCondition)
- private
- fValue : Extended;
- public
- constructor Create(const aValue : Extended; const aName : string; aPostCondition : Boolean);
- function WithExceptionOnFailure(aExceptionClass : ExceptClass) : IFloatCondition;
- function IsInRange(aMin, aMax : Extended) : IFloatCondition; overload;
- function IsInRange(aMin, aMax : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsNotInRange(aMin, aMax : Extended) : IFloatCondition; overload;
- function IsNotInRange(aMin, aMax : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsEqualTo(aValue : Extended) : IFloatCondition; overload;
- function IsEqualTo(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsNotEqualTo(aValue : Extended) : IFloatCondition; overload;
- function IsNotEqualTo(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsGreaterThan(aValue : Extended) : IFloatCondition; overload;
- function IsGreaterThan(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsNotGreaterThan(aValue : Extended) : IFloatCondition; overload;
- function IsNotGreaterThan(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsGreaterOrEqual(aValue : Extended) : IFloatCondition; overload;
- function IsGreaterOrEqual(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsNotGreaterOrEqual(aValue : Extended) : IFloatCondition; overload;
- function IsNotGreaterOrEqual(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsLessThan(aValue : Extended) : IFloatCondition; overload;
- function IsLessThan(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsNotLessThan(aValue : Extended) : IFloatCondition; overload;
- function IsNotLessThan(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsLessOrEqual(aValue : Extended) : IFloatCondition; overload;
- function IsLessOrEqual(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function IsNotLessOrEqual(aValue : Extended) : IFloatCondition; overload;
- function IsNotLessOrEqual(aValue : Extended; const aCustomMessage : string) : IFloatCondition; overload;
- function Evaluate(aExpression : Boolean) : IFloatCondition; overload;
- function Evaluate(aExpression : Boolean; const aCustomMessage : string) : IFloatCondition; overload;
- end;
- TObjectCondition = class(TCondition,IObjectCondition)
- private
- fValue : TObject;
- public
- constructor Create(const aValue : TObject; const aName : string; aPostCondition : Boolean);
- function WithExceptionOnFailure(aExceptionClass : ExceptClass) : IObjectCondition;
- function IsNull : IObjectCondition; overload;
- function IsNull(const aCustomMessage : string): IObjectCondition; overload;
- function IsNotNull : IObjectCondition; overload;
- function IsNotNull(const aCustomMessage : string) : IObjectCondition; overload;
- function IsOfType(aClass : TClass) : IObjectCondition; overload;
- function IsOfType(aClass : TClass; const aCustomMessage : string) : IObjectCondition; overload;
- function DoesNotOfType(aClass : TClass) : IObjectCondition; overload;
- function DoesNotOfType(aClass : TClass; const aCustomMessage : string) : IObjectCondition; overload;
- function Evaluate(aExpression : Boolean) : IObjectCondition; overload;
- function Evaluate(aExpression : Boolean; const aCustomMessage : string) : IObjectCondition; overload;
- end;
- IConditionValidator = interface
- ['{F707606E-7603-4690-BE76-2443B0A36D5F}']
- function Requires(const aValue : string; const aName : string = '') : IStringCondition; overload;
- function Requires(const aValue : Int64; const aName : string = '') : IIntegerCondition; overload;
- function Requires(const aValue : Extended; const aName : string = '') : IFloatCondition; overload;
- function Requires(const aValue : TObject; const aName : string = '') : IObjectCondition; overload;
- function Ensures(const aValue : string; const aName : string = '') : IStringCondition; overload;
- function Ensures(const aValue : Int64; const aName : string = '') : IIntegerCondition; overload;
- function Ensures(const aValue : Extended; const aName : string = '') : IFloatCondition; overload;
- function Ensures(const aValue : TObject; const aName : string = '') : IObjectCondition; overload;
- end;
- TConditionValidator = class(TInterfacedObject,IConditionValidator)
- public
- function Requires(const aValue : string; const aName : string = '') : IStringCondition; overload;
- function Requires(const aValue : Int64; const aName : string = '') : IIntegerCondition; overload;
- function Requires(const aValue : Extended; const aName : string = '') : IFloatCondition; overload;
- function Requires(const aValue : TObject; const aName : string = '') : IObjectCondition; overload;
- function Ensures(const aValue : string; const aName : string = '') : IStringCondition; overload;
- function Ensures(const aValue : Int64; const aName : string = '') : IIntegerCondition; overload;
- function Ensures(const aValue : Extended; const aName : string = '') : IFloatCondition; overload;
- function Ensures(const aValue : TObject; const aName : string = '') : IObjectCondition; overload;
- end;
- EPreConditionError = class(Exception);
- EPostConditionError = class(Exception);
- function Condition : IConditionValidator;
- implementation
- function Condition : IConditionValidator;
- begin
- Result := TConditionValidator.Create;
- end;
- { TEvaluator }
- function TConditionValidator.Requires(const aValue: string; const aName : string = ''): IStringCondition;
- begin
- Result := TStringCondition.Create(aValue,aName,False);
- end;
- function TConditionValidator.Requires(const aValue: Int64; const aName : string = ''): IIntegerCondition;
- begin
- Result := TIntegerCondition.Create(aValue,aName,False);
- end;
- function TConditionValidator.Requires(const aValue: Extended; const aName : string = ''): IFloatCondition;
- begin
- Result := TFloatCondition.Create(aValue,aName,False);
- end;
- function TConditionValidator.Requires(const aValue: TObject; const aName : string = ''): IObjectCondition;
- begin
- Result := TObjectCondition.Create(aValue,aName,False);
- end;
- function TConditionValidator.Ensures(const aValue, aName: string): IStringCondition;
- begin
- Result := TStringCondition.Create(aValue,aName,True);
- end;
- function TConditionValidator.Ensures(const aValue: Int64; const aName: string): IIntegerCondition;
- begin
- Result := TIntegerCondition.Create(aValue,aName,True);
- end;
- function TConditionValidator.Ensures(const aValue: Extended; const aName: string): IFloatCondition;
- begin
- Result := TFloatCondition.Create(aValue,aName,True);
- end;
- function TConditionValidator.Ensures(const aValue: TObject; const aName: string): IObjectCondition;
- begin
- Result := TObjectCondition.Create(aValue,aName,True);
- end;
- { TStringCondition }
- constructor TStringCondition.Create(const aValue: string; const aName : string; aPostCondition : Boolean);
- begin
- fName := aName;
- fValue := aValue;
- fPostCondition := aPostCondition;
- end;
- function TStringCondition.WithExceptionOnFailure(aExceptionClass: ExceptClass): IStringCondition;
- begin
- fExceptionClass := aExceptionClass;
- Result := Self;
- end;
- function TStringCondition.IsEmpty: IStringCondition;
- begin
- Result := Self.IsEmpty('');
- end;
- function TStringCondition.IsEmpty(const aCustomMessage : string): IStringCondition;
- begin
- if not fValue.IsEmpty then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentNilException,aCustomMessage)
- else ThrowException('must be empty');
- end;
- Result := Self;
- end;
- function TStringCondition.IsNotEmpty: IStringCondition;
- begin
- Result := Self.IsNotEmpty('');
- end;
- function TStringCondition.IsNotEmpty(const aCustomMessage : string): IStringCondition;
- begin
- if fValue.IsEmpty then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentNilException,aCustomMessage)
- else ThrowException('should not be empty');
- end;
- Result := Self;
- end;
- function TStringCondition.StartsWith(const aText: string; aIgnoreCase : Boolean = False): IStringCondition;
- begin
- Result := Self.StartsWith(aText,'',aIgnoreCase);
- end;
- function TStringCondition.StartsWith(const aText, aCustomMessage : string; aIgnoreCase : Boolean = False): IStringCondition;
- begin
- if not fValue.StartsWith(aText,aIgnoreCase) then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must start with "%s"',[aText])
- end;
- Result := Self;
- end;
- function TStringCondition.DoesNotStartsWith(const aText: string; aIgnoreCase: Boolean): IStringCondition;
- begin
- Result := Self.DoesNotStartsWith(aText,'',aIgnoreCase);
- end;
- function TStringCondition.DoesNotStartsWith(const aText, aCustomMessage: string; aIgnoreCase: Boolean): IStringCondition;
- begin
- if fValue.StartsWith(aText,aIgnoreCase) then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not start with "%s"',[aText]);
- end;
- Result := Self;
- end;
- function TStringCondition.EndsWith(const aText: string; aIgnoreCase : Boolean = False): IStringCondition;
- begin
- Result := Self.EndsWith(aText,'',aIgnoreCase);
- end;
- function TStringCondition.EndsWith(const aText, aCustomMessage: string; aIgnoreCase : Boolean = False): IStringCondition;
- begin
- if not fValue.EndsWith(aText,aIgnoreCase) then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must end with "%s"',[aText]);
- end;
- Result := Self;
- end;
- function TStringCondition.DoesNotEndsWith(const aText: string; aIgnoreCase: Boolean): IStringCondition;
- begin
- Result := Self.DoesNotEndsWith(aText,'',aIgnoreCase);
- end;
- function TStringCondition.DoesNotEndsWith(const aText, aCustomMessage: string; aIgnoreCase: Boolean): IStringCondition;
- begin
- if fValue.EndsWith(aText,aIgnoreCase) then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not be end with "%s"',[aText]);
- end;
- Result := Self;
- end;
- function TStringCondition.Contains(const aText: string; aIgnoreCase: Boolean): IStringCondition;
- begin
- Result := Self.Contains(aText,'',aIgnoreCase);
- end;
- function TStringCondition.Contains(const aText, aCustomMessage: string; aIgnoreCase: Boolean): IStringCondition;
- begin
- if aIgnoreCase then
- begin
- if not ContainsText(fValue,aText) then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must contain "%s"',[aText]);
- end;
- end
- else
- begin
- if not fValue.Contains(aText) then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must contain "%s"',[aText]);
- end;
- end;
- Result := Self;
- end;
- function TStringCondition.DoesNotContains(const aText: string; aIgnoreCase: Boolean = False): IStringCondition;
- begin
- Result := Self.DoesNotContains(aText,'',aIgnoreCase);
- end;
- function TStringCondition.DoesNotContains(const aText, aCustomMessage: string; aIgnoreCase: Boolean = False): IStringCondition;
- begin
- if aIgnoreCase then
- begin
- if ContainsText(fValue,aText) then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not contain "%s"',[aText]);
- end;
- end
- else
- begin
- if fValue.Contains(aText) then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not contain "%s"',[aText]);
- end;
- end;
- Result := Self;
- end;
- function TStringCondition.HasLength(aLen: Integer): IStringCondition;
- begin
- Result := Self.HasLength(aLen,'');
- end;
- function TStringCondition.HasLength(aLen: Integer; const aCustomMessage : string): IStringCondition;
- begin
- if fValue.Length <> aLen then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be %d length',[aLen]);
- end;
- Result := Self;
- end;
- function TStringCondition.DoesNotHasLength(aLen: Integer): IStringCondition;
- begin
- Result := Self.DoesNotHasLength(aLen,'');
- end;
- function TStringCondition.DoesNotHasLength(aLen: Integer; const aCustomMessage : string): IStringCondition;
- begin
- if fValue.Length = aLen then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not be %d length',[aLen]);
- end;
- Result := Self;
- end;
- function TStringCondition.IsShorterThan(aLen: Integer): IStringCondition;
- begin
- Result := Self.IsShorterThan(aLen,'');
- end;
- function TStringCondition.IsShorterThan(aLen: Integer; const aCustomMessage : string): IStringCondition;
- begin
- if fValue.Length >= aLen then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be shorten than %d',[aLen]);
- end;
- Result := Self;
- end;
- function TStringCondition.IsShorterOrEqual(aLen: Integer): IStringCondition;
- begin
- Result := Self.IsShorterOrEqual(aLen,'');
- end;
- function TStringCondition.IsShorterOrEqual(aLen: Integer; const aCustomMessage : string): IStringCondition;
- begin
- if fValue.Length > aLen then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be shorter or equal to %d',[aLen]);
- end;
- Result := Self;
- end;
- function TStringCondition.IsLongerOrEqual(aLen: Integer): IStringCondition;
- begin
- Result := Self.IsLongerOrEqual(aLen,'');
- end;
- function TStringCondition.IsLongerOrEqual(aLen: Integer; const aCustomMessage : string): IStringCondition;
- begin
- if fValue.Length < aLen then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be longer or equal to %d',[aLen]);
- end;
- Result := Self;
- end;
- function TStringCondition.IsLongerThan(aLen: Integer): IStringCondition;
- begin
- Result := Self.IsLongerThan(aLen,'');
- end;
- function TStringCondition.IsLongerThan(aLen: Integer; const aCustomMessage : string): IStringCondition;
- begin
- if fValue.Length <= aLen then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be longer than %d',[aLen]);
- end;
- Result := Self;
- end;
- function TStringCondition.HasLengthRange(aMin, aMax: Integer): IStringCondition;
- begin
- Result := Self.HasLengthRange(aMin,aMax,'');
- end;
- function TStringCondition.HasLengthRange(aMin, aMax: Integer; const aCustomMessage : string): IStringCondition;
- begin
- if (fValue.Length < aMin) or (fValue.Length > aMax) then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentOutOfRangeException,aCustomMessage)
- else ThrowException('must be in %d-%d length range',[aMin,aMax]);
- end;
- Result := Self;
- end;
- function TStringCondition.IsUpperCase: IStringCondition;
- begin
- Result := Self.IsUpperCase('');
- end;
- function TStringCondition.IsUpperCase(const aCustomMessage : string): IStringCondition;
- begin
- if fValue.ToUpper <> fValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be upper case');
- end;
- Result := Self;
- end;
- function TStringCondition.IsLowerCase: IStringCondition;
- begin
- Result := Self.IsLowerCase('');
- end;
- function TStringCondition.IsLowerCase(const aCustomMessage : string): IStringCondition;
- begin
- if fValue.ToLower <> fValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be lower case');
- end;
- Result := Self;
- end;
- function TStringCondition.IsNotUpperCase: IStringCondition;
- begin
- Result := Self.IsNotUpperCase('');
- end;
- function TStringCondition.IsNotUpperCase(const aCustomMessage : string): IStringCondition;
- begin
- if fValue.ToUpper = fValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not be upper case');
- end;
- Result := Self;
- end;
- function TStringCondition.IsNotLowerCase: IStringCondition;
- begin
- Result := Self.IsNotLowerCase('');
- end;
- function TStringCondition.IsNotLowerCase(const aCustomMessage : string): IStringCondition;
- begin
- if fValue.ToLower = fValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not be lower case');
- end;
- Result := Self;
- end;
- function TStringCondition.Evaluate(aExpression: Boolean): IStringCondition;
- begin
- Result := Self.Evaluate(aExpression,'');
- end;
- function TStringCondition.Evaluate(aExpression: Boolean; const aCustomMessage : string): IStringCondition;
- begin
- if not aExpression then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must meet condition');
- end;
- end;
- { TIntegerCondition }
- constructor TIntegerCondition.Create(const aValue: Int64; const aName : string; aPostCondition : Boolean);
- begin
- fName := aName;
- fValue := aValue;
- fPostCondition := aPostCondition;
- end;
- function TIntegerCondition.WithExceptionOnFailure(aExceptionClass: ExceptClass): IIntegerCondition;
- begin
- fExceptionClass := aExceptionClass;
- Result := Self;
- end;
- function TIntegerCondition.IsEqualTo(aValue: Int64): IIntegerCondition;
- begin
- Result := Self.IsEqualTo(aValue,'');
- end;
- function TIntegerCondition.IsEqualTo(aValue: Int64; const aCustomMessage : string): IIntegerCondition;
- begin
- if fValue <> aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be equal to %d',[aValue]);
- end;
- Result := Self;
- end;
- function TIntegerCondition.IsGreaterOrEqual(aValue: Int64): IIntegerCondition;
- begin
- Result := Self.IsGreaterOrEqual(aValue,'');
- end;
- function TIntegerCondition.IsGreaterOrEqual(aValue: Int64; const aCustomMessage : string): IIntegerCondition;
- begin
- if fValue < aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be greather or equal to %d',[aValue]);
- end;
- Result := Self;
- end;
- function TIntegerCondition.IsGreaterThan(aValue: Int64): IIntegerCondition;
- begin
- Result := Self.IsGreaterThan(aValue,'');
- end;
- function TIntegerCondition.IsGreaterThan(aValue: Int64; const aCustomMessage : string): IIntegerCondition;
- begin
- if fValue <= aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be greather than %d',[aValue]);
- end;
- Result := Self;
- end;
- function TIntegerCondition.IsInRange(aMin, aMax: Int64): IIntegerCondition;
- begin
- Result := Self.IsInRange(aMin,aMax,'');
- end;
- function TIntegerCondition.IsInRange(aMin, aMax: Int64; const aCustomMessage : string): IIntegerCondition;
- begin
- if (fValue < aMin) or (fValue > aMax) then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentOutOfRangeException,aCustomMessage)
- else ThrowException('must be in %d-%d range',[aMin,aMax]);
- end;
- Result := Self;
- end;
- function TIntegerCondition.IsLessOrEqual(aValue: Int64): IIntegerCondition;
- begin
- Result := Self.IsLessOrEqual(aValue,'');
- end;
- function TIntegerCondition.IsLessOrEqual(aValue: Int64; const aCustomMessage : string): IIntegerCondition;
- begin
- if fValue > aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be less or equal to %d',[aValue]);
- end;
- Result := Self;
- end;
- function TIntegerCondition.IsLessThan(aValue: Int64): IIntegerCondition;
- begin
- Result := Self.IsLessThan(aValue,'');
- end;
- function TIntegerCondition.IsLessThan(aValue: Int64; const aCustomMessage : string): IIntegerCondition;
- begin
- if fValue >= aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be less than %d',[aValue]);
- end;
- Result := Self;
- end;
- function TIntegerCondition.IsNotEqualTo(aValue: Int64): IIntegerCondition;
- begin
- Result := Self.IsNotEqualTo(aValue,'');
- end;
- function TIntegerCondition.IsNotEqualTo(aValue: Int64; const aCustomMessage : string): IIntegerCondition;
- begin
- if fValue = aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not be equal to %d',[aValue]);
- end;
- Result := Self;
- end;
- function TIntegerCondition.IsNotGreaterOrEqual(aValue: Int64): IIntegerCondition;
- begin
- Result := Self.IsNotGreaterOrEqual(aValue,'');
- end;
- function TIntegerCondition.IsNotGreaterOrEqual(aValue: Int64; const aCustomMessage : string): IIntegerCondition;
- begin
- if fValue >= aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not be greater or equal to %d',[aValue]);
- end;
- Result := Self;
- end;
- function TIntegerCondition.IsNotGreaterThan(aValue: Int64): IIntegerCondition;
- begin
- Result := Self.IsNotGreaterThan(aValue,'');
- end;
- function TIntegerCondition.IsNotGreaterThan(aValue: Int64; const aCustomMessage : string): IIntegerCondition;
- begin
- if fValue > aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not be greater than %d',[aValue]);
- end;
- Result := Self;
- end;
- function TIntegerCondition.IsNotInRange(aMin, aMax: Int64): IIntegerCondition;
- begin
- Result := Self.IsNotInRange(aMin,aMax,'');
- end;
- function TIntegerCondition.IsNotInRange(aMin, aMax: Int64; const aCustomMessage : string): IIntegerCondition;
- begin
- if (fValue >= aMin) and (fValue <= aMax) then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentOutOfRangeException,aCustomMessage)
- else ThrowException('should not be in range %d-%d',[aMin,aMax]);
- end;
- Result := Self;
- end;
- function TIntegerCondition.IsNotLessOrEqual(aValue: Int64): IIntegerCondition;
- begin
- Result := Self.IsNotLessOrEqual(aValue,'');
- end;
- function TIntegerCondition.IsNotLessOrEqual(aValue: Int64; const aCustomMessage : string): IIntegerCondition;
- begin
- if fValue <= aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not be less or equal to %d',[aValue]);
- end;
- Result := Self;
- end;
- function TIntegerCondition.IsNotLessThan(aValue: Int64): IIntegerCondition;
- begin
- Result := Self.IsNotLessThan(aValue,'');
- end;
- function TIntegerCondition.IsNotLessThan(aValue: Int64; const aCustomMessage : string): IIntegerCondition;
- begin
- if fValue < aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not be less than %d',[aValue]);
- end;
- Result := Self;
- end;
- function TIntegerCondition.Evaluate(aExpression: Boolean): IIntegerCondition;
- begin
- Result := Self.Evaluate(aExpression,'');
- end;
- function TIntegerCondition.Evaluate(aExpression: Boolean; const aCustomMessage : string): IIntegerCondition;
- begin
- if not aExpression then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must meet condition');
- end;
- end;
- { TFloatCondition }
- constructor TFloatCondition.Create(const aValue: Extended; const aName : string; aPostCondition : Boolean);
- begin
- fName := aName;
- fValue := aValue;
- fPostCondition := aPostCondition;
- end;
- function TFloatCondition.WithExceptionOnFailure(aExceptionClass: ExceptClass): IFloatCondition;
- begin
- fExceptionClass := aExceptionClass;
- Result := Self;
- end;
- function TFloatCondition.IsEqualTo(aValue: Extended): IFloatCondition;
- begin
- Result := Self.IsEqualTo(aValue,'');
- end;
- function TFloatCondition.IsEqualTo(aValue: Extended; const aCustomMessage : string): IFloatCondition;
- begin
- if fValue <> aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be equal to %d',[aValue]);
- end;
- Result := Self;
- end;
- function TFloatCondition.IsGreaterOrEqual(aValue: Extended): IFloatCondition;
- begin
- Result := Self.IsGreaterOrEqual(aValue,'');
- end;
- function TFloatCondition.IsGreaterOrEqual(aValue: Extended; const aCustomMessage : string): IFloatCondition;
- begin
- if fValue < aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be greather or equal to %d',[aValue]);
- end;
- Result := Self;
- end;
- function TFloatCondition.IsGreaterThan(aValue: Extended): IFloatCondition;
- begin
- Result := Self.IsGreaterThan(aValue,'');
- end;
- function TFloatCondition.IsGreaterThan(aValue: Extended; const aCustomMessage : string): IFloatCondition;
- begin
- if fValue <= aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be greather than %d',[aValue]);
- end;
- Result := Self;
- end;
- function TFloatCondition.IsInRange(aMin, aMax: Extended): IFloatCondition;
- begin
- Result := Self.IsInRange(aMin,aMax,'');
- end;
- function TFloatCondition.IsInRange(aMin, aMax: Extended; const aCustomMessage : string): IFloatCondition;
- begin
- if (fValue < aMin) or (fValue > aMax) then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentOutOfRangeException,aCustomMessage)
- else ThrowException('must be in %d-%d range',[aMin,aMax]);
- end;
- Result := Self;
- end;
- function TFloatCondition.IsLessOrEqual(aValue: Extended): IFloatCondition;
- begin
- Result := Self.IsLessOrEqual(aValue,'');
- end;
- function TFloatCondition.IsLessOrEqual(aValue: Extended; const aCustomMessage : string): IFloatCondition;
- begin
- if fValue > aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be less or equal to %d',[aValue]);
- end;
- Result := Self;
- end;
- function TFloatCondition.IsLessThan(aValue: Extended): IFloatCondition;
- begin
- Result := Self.IsLessThan(aValue,'');
- end;
- function TFloatCondition.IsLessThan(aValue: Extended; const aCustomMessage : string): IFloatCondition;
- begin
- if fValue >= aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be less than %d',[aValue]);
- end;
- Result := Self;
- end;
- function TFloatCondition.IsNotEqualTo(aValue: Extended): IFloatCondition;
- begin
- Result := Self.IsNotEqualTo(aValue,'');
- end;
- function TFloatCondition.IsNotEqualTo(aValue: Extended; const aCustomMessage : string): IFloatCondition;
- begin
- if fValue = aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not be equal to %d',[aValue]);
- end;
- Result := Self;
- end;
- function TFloatCondition.IsNotGreaterOrEqual(aValue: Extended): IFloatCondition;
- begin
- Result := Self.IsNotGreaterOrEqual(aValue,'');
- end;
- function TFloatCondition.IsNotGreaterOrEqual(aValue: Extended; const aCustomMessage : string): IFloatCondition;
- begin
- if fValue >= aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not be greater or equal to %d',[aValue]);
- end;
- Result := Self;
- end;
- function TFloatCondition.IsNotGreaterThan(aValue: Extended): IFloatCondition;
- begin
- Result := Self.IsNotGreaterThan(aValue,'');
- end;
- function TFloatCondition.IsNotGreaterThan(aValue: Extended; const aCustomMessage : string): IFloatCondition;
- begin
- if fValue > aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not be greater than %d',[aValue]);
- end;
- Result := Self;
- end;
- function TFloatCondition.IsNotInRange(aMin, aMax: Extended): IFloatCondition;
- begin
- Result := Self.IsNotInRange(aMin,aMax,'');
- end;
- function TFloatCondition.IsNotInRange(aMin, aMax: Extended; const aCustomMessage : string): IFloatCondition;
- begin
- if (fValue >= aMin) and (fValue <= aMax) then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentOutOfRangeException,aCustomMessage)
- else ThrowException('should not be in range %d-%d',[aMin,aMax]);
- end;
- Result := Self;
- end;
- function TFloatCondition.IsNotLessOrEqual(aValue: Extended): IFloatCondition;
- begin
- Result := Self.IsNotLessOrEqual(aValue,'');
- end;
- function TFloatCondition.IsNotLessOrEqual(aValue: Extended; const aCustomMessage : string): IFloatCondition;
- begin
- if fValue <= aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not be less or equal to %d',[aValue]);
- end;
- Result := Self;
- end;
- function TFloatCondition.IsNotLessThan(aValue: Extended): IFloatCondition;
- begin
- Result := Self.IsNotLessThan(aValue,'');
- end;
- function TFloatCondition.IsNotLessThan(aValue: Extended; const aCustomMessage : string): IFloatCondition;
- begin
- if fValue < aValue then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not be less than %d',[aValue]);
- end;
- Result := Self;
- end;
- function TFloatCondition.Evaluate(aExpression: Boolean): IFloatCondition;
- begin
- Result := Self.Evaluate(aExpression,'');
- end;
- function TFloatCondition.Evaluate(aExpression: Boolean; const aCustomMessage : string): IFloatCondition;
- begin
- if not aExpression then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must meet condition');
- end;
- end;
- { TObjectCondition }
- constructor TObjectCondition.Create(const aValue: TObject; const aName: string; aPostCondition : Boolean);
- begin
- fName := aName;
- fValue := aValue;
- fPostCondition := aPostCondition;
- end;
- function TObjectCondition.WithExceptionOnFailure(aExceptionClass: ExceptClass): IObjectCondition;
- begin
- fExceptionClass := aExceptionClass;
- Result := Self;
- end;
- function TObjectCondition.IsNull: IObjectCondition;
- begin
- Result := Self.IsNull('');
- end;
- function TObjectCondition.IsNull(const aCustomMessage: string): IObjectCondition;
- begin
- if fValue <> nil then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentNilException,aCustomMessage)
- else ThrowException('must be null');
- end;
- Result := Self;
- end;
- function TObjectCondition.IsNotNull: IObjectCondition;
- begin
- Result := Self.IsNotNull('');
- end;
- function TObjectCondition.IsNotNull(const aCustomMessage: string): IObjectCondition;
- begin
- if fValue = nil then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentNilException,aCustomMessage)
- else ThrowException('should not be null');
- end;
- Result := Self;
- end;
- function TObjectCondition.IsOfType(aClass: TClass): IObjectCondition;
- begin
- Result := Self.IsOfType(aClass,'');
- end;
- function TObjectCondition.IsOfType(aClass: TClass; const aCustomMessage: string): IObjectCondition;
- begin
- if not(fValue is aClass) then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must be of type "%s"',[aClass.ClassName]);
- end;
- Result := Self;
- end;
- function TObjectCondition.DoesNotOfType(aClass: TClass): IObjectCondition;
- begin
- Result := Self.DoesNotOfType(aClass,'');
- end;
- function TObjectCondition.DoesNotOfType(aClass: TClass; const aCustomMessage: string): IObjectCondition;
- begin
- if fValue is aClass then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('should not be of type "%s"',[aClass.ClassName]);
- end;
- Result := Self;
- end;
- function TObjectCondition.Evaluate(aExpression: Boolean): IObjectCondition;
- begin
- Result := Self.Evaluate(aExpression,'');
- end;
- function TObjectCondition.Evaluate(aExpression: Boolean; const aCustomMessage: string): IObjectCondition;
- begin
- if not aExpression then
- begin
- if not aCustomMessage.IsEmpty then ThrowException(EArgumentException,aCustomMessage)
- else ThrowException('must meet condition');
- end;
- Result := Self;
- end;
- { TCondition }
- constructor TCondition.Create;
- begin
- fName := '';
- fExceptionClass := nil;
- fPostCondition := False;
- end;
- procedure TCondition.ThrowException(const aMsg: string);
- var
- rexception : ExceptClass;
- begin
- if fExceptionClass <> nil then raise fExceptionClass.Create(aMsg)
- else
- begin
- if fPostCondition then rexception := EPostConditionError
- else rexception := EPreConditionError;
- if fName.IsEmpty then raise rexception.Create(aMsg)
- else raise rexception.CreateFmt('[%s] %s',[fName,aMsg]);
- end;
- end;
- procedure TCondition.ThrowException(const aMsg: string; aValues: array of const);
- begin
- if fExceptionClass <> nil then raise fExceptionClass.Create(aMsg)
- else ThrowException(Format(aMsg,aValues));
- end;
- procedure TCondition.ThrowException(aExceptionClass : ExceptClass; const aMsg : string);
- begin
- if fExceptionClass <> nil then raise fExceptionClass.Create(aMsg)
- else raise aExceptionClass.Create(aMsg);
- end;
- end.
|