unit class_basics;

interface

uses Contnrs;


{$REGION 'КЛАСС "Cущность'}
type
  TRD_Something = class (TObject)
    private
      f_name: string;           //Имя
      f_description: string;    //описание

    public
      function Get_name: string;
      function Get_description: string;

      constructor Create ( in_name, in_desc: string); virtual;
      destructor Destroy; override;
  end;
{$ENDREGION 'КЛАСС "Cущность'}


{$REGION 'КЛАСС "Массив Сущностей"'}
type
  TRD_List = class (TObjectList)
    private
      current_item: TRD_Something;
    public
      function Find_by_name( in_object_name: string ): TRD_Something; overload;
      function Index_by_name( in_object_name: string ): integer;
      function Find_by_index ( in_index: integer): TRD_Something; overload;
      function Next_item: TRD_Something; overload;
      function Prev_item: TRD_Something; overload;
      function First_item: TRD_Something; overload;
      function Last_item: TRD_Something; overload;
      function Get_current_item: TRD_Something; overload;
    published
       function set_current_item (in_item_index: integer): boolean;

end;
{$ENDREGION}

implementation

{ TRD_Something }

{$REGION 'КЛАСС "СУЩНОСТЬ" РЕАЛИЗАЦИЯ'}
constructor TRD_Something.Create(in_name, in_desc: string);
begin
  inherited Create;
Assert( ( in_name <> '' ),
        'Входное поле ИМЯ СУЩНОСТИ класса RDSomething пустое!'+'{01A9B53C-F9C2-4501-9C17-23C7B7A1C51D}');

  self.f_name := in_name;
  self.f_description := in_desc;
end;

destructor TRD_Something.Destroy;
begin
  self.f_name := '';
  self.f_description := '';
  inherited;
end;

function TRD_Something.Get_description: string;
begin
   result := self.f_description;
end;

function TRD_Something.Get_name: string;
begin
 Assert( ( self.f_name <> ''),
          'Поле ИМЯ СУЩНОСТИ класса RDSomething пустое!'+'{74FAF375-02FB-4916-972B-607F52A944C0}');
   result := self.f_name;
end;
{$ENDREGION}

{ TRD_List }

{$REGION 'КЛАСС "Массив Сущностей" РЕАЛИЗАЦИЯ'}
function TRD_List.Find_by_index(in_index: integer): TRD_Something;
var i: integer;
begin
result := nil;
if ( ( in_index > 0 ) and ( in_index < self.Count ) ) then
  begin
    result := self[ i ] as TRD_Something;
  end;
end;

function TRD_List.Find_by_name( in_object_name: string): TRD_Something;
var i: integer;
begin
result := nil;
if ( in_object_name <> '' ) then
  begin
    for i := 0 to self.Count - 1 do
    begin
      if ( ( self[ i ] as TRD_Something ).Get_name = in_object_name ) then
      begin
        result := self[ i ] as TRD_Something;
      end;
    end;
  end;
end;

function TRD_List.First_item: TRD_Something;
begin
  self.current_item := self.First as TRD_Something;
  result:=self.First as TRD_Something;
end;

function TRD_List.Get_current_item: TRD_Something;
begin
  result := self.current_item;
end;

function TRD_List.Index_by_name(in_object_name: string): integer;
var i: integer;
begin
result := -1;
if ( in_object_name <> '' ) then
  begin
    for i := 0 to self.Count - 1 do
    begin
      if ( ( self[ i ] as TRD_Something ).Get_name = in_object_name ) then
      begin
        result := i;
      end;
    end;
  end;
end;

function TRD_List.Last_item: TRD_Something;
begin
  self.current_item := self.Last as TRD_Something;
  result := self.Last as TRD_Something;
end;

function TRD_List.Next_item: TRD_Something;
begin
  result:=nil;
  if ( ( Index_by_name( self.current_item.Get_name ) + 1 ) <=  self.Count - 1) then
  begin
    self.current_item := self[ Index_by_name( self.current_item.Get_name ) + 1 ] as TRD_Something;
    result := self[ Index_by_name( self.current_item.Get_name ) + 1 ] as TRD_Something;
  end;
end;

function TRD_List.Prev_item: TRD_Something;
begin
  result:=nil;
  if ( ( Index_by_name( self.current_item.Get_name ) + 1 ) >=  0 ) then
  begin
    self.current_item := self[ Index_by_name( self.current_item.Get_name ) - 1 ] as TRD_Something;
    result := self[ Index_by_name( self.current_item.Get_name ) - 1 ] as TRD_Something;
  end;
end;

function TRD_List.set_current_item(in_item_index: integer): boolean;
begin
  result := FALSE;
  try
    self.current_item := self [ in_item_index ] as TRD_Something;
  finally
    result := TRUE;
  end;
end;

{$ENDREGION}



end.
