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