Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlReaderWriterBranch/HeuristicLab.Data/ItemList_T.cs @ 253

Last change on this file since 253 was 125, checked in by gkronber, 17 years ago

created a branch that combines the XmlReader and XmlWriter branches

File size: 7.9 KB
Line 
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 {
30  public class ItemList<T> : ItemBase, IList<T> where T : IItem {
31    private List<T> list;
32
33    public ItemList() {
34      list = new List<T>();
35    }
36
37    public override IView CreateView() {
38      return new ItemListView<T>(this);
39    }
40
41    public override object Clone(IDictionary<Guid, object> clonedObjects) {
42      ItemList<T> clone = new ItemList<T>();
43      clonedObjects.Add(Guid, clone);
44      CloneElements(clone, clonedObjects);
45      return clone;
46    }
47    protected void CloneElements(ItemList<T> destination, IDictionary<Guid, object> clonedObjects) {
48      for (int i = 0; i < list.Count; i++)
49        destination.list.Add((T) Auxiliary.Clone(list[i], clonedObjects));
50    }
51
52    //public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
53    //  XmlNode node = base.GetXmlNode(name, document, persistedObjects);
54    //  for (int i = 0; i < list.Count; i++)
55    //    node.AppendChild(PersistenceManager.Persist(list[i], document, persistedObjects));
56    //  return node;
57    //}
58    public override void Persist(string name, XmlWriter writer, IDictionary<Guid, IStorable> persistedObjects) {
59      base.Persist(name, writer, persistedObjects);
60      for(int i = 0; i < list.Count; i++)
61        PersistenceManager.Persist(list[i], writer, persistedObjects);
62    }
63    //public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
64    //  base.Populate(node, restoredObjects);
65    //  for(int i = 0; i < node.ChildNodes.Count; i++)
66    //    list.Add((T)PersistenceManager.Restore(node.ChildNodes[i], restoredObjects));
67    //}
68    public override void Populate(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
69      base.Populate(reader, restoredObjects);
70      if(!reader.IsEmptyElement) {
71        reader.Read();
72        while(reader.IsStartElement()) {
73          list.Add((T)PersistenceManager.Restore(reader, restoredObjects));
74          reader.Skip();
75        }
76        reader.ReadEndElement();
77      } else {
78        reader.Read();
79      }
80    }
81
82    public override string ToString() {
83      if (list.Count > 0) {
84        StringBuilder builder = new StringBuilder();
85        builder.Append(list[0].ToString());
86        for (int i = 1; i < list.Count; i++) {
87          builder.Append(";");
88          builder.Append(list[i].ToString());
89        }
90        return builder.ToString();
91      } else {
92        return "Empty List";
93      }
94    }
95
96    #region IList<T> Members
97    public int IndexOf(T item) {
98      return list.IndexOf(item);
99    }
100    public void Insert(int index, T item) {
101      list.Insert(index, item);
102      OnItemAdded(item, index);
103    }
104    public void RemoveAt(int index) {
105      IItem item = list[index];
106      list.RemoveAt(index);
107      OnItemRemoved(item, index);
108    }
109    public T this[int index] {
110      get { return list[index]; }
111      set { list[index] = value; }
112    }
113    #endregion
114
115    #region ICollection<T> Members
116    public void Add(T item) {
117      list.Add(item);
118      OnItemAdded(item, list.Count - 1);
119    }
120    public void Clear() {
121      list.Clear();
122      OnCleared();
123    }
124    public bool Contains(T item) {
125      return list.Contains(item);
126    }
127    public void CopyTo(T[] array, int arrayIndex) {
128      list.CopyTo(array, arrayIndex);
129    }
130    public int Count {
131      get { return list.Count; }
132    }
133    public bool IsReadOnly {
134      get { return false; }
135    }
136    public bool Remove(T item) {
137      int index = list.IndexOf(item);
138      if (list.Remove(item)) {
139        OnItemRemoved(item, index);
140        return true;
141      } else {
142        return false;
143      }
144    }
145    #endregion
146
147    #region IEnumerable<T> Members
148    public IEnumerator<T> GetEnumerator() {
149      return list.GetEnumerator();
150    }
151    #endregion
152
153    #region IEnumerable Members
154    IEnumerator IEnumerable.GetEnumerator() {
155      return list.GetEnumerator();
156    }
157    #endregion
158
159    #region List<T> Methods
160    public int LastIndexOf(T item) {
161      return list.LastIndexOf(item);
162    }
163
164    public int LastIndexOf(T item, int index) {
165      return list.LastIndexOf(item, index);
166    }
167
168    public int LastIndexOf(T item, int index, int count) {
169      return list.LastIndexOf(item, index, count);
170    }
171
172    public int IndexOf(T item, int index) {
173      return list.IndexOf(item, index);
174    }
175
176    public int IndexOf(T item, int index, int count) {
177      return list.IndexOf(item, index, count);
178    }
179
180    public void AddRange(IEnumerable<T> collection) {
181      foreach (T obj in collection) {
182        this.Add(obj);
183      }
184    }
185
186    public bool Exists(Predicate<T> match) {
187      return list.Exists(match);
188    }
189
190    public int BinarySearch(T item) {
191      return list.BinarySearch(item);
192    }
193
194    public int BinarySearch(T item, IComparer<T> comparer) {
195      return list.BinarySearch(item, comparer);
196    }
197
198    public int BinarySearch(int index, int count, T item, IComparer<T> comparer) {
199      return list.BinarySearch(index, count, item, comparer);
200    }
201
202    public T Find(Predicate<T> match) {
203      return list.Find(match);
204    }
205
206    public List<T> FindAll(Predicate<T> match) {
207      return list.FindAll(match);
208    }
209
210    public int FindIndex(Predicate<T> match) {
211      return list.FindIndex(match);
212    }
213
214    public T FindLast(Predicate<T> match) {
215      return list.FindLast(match);
216    }
217
218    public int FindLastIndex(Predicate<T> match) {
219      return list.FindLastIndex(match);
220    }
221
222    public void Sort() {
223      list.Sort();
224    }
225
226    public void Sort(IComparer<T> comparer) {
227      list.Sort(comparer);
228    }
229
230    public void Sort(Comparison<T> comparison) {
231      list.Sort(comparison);
232    }
233
234    public void Reverse() {
235      list.Reverse();
236    }
237
238    public ItemList<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter) where TOutput : IItem {
239      ItemList<TOutput> targetList = new ItemList<TOutput>();
240      foreach (T item in list) {
241        targetList.Add(converter.Invoke(item));
242      }
243      return targetList;
244    }
245
246    public bool TrueForAll(Predicate<T> match) {
247      return list.TrueForAll(match);
248    }
249
250    #endregion
251
252    public event EventHandler<ItemIndexEventArgs> ItemAdded;
253    protected virtual void OnItemAdded(IItem item, int index) {
254      if (ItemAdded != null)
255        ItemAdded(this, new ItemIndexEventArgs(item, index));
256      OnChanged();
257    }
258    public event EventHandler<ItemIndexEventArgs> ItemRemoved;
259    protected virtual void OnItemRemoved(IItem item, int index) {
260      if (ItemRemoved != null)
261        ItemRemoved(this, new ItemIndexEventArgs(item, index));
262      OnChanged();
263    }
264    public event EventHandler Cleared;
265    protected virtual void OnCleared() {
266      if (Cleared != null)
267        Cleared(this, new EventArgs());
268      OnChanged();
269    }
270  }
271}
Note: See TracBrowser for help on using the repository browser.