类声明部分:
TDMSTrains = class(TList)
private
FHashed: Boolean;
FHashList: TFpHashList;
FOwnsObjects: Boolean;
FSorted: Boolean;
FUpdateLevel: Integer;
protected
function GetItem(Index: Integer): TDMSTrain;
procedure Notify(Ptr: Pointer; Action: TListNotification); override;
procedure SetItem(Index: Integer; AObject: TDMSTrain);
public
constructor Create; overload;
constructor Create(AOwnsObjects: Boolean;Hashed:Boolean;AutoSorted:boolean);
overload;
destructor Destroy; override;
function Add(AObject: TDMSTrain): Integer;
procedure BeginUpdate;
procedure EndUpdate;
function Extract(Item: TDMSTrain): TDMSTrain;
function Find(const ATrainId:string): TDMSTrain;
function First: TDMSTrain;
function IndexOf(AObject: TDMSTrain): Integer;
procedure Insert(Index: Integer; AObject: TDMSTrain);
function Last: TDMSTrain;
function Remove(AObject: TDMSTrain): Integer;
property Items[Index: Integer]: TDMSTrain read GetItem write SetItem;
default;
property OwnsObjects: Boolean read FOwnsObjects write FOwnsObjects;
property UpdateLevel: Integer read FUpdateLevel;
end;
实现部分:
{
********************************** TDMSTrains **********************************
}
constructor TDMSTrains.Create;
begin
inherited Create;
FOwnsObjects := True;
FHashList:=TFpHashList.Create;
FHashed:=True;
FSorted:=True;
end;
constructor TDMSTrains.Create(AOwnsObjects: Boolean;Hashed:Boolean;
AutoSorted:boolean);
begin
inherited Create;
FHashList:=TFpHashList.Create;
FOwnsObjects := AOwnsObjects;
FHashed:=Hashed;
FSorted:=AutoSorted;
end;
destructor TDMSTrains.Destroy;
begin
inherited;
FHashList.Free;
end;
function TDMSTrains.Add(AObject: TDMSTrain): Integer;
begin
Result := inherited Add(AObject);
end;
procedure TDMSTrains.BeginUpdate;
begin
Inc(FUpdateLevel);
end;
procedure TDMSTrains.EndUpdate;
begin
if FUpdateLevel>0 then
Dec(FUpdateLevel);
if (FUpdateLevel=0) and FSorted then
Sort(SortTrainBy_PSTLJ_TRAINTYPE_ASC);
end;
function TDMSTrains.Extract(Item: TDMSTrain): TDMSTrain;
begin
Result := TDMSTrain(inherited Extract(Item));
end;
function TDMSTrains.Find(const ATrainId:string): TDMSTrain;
begin
Result:=TDMSTrain(FHashList.Find(ATrainid));
end;
function TDMSTrains.First: TDMSTrain;
begin
Result := TDMSTrain(inherited First);
end;
function TDMSTrains.GetItem(Index: Integer): TDMSTrain;
begin
Result := inherited Items[Index];
end;
function TDMSTrains.IndexOf(AObject: TDMSTrain): Integer;
begin
Result := inherited IndexOf(AObject);
end;
procedure TDMSTrains.Insert(Index: Integer; AObject: TDMSTrain);
begin
inherited Insert(Index, AObject);
end;
function TDMSTrains.Last: TDMSTrain;
begin
Result := TDMSTrain(inherited Last);
end;
procedure TDMSTrains.Notify(Ptr: Pointer; Action: TListNotification);
var
T: Integer;
begin
if FHashed then
begin
if (Action=lnDeleted)then
begin
T:=FHashList.FindIndexOf(TDMSTrain(Ptr).name);
if T<>-1 then
FHashList.Delete(T);
if OwnsObjects then
TDMSTrain(Ptr).Free;
end else if (Action=lnAdded) then
begin
FHashList.Add(TDMSTrain(Ptr).name,Ptr);
if (FUpdateLevel=0) and FSorted then
Sort(SortTrainBy_PSTLJ_TRAINTYPE_ASC);
end;
end;
inherited Notify(Ptr, Action);
end;
function TDMSTrains.Remove(AObject: TDMSTrain): Integer;
begin
Result := inherited Remove(AObject);
end;
procedure TDMSTrains.SetItem(Index: Integer; AObject: TDMSTrain);
begin
inherited Items[Index] := AObject;
end;