Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 68 was 68, checked in by swagner, 16 years ago

Worked on ticket #58

  • added patch of pspitzli
File size: 5.7 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 void AddRange(IEnumerable<T> collection) {
143      foreach (T obj in collection) {
144        this.Add(obj);
145      }
146    }
147
148    public bool Exists(Predicate<T> match) {
149      return list.Exists(match);
150    }
151
152    public T Find(Predicate<T> match) {
153      return list.Find(match);
154    }
155
156    public List<T> FindAll(Predicate<T> match) {
157      return list.FindAll(match);
158    }
159
160    public int FindIndex(Predicate<T> match) {
161      return list.FindIndex(match);
162    }
163
164    public void Sort() {
165      list.Sort();
166    }
167
168    public void Sort(IComparer<T> comparer) {
169      list.Sort(comparer);
170    }
171
172    public void Sort(Comparison<T> comparison) {
173      list.Sort(comparison);
174    }   
175
176    public void Reverse() {
177     list.Reverse();
178    }
179    #endregion
180
181    public event EventHandler<ItemIndexEventArgs> ItemAdded;
182    protected virtual void OnItemAdded(IItem item, int index) {
183      if (ItemAdded != null)
184        ItemAdded(this, new ItemIndexEventArgs(item, index));
185      OnChanged();
186    }
187    public event EventHandler<ItemIndexEventArgs> ItemRemoved;
188    protected virtual void OnItemRemoved(IItem item, int index) {
189      if (ItemRemoved != null)
190        ItemRemoved(this, new ItemIndexEventArgs(item, index));
191      OnChanged();
192    }
193    public event EventHandler Cleared;
194    protected virtual void OnCleared() {
195      if (Cleared != null)
196        Cleared(this, new EventArgs());
197      OnChanged();
198    }
199  }
200}
Note: See TracBrowser for help on using the repository browser.