Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/ItemList_T.cs @ 51

Last change on this file since 51 was 41, checked in by swagner, 17 years ago

Worked on ticket #41

  • moved specific classes ItemList and ItemListView into separate files
File size: 4.8 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Text;
26using System.Xml;
27using HeuristicLab.Core;
28
29namespace HeuristicLab.Data {
[40]30  public class ItemList<T> : ItemBase, IList<T> where T : IItem {
31    private List<T> list;
[2]32
33    public ItemList() {
[40]34      list = new List<T>();
[2]35    }
36
37    public override IView CreateView() {
[40]38      return new ItemListView<T>(this);
[2]39    }
40
41    public override object Clone(IDictionary<Guid, object> clonedObjects) {
[40]42      ItemList<T> clone = new ItemList<T>();
[2]43      clonedObjects.Add(Guid, clone);
44      for (int i = 0; i < list.Count; i++)
[40]45        clone.list.Add((T)Auxiliary.Clone(list[i], clonedObjects));
[2]46      return clone;
47    }
48
49    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
50      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
51      for (int i = 0; i < list.Count; i++)
52        node.AppendChild(PersistenceManager.Persist(list[i], document, persistedObjects));
53      return node;
54    }
55    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
56      base.Populate(node, restoredObjects);
57      for (int i = 0; i < node.ChildNodes.Count; i++)
[40]58        list.Add((T)PersistenceManager.Restore(node.ChildNodes[i], restoredObjects));
[2]59    }
60
61    public override string ToString() {
62      if (list.Count > 0) {
63        StringBuilder builder = new StringBuilder();
64        builder.Append(list[0].ToString());
65        for (int i = 1; i < list.Count; i++) {
66          builder.Append(";");
67          builder.Append(list[i].ToString());
68        }
69        return builder.ToString();
70      } else {
71        return "Empty List";
72      }
73    }
74
[40]75    #region IList<T> Members
76    public int IndexOf(T item) {
[2]77      return list.IndexOf(item);
78    }
[40]79    public void Insert(int index, T item) {
[2]80      list.Insert(index, item);
81      OnItemAdded(item, index);
82    }
83    public void RemoveAt(int index) {
84      IItem item = list[index];
85      list.RemoveAt(index);
86      OnItemRemoved(item, index);
87    }
[40]88    public T this[int index] {
[2]89      get { return list[index]; }
[40]90      set { list[index] = value; }
[2]91    }
92    #endregion
93
[40]94    #region ICollection<T> Members
95    public void Add(T item) {
[2]96      list.Add(item);
97      OnItemAdded(item, list.Count - 1);
98    }
99    public void Clear() {
100      list.Clear();
101      OnCleared();
102    }
[40]103    public bool Contains(T item) {
[2]104      return list.Contains(item);
105    }
[40]106    public void CopyTo(T[] array, int arrayIndex) {
[2]107      list.CopyTo(array, arrayIndex);
108    }
109    public int Count {
110      get { return list.Count; }
111    }
112    public bool IsReadOnly {
113      get { return false; }
114    }
[40]115    public bool Remove(T item) {
[2]116      int index = list.IndexOf(item);
117      if (list.Remove(item)) {
118        OnItemRemoved(item, index);
119        return true;
120      } else {
121        return false;
122      }
123    }
124    #endregion
125
[40]126    #region IEnumerable<T> Members
127    public IEnumerator<T> GetEnumerator() {
[2]128      return list.GetEnumerator();
129    }
130    #endregion
131
132    #region IEnumerable Members
133    IEnumerator IEnumerable.GetEnumerator() {
134      return list.GetEnumerator();
135    }
136    #endregion
137
138    public event EventHandler<ItemIndexEventArgs> ItemAdded;
139    protected virtual void OnItemAdded(IItem item, int index) {
140      if (ItemAdded != null)
141        ItemAdded(this, new ItemIndexEventArgs(item, index));
142      OnChanged();
143    }
144    public event EventHandler<ItemIndexEventArgs> ItemRemoved;
145    protected virtual void OnItemRemoved(IItem item, int index) {
146      if (ItemRemoved != null)
147        ItemRemoved(this, new ItemIndexEventArgs(item, index));
148      OnChanged();
149    }
150    public event EventHandler Cleared;
151    protected virtual void OnCleared() {
152      if (Cleared != null)
153        Cleared(this, new EventArgs());
154      OnChanged();
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.