Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 53 was 53, checked in by mkofler, 16 years ago

Worked on ticket #51

  • implemented AddRange, Exists and FindAll
File size: 5.1 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      for (int i = 0; i < list.Count; i++)
45        clone.list.Add((T) Auxiliary.Clone(list[i], clonedObjects));
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++)
58        list.Add((T) PersistenceManager.Restore(node.ChildNodes[i], restoredObjects));
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
75    #region IList<T> Members
76    public int IndexOf(T item) {
77      return list.IndexOf(item);
78    }
79    public void Insert(int index, T item) {
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    }
88    public T this[int index] {
89      get { return list[index]; }
90      set { list[index] = value; }
91    }
92    #endregion
93
94    #region ICollection<T> Members
95    public void Add(T item) {
96      list.Add(item);
97      OnItemAdded(item, list.Count - 1);
98    }
99    public void Clear() {
100      list.Clear();
101      OnCleared();
102    }
103    public bool Contains(T item) {
104      return list.Contains(item);
105    }
106    public void CopyTo(T[] array, int arrayIndex) {
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    }
115    public bool Remove(T item) {
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
126    #region IEnumerable<T> Members
127    public IEnumerator<T> GetEnumerator() {
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    #region List<T> Methods
139    public void AddRange(IEnumerable<T> collection) {
140      foreach (T obj in collection) {
141        this.Add(obj);
142      }
143    }
144
145    public bool Exists(Predicate<T> match) {
146      return list.Exists(match);
147    }
148
149    public List<T> FindAll(Predicate<T> match) {
150      return list.FindAll(match);
151    }
152    #endregion
153
154    public event EventHandler<ItemIndexEventArgs> ItemAdded;
155    protected virtual void OnItemAdded(IItem item, int index) {
156      if (ItemAdded != null)
157        ItemAdded(this, new ItemIndexEventArgs(item, index));
158      OnChanged();
159    }
160    public event EventHandler<ItemIndexEventArgs> ItemRemoved;
161    protected virtual void OnItemRemoved(IItem item, int index) {
162      if (ItemRemoved != null)
163        ItemRemoved(this, new ItemIndexEventArgs(item, index));
164      OnChanged();
165    }
166    public event EventHandler Cleared;
167    protected virtual void OnCleared() {
168      if (Cleared != null)
169        Cleared(this, new EventArgs());
170      OnChanged();
171    }
172  }
173}
Note: See TracBrowser for help on using the repository browser.