Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlTextWriterBranch/HeuristicLab.Data/ItemList_T.cs @ 137

Last change on this file since 137 was 119, checked in by gkronber, 17 years ago

created a branch that uses XmlTextWriter instead of XMLDocument to save documents. Investigating ticket #103.

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