Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.Data/ItemList_T.cs @ 5698

Last change on this file since 5698 was 69, checked in by mkofler, 17 years ago

Ticket #51: Added ItemList wrapper for ConvertAll and a few other methods

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