unit ComparerFun;
interface
uses
System.SysUtils,
System.Generics.Defaults;
type
TInverseComparer<T> =
class( TInterfacedObject, IComparer<T> )
private
FComparer: IComparer<T>;
function Compare(
const Left, Right: T ): Integer;
constructor Create( AComparer: IComparer<T> );
public
class function Construct( AComparer: IComparer<T> ): IComparer<T>;
end;
TConsoleLogStringComparer =
class( TInterfacedObject, IComparer<
string> )
private
FComparer: IComparer<
string>;
function Compare(
const Left, Right:
string ): Integer;
constructor Create( AComparer: IComparer<
string> );
public
class function Construct( AComparer: IComparer<
string> ): IComparer<
string>;
end;
implementation
{ TInverseComparer<T> }
function TInverseComparer<T>.Compare(
const Left, Right: T ): Integer;
begin
Result := -FComparer.Compare( Left, Right );
end;
class function TInverseComparer<T>.Construct( AComparer: IComparer<T> ): IComparer<T>;
begin
Result := Self.Create( AComparer );
end;
constructor TInverseComparer<T>.Create( AComparer: IComparer<T> );
begin
Assert( Assigned( AComparer ) );
inherited Create;
FComparer := AComparer;
end;
{ TConsoleLogStringComparer }
function TConsoleLogStringComparer.Compare(
const Left, Right:
string ): Integer;
begin
Result := FComparer.Compare( Left, Right );
WriteLn( Format( '
Compare( "%s", "%s" ) = %d', [Left, Right, Result] ) );
end;
class function TConsoleLogStringComparer.Construct( AComparer: IComparer<
string> ): IComparer<
string>;
begin
Result := Self.Create( AComparer );
end;
constructor TConsoleLogStringComparer.Create( AComparer: IComparer<
string> );
begin
Assert( Assigned( AComparer ) );
// Ein simpler Guard
inherited Create;
FComparer := AComparer;
end;
end.