unit ProjGatherProperties;

interface

uses
  Classes, ZhAPI;

type
  TProjGatherProperty = class
  private
    FName: string;
    FValue: string;
  public
    constructor Create(const AName: string);
    destructor Destroy; override;

    property Name: string read FName write FName;
    property Value: string read FValue write FValue;
  end;

  TProjGatherProperties = class
  private
    FList: TList;

    function FindProjGatherProperty(const AName: string): TProjGatherProperty;
    function AddProjGatherProperty(const AName: string): TProjGatherProperty;

    function GetCount: Integer;
    function GetPgPropperty(AIndex: Integer): TProjGatherProperty;
  public
    constructor Create;
    destructor Destroy; override;

    function GetProjGatherProperty(const AName: string): TProjGatherProperty;

    property Count: Integer read GetCount;
    property PgProperty[AIndex: Integer]: TProjGatherProperty read GetPgPropperty;
  end;

implementation

uses SysUtils;

{ TProjGatherProperty }

constructor TProjGatherProperty.Create(const AName: string);
begin
  FName := AName;
end;

destructor TProjGatherProperty.Destroy;
begin
  inherited;
end;

{ TProjGatherProperties }

function TProjGatherProperties.AddProjGatherProperty(
  const AName: string): TProjGatherProperty;
begin
  Result := TProjGatherProperty.Create(AName);
  FList.Add(Result);
end;

constructor TProjGatherProperties.Create;
begin
  FList := TList.Create;
end;

destructor TProjGatherProperties.Destroy;
begin
  ClearObjects(FList);
  FList.Free;
  inherited;
end;

function TProjGatherProperties.FindProjGatherProperty(
  const AName: string): TProjGatherProperty;
var
  i: Integer;
  vProperty: TProjGatherProperty;
begin
  Result := nil;
  for i := 0 to Count - 1 do
  begin
    vProperty := PgProperty[i];
    if vProperty.Name = AName then
    begin
      Result := vProperty;
      Break;
    end;
  end;
end;

function TProjGatherProperties.GetCount: Integer;
begin
  Result := FList.Count;
end;

function TProjGatherProperties.GetPgPropperty(
  AIndex: Integer): TProjGatherProperty;
begin
  Result := TProjGatherProperty(FList.Items[AIndex]);
end;

function TProjGatherProperties.GetProjGatherProperty(
  const AName: string): TProjGatherProperty;
begin
  Result := FindProjGatherProperty(AName);
  if not Assigned(Result) then
    Result := AddProjGatherProperty(AName);
end;

end.
