unit FilenameBuilder;
interface
uses
Classes,
Generics.Collections,
SysUtils;
type
TMP3Meta =
record
Interpret :
string;
Titel :
string;
Album :
string;
CdNr : Integer;
TrackNr : Integer;
end;
IFilenamePart =
interface
['
{281FA130-B641-454D-8E9A-58D0C02F4C14}']
function Build(
const AData : TMP3Meta ) :
string;
end;
TFilenamePartFactory =
class
private
FParts : TDictionary<
string, IFilenamePart>;
public
constructor Create;
destructor Destroy;
override;
function GetPart(
const AKey :
string ) : IFilenamePart;
end;
TFilenameBuilder =
class
private
FParts : TList<IFilenamePart>;
public
constructor Create;
destructor Destroy;
override;
procedure AddPart( APart : IFilenamePart );
procedure Clear;
function GetFilename(
const AData : TMP3Meta ) :
string;
end;
implementation
type
TFilenamePart =
class( TInterfacedObject, IFilenamePart )
protected
function Build(
const AData : TMP3Meta ) :
string;
virtual;
abstract;
end;
TInterpretPart =
class( TFilenamePart )
protected
function Build(
const AData : TMP3Meta ) :
string;
override;
end;
TTitelPart =
class( TFilenamePart )
protected
function Build(
const AData : TMP3Meta ) :
string;
override;
end;
TAlbumPart =
class( TFilenamePart )
protected
function Build(
const AData : TMP3Meta ) :
string;
override;
end;
TCdNrPart =
class( TFilenamePart )
protected
function Build(
const AData : TMP3Meta ) :
string;
override;
end;
TTrackNrPart =
class( TFilenamePart )
protected
function Build(
const AData : TMP3Meta ) :
string;
override;
end;
{ TFilenameBuilder }
procedure TFilenameBuilder.AddPart( APart : IFilenamePart );
begin
FParts.Add( APart );
end;
procedure TFilenameBuilder.Clear;
begin
FParts.Clear;
end;
constructor TFilenameBuilder.Create;
begin
inherited Create;
FParts := TList<IFilenamePart>.Create;
end;
destructor TFilenameBuilder.Destroy;
begin
FParts.Free;
inherited;
end;
function TFilenameBuilder.GetFilename(
const AData : TMP3Meta ) :
string;
var
LBuilder : TStringBuilder;
LIdx : Integer;
begin
LBuilder := TStringBuilder.Create;
try
for LIdx := 0
to FParts.Count - 1
do
begin
if LIdx > 0
then
LBuilder.Append( '
-' );
LBuilder.Append( FParts[LIdx].Build( AData ) );
end;
Result := LBuilder.ToString;
finally
LBuilder.Free;
end;
end;
{ TInterpretPart }
function TInterpretPart.Build(
const AData : TMP3Meta ) :
string;
begin
Result := AData.Interpret;
end;
{ TTitelPart }
function TTitelPart.Build(
const AData : TMP3Meta ) :
string;
begin
Result := AData.Titel;
end;
{ TAlbumPart }
function TAlbumPart.Build(
const AData : TMP3Meta ) :
string;
begin
Result := AData.Album;
end;
{ TCdNrPart }
function TCdNrPart.Build(
const AData : TMP3Meta ) :
string;
begin
Result := IntToStr( AData.CdNr );
end;
{ TTrackNrPart }
function TTrackNrPart.Build(
const AData : TMP3Meta ) :
string;
begin
Result := IntToStr( AData.TrackNr );
end;
{ TFilenamePartFactory }
constructor TFilenamePartFactory.Create;
begin
inherited Create;
FParts := TDictionary<
string, IFilenamePart>.Create;
FParts.Add( '
Interpret', TInterpretPart.Create );
FParts.Add( '
Titel', TTitelPart.Create );
FParts.Add( '
Album', TAlbumPart.Create );
FParts.Add( '
CdNr', TCdNrPart.Create );
FParts.Add( '
TrackNr', TTrackNrPart.Create );
end;
destructor TFilenamePartFactory.Destroy;
begin
FParts.Free;
inherited;
end;
function TFilenamePartFactory.GetPart(
const AKey :
string ) : IFilenamePart;
begin
Result := FParts[AKey];
end;
end.