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 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Data {
|
---|
31 | /// <summary>
|
---|
32 | /// Represents a list of items where the items are of the type <typeparamref name="T"/>.
|
---|
33 | /// </summary>
|
---|
34 | /// <typeparam name="T">The type of the items in this list. <paramref name="T"/> must implement <see cref="IItem"/>.</typeparam>
|
---|
35 | public class ItemList<T> : ItemBase, IList<T> where T : IItem {
|
---|
36 |
|
---|
37 | [Storable]
|
---|
38 | private List<T> list;
|
---|
39 |
|
---|
40 | /// <summary>
|
---|
41 | /// Initializes a new instance of the class <see cref="ItemList<T>"/>.
|
---|
42 | /// </summary>
|
---|
43 | public ItemList() {
|
---|
44 | list = new List<T>();
|
---|
45 | }
|
---|
46 |
|
---|
47 | /// <summary>
|
---|
48 | /// Creates a new instance of <see cref="ItemListView<T>"/>.
|
---|
49 | /// </summary>
|
---|
50 | /// <returns>The created instance as <see cref="ItemListView<T>"/>.</returns>
|
---|
51 | public override IView CreateView() {
|
---|
52 | return new ItemListView<T>(this);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /// <summary>
|
---|
56 | /// Clones the current instance.
|
---|
57 | /// </summary>
|
---|
58 | /// <remarks>Saves the cloned instance in the dictionary <paramref name="clonedObjects"/>.</remarks>
|
---|
59 | /// <param name="clonedObjects">A dictionary of all already cloned objects.</param>
|
---|
60 | /// <returns>The cloned instance as <see cref="ItemList<T>"/>.</returns>
|
---|
61 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
62 | ItemList<T> clone = new ItemList<T>();
|
---|
63 | clonedObjects.Add(Guid, clone);
|
---|
64 | CloneElements(clone, clonedObjects);
|
---|
65 | return clone;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /// <summary>
|
---|
69 | /// Clones all elements in the current list.
|
---|
70 | /// </summary>
|
---|
71 | /// <remarks>Clones only elements that have not already been cloned
|
---|
72 | /// (and therefore exist in the dictionary <paramref name="clonedObjects"/>).</remarks>
|
---|
73 | /// <param name="destination">The <see cref="ItemList<T>"/> where to save the cloned objects.</param>
|
---|
74 | /// <param name="clonedObjects">A dictionary of all already cloned objects.</param>
|
---|
75 | protected void CloneElements(ItemList<T> destination, IDictionary<Guid, object> clonedObjects) {
|
---|
76 | for (int i = 0; i < list.Count; i++)
|
---|
77 | destination.list.Add((T) Auxiliary.Clone(list[i], clonedObjects));
|
---|
78 | }
|
---|
79 |
|
---|
80 | /// <summary>
|
---|
81 | /// The string representation of the list.
|
---|
82 | /// </summary>
|
---|
83 | /// <returns>The elements of the list as string, each element separated by a semicolon. <br/>
|
---|
84 | /// If the list is empty, "Empty List" is returned.</returns>
|
---|
85 | public override string ToString() {
|
---|
86 | if (list.Count > 0) {
|
---|
87 | StringBuilder builder = new StringBuilder();
|
---|
88 | builder.Append(list[0].ToString());
|
---|
89 | for (int i = 1; i < list.Count; i++) {
|
---|
90 | builder.Append(";");
|
---|
91 | builder.Append(list[i].ToString());
|
---|
92 | }
|
---|
93 | return builder.ToString();
|
---|
94 | } else {
|
---|
95 | return "Empty List";
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | #region IList<T> Members
|
---|
100 | /// <inheritdoc cref="List<T>.IndexOf(T)"/>
|
---|
101 | public int IndexOf(T item) {
|
---|
102 | return list.IndexOf(item);
|
---|
103 | }
|
---|
104 | ///// <summary>
|
---|
105 | ///// Inserts a specified <paramref name="item"/> at a specific position <paramref name="index"/>.
|
---|
106 | ///// </summary>
|
---|
107 | ///// <remarks>Calls <see cref="M:ItemList<T>.OnItemAdded"/>.</remarks>
|
---|
108 | ///// <param name="index">The position where to insert the <paramref name="item"/>.</param>
|
---|
109 | ///// <param name="item">The element to insert.</param>
|
---|
110 | /// <inheritdoc cref="List<T>.Insert"/>
|
---|
111 | /// <remarks>Calls <see cref="OnItemAdded"/>.</remarks>
|
---|
112 | public void Insert(int index, T item) {
|
---|
113 | list.Insert(index, item);
|
---|
114 | OnItemAdded(item, index);
|
---|
115 | }
|
---|
116 | ///// <summary>
|
---|
117 | ///// Removes the element at the specified <paramref name="index"/>.
|
---|
118 | ///// </summary>
|
---|
119 | ///// <remarks>Calls <see cref="M:ItemList<T>.OnItemRemoved"/>.</remarks>
|
---|
120 | ///// <param name="index">The position where to remove the element.</param>
|
---|
121 | /// <inheritdoc cref="List<T>.RemoveAt"/>
|
---|
122 | /// <remarks>Calls <see cref="OnItemRemoved"/>.</remarks>
|
---|
123 | public void RemoveAt(int index) {
|
---|
124 | IItem item = list[index];
|
---|
125 | list.RemoveAt(index);
|
---|
126 | OnItemRemoved(item, index);
|
---|
127 | }
|
---|
128 | /// <summary>
|
---|
129 | /// Gets or sets the element at the specified <paramref name="index"/>.
|
---|
130 | /// </summary>
|
---|
131 | /// <param name="index">The position where to get or set the element.</param>
|
---|
132 | /// <returns>The element at the specified <paramref name="index"/>.</returns>
|
---|
133 | public T this[int index] {
|
---|
134 | get { return list[index]; }
|
---|
135 | set { list[index] = value; }
|
---|
136 | }
|
---|
137 | #endregion
|
---|
138 |
|
---|
139 | #region ICollection<T> Members
|
---|
140 | ///// <summary>
|
---|
141 | ///// Adds an item to the current list.
|
---|
142 | ///// </summary>
|
---|
143 | ///// <remarks>Calls <see cref="M:ItemList<T>.OnItemAdded"/>.</remarks>
|
---|
144 | ///// <param name="item">The element to add.</param>
|
---|
145 | /// <inheritdoc cref="List<T>.Add"/>
|
---|
146 | /// <remarks>Calls <see cref="OnItemAdded"/>.</remarks>
|
---|
147 | public void Add(T item) {
|
---|
148 | list.Add(item);
|
---|
149 | OnItemAdded(item, list.Count - 1);
|
---|
150 | }
|
---|
151 | ///// <summary>
|
---|
152 | ///// Empties the list.
|
---|
153 | ///// </summary>
|
---|
154 | ///// <remarks>Calls <see cref="M:ItemList<T>.OnCleared"/>.</remarks>
|
---|
155 | /// <inheritdoc cref="List<T>.Clear"/>
|
---|
156 | /// <remarks>Calls <see cref="OnCleared"/>.</remarks>
|
---|
157 | public void Clear() {
|
---|
158 | list.Clear();
|
---|
159 | OnCleared();
|
---|
160 | }
|
---|
161 | /// <inheritdoc cref="List<T>.Contains"/>
|
---|
162 | public bool Contains(T item) {
|
---|
163 | return list.Contains(item);
|
---|
164 | }
|
---|
165 | /// <inheritdoc cref="List<T>.CopyTo(T[], int)"/>
|
---|
166 | public void CopyTo(T[] array, int arrayIndex) {
|
---|
167 | list.CopyTo(array, arrayIndex);
|
---|
168 | }
|
---|
169 | /// <inheritdoc cref="List<T>.Count"/>
|
---|
170 | public int Count {
|
---|
171 | get { return list.Count; }
|
---|
172 | }
|
---|
173 | /// <summary>
|
---|
174 | /// Checks whether the current list is read-only.
|
---|
175 | /// </summary>
|
---|
176 | /// <remarks>Always returns <c>false</c>.</remarks>
|
---|
177 | public bool IsReadOnly {
|
---|
178 | get { return false; }
|
---|
179 | }
|
---|
180 | /// <summary>
|
---|
181 | /// Removes the specified <paramref name="item"/>.
|
---|
182 | /// </summary>
|
---|
183 | /// <remarks>If the <paramref name="item"/> can be successfully removed,
|
---|
184 | /// <see cref="OnItemRemoved"/> is called.</remarks>
|
---|
185 | /// <param name="item">The element to remove.</param>
|
---|
186 | /// <returns><c>true</c>, if the element could be removed successfully, <c>false</c> otherwise.</returns>
|
---|
187 | public bool Remove(T item) {
|
---|
188 | int index = list.IndexOf(item);
|
---|
189 | if (list.Remove(item)) {
|
---|
190 | OnItemRemoved(item, index);
|
---|
191 | return true;
|
---|
192 | } else {
|
---|
193 | return false;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | #endregion
|
---|
197 |
|
---|
198 | #region IEnumerable<T> Members
|
---|
199 | /// <inheritdoc cref="List<T>.GetEnumerator"/>
|
---|
200 | public IEnumerator<T> GetEnumerator() {
|
---|
201 | return list.GetEnumerator();
|
---|
202 | }
|
---|
203 | #endregion
|
---|
204 |
|
---|
205 | #region IEnumerable Members
|
---|
206 | /// <inheritdoc cref="List<T>.GetEnumerator"/>
|
---|
207 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
208 | return list.GetEnumerator();
|
---|
209 | }
|
---|
210 | #endregion
|
---|
211 |
|
---|
212 | #region List<T> Methods
|
---|
213 | /// <inheritdoc cref="List<T>.LastIndexOf(T)"/>
|
---|
214 | public int LastIndexOf(T item) {
|
---|
215 | return list.LastIndexOf(item);
|
---|
216 | }
|
---|
217 |
|
---|
218 | /// <inheritdoc cref="List<T>.LastIndexOf(T, int)"/>
|
---|
219 | public int LastIndexOf(T item, int index) {
|
---|
220 | return list.LastIndexOf(item, index);
|
---|
221 | }
|
---|
222 |
|
---|
223 | /// <inheritdoc cref="List<T>.LastIndexOf(T, int, int)"/>
|
---|
224 | public int LastIndexOf(T item, int index, int count) {
|
---|
225 | return list.LastIndexOf(item, index, count);
|
---|
226 | }
|
---|
227 |
|
---|
228 | /// <inheritdoc cref="List<T>.IndexOf(T, int)"/>
|
---|
229 | public int IndexOf(T item, int index) {
|
---|
230 | return list.IndexOf(item, index);
|
---|
231 | }
|
---|
232 |
|
---|
233 | /// <inheritdoc cref="List<T>.IndexOf(T, int, int)"/>
|
---|
234 | public int IndexOf(T item, int index, int count) {
|
---|
235 | return list.IndexOf(item, index, count);
|
---|
236 | }
|
---|
237 | /// <summary>
|
---|
238 | /// Adds all the elements in the specified <paramref name="collection"/> to the current list.
|
---|
239 | /// </summary>
|
---|
240 | /// <param name="collection">The elements to add to the current list.</param>
|
---|
241 | public void AddRange(IEnumerable<T> collection) {
|
---|
242 | foreach (T obj in collection) {
|
---|
243 | this.Add(obj);
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | /// <inheritdoc cref="List<T>.Exists"/>
|
---|
248 | public bool Exists(Predicate<T> match) {
|
---|
249 | return list.Exists(match);
|
---|
250 | }
|
---|
251 |
|
---|
252 | /// <inheritdoc cref="List<T>.BinarySearch(T)"/>
|
---|
253 | public int BinarySearch(T item) {
|
---|
254 | return list.BinarySearch(item);
|
---|
255 | }
|
---|
256 |
|
---|
257 | /// <inheritdoc cref="List<T>.BinarySearch(T, System.Collections.Generic.IComparer<T>)"/>
|
---|
258 | public int BinarySearch(T item, IComparer<T> comparer) {
|
---|
259 | return list.BinarySearch(item, comparer);
|
---|
260 | }
|
---|
261 |
|
---|
262 | /// <inheritdoc cref="List<T>.BinarySearch(int, int, T, System.Collections.Generic.IComparer<T>)"/>
|
---|
263 | public int BinarySearch(int index, int count, T item, IComparer<T> comparer) {
|
---|
264 | return list.BinarySearch(index, count, item, comparer);
|
---|
265 | }
|
---|
266 | /// <inheritdoc cref="List<T>.Find"/>
|
---|
267 | public T Find(Predicate<T> match) {
|
---|
268 | return list.Find(match);
|
---|
269 | }
|
---|
270 |
|
---|
271 | /// <inheritdoc cref="List<T>.FindAll"/>
|
---|
272 | public List<T> FindAll(Predicate<T> match) {
|
---|
273 | return list.FindAll(match);
|
---|
274 | }
|
---|
275 |
|
---|
276 | /// <inheritdoc cref="List<T>.FindIndex(System.Predicate<T>)"/>
|
---|
277 | public int FindIndex(Predicate<T> match) {
|
---|
278 | return list.FindIndex(match);
|
---|
279 | }
|
---|
280 |
|
---|
281 | /// <inheritdoc cref="List<T>.FindLast"/>
|
---|
282 | public T FindLast(Predicate<T> match) {
|
---|
283 | return list.FindLast(match);
|
---|
284 | }
|
---|
285 |
|
---|
286 | /// <inheritdoc cref="List<T>.FindLastIndex(System.Predicate<T>)"/>
|
---|
287 | public int FindLastIndex(Predicate<T> match) {
|
---|
288 | return list.FindLastIndex(match);
|
---|
289 | }
|
---|
290 |
|
---|
291 | /// <inheritdoc cref="List<T>.Sort()"/>
|
---|
292 | public void Sort() {
|
---|
293 | list.Sort();
|
---|
294 | }
|
---|
295 |
|
---|
296 | /// <inheritdoc cref="List<T>.Sort(System.Collections.Generic.IComparer<T>)"/>
|
---|
297 | public void Sort(IComparer<T> comparer) {
|
---|
298 | list.Sort(comparer);
|
---|
299 | }
|
---|
300 |
|
---|
301 | /// <inheritdoc cref="List<T>.Sort(System.Comparison<T>)"/>
|
---|
302 | public void Sort(Comparison<T> comparison) {
|
---|
303 | list.Sort(comparison);
|
---|
304 | }
|
---|
305 |
|
---|
306 | /// <inheritdoc cref="List<T>.Reverse()"/>
|
---|
307 | public void Reverse() {
|
---|
308 | list.Reverse();
|
---|
309 | }
|
---|
310 |
|
---|
311 | /// <summary>
|
---|
312 | /// Converts all elements in the current list to a specified type <typeparamref name="TOutput"/>.
|
---|
313 | /// </summary>
|
---|
314 | /// <typeparam name="TOutput">The type to convert the items to, which must implement <see cref="IItem"/>.</typeparam>
|
---|
315 | /// <param name="converter">A delegate that converts elements from type <c>T</c> to type <typeparamref name="TOutput"/>.</param>
|
---|
316 | /// <returns>A list containing the converted elements.</returns>
|
---|
317 | public ItemList<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter) where TOutput : IItem {
|
---|
318 | ItemList<TOutput> targetList = new ItemList<TOutput>();
|
---|
319 | foreach (T item in list) {
|
---|
320 | targetList.Add(converter.Invoke(item));
|
---|
321 | }
|
---|
322 | return targetList;
|
---|
323 | }
|
---|
324 |
|
---|
325 | /// <inheritdoc cref="List<T>.TrueForAll"/>
|
---|
326 | public bool TrueForAll(Predicate<T> match) {
|
---|
327 | return list.TrueForAll(match);
|
---|
328 | }
|
---|
329 |
|
---|
330 | #endregion
|
---|
331 |
|
---|
332 | /// <summary>
|
---|
333 | /// Occurs where a new item is added to the list.
|
---|
334 | /// </summary>
|
---|
335 | public event EventHandler<ItemIndexEventArgs> ItemAdded;
|
---|
336 | /// <summary>
|
---|
337 | /// Fires a new <c>ItemAdded</c> event.
|
---|
338 | /// </summary>
|
---|
339 | /// <remarks>Calls <see cref="HeuristicLab.Core.ItemBase.OnChanged"/>.</remarks>
|
---|
340 | /// <param name="item">The added element.</param>
|
---|
341 | /// <param name="index">The position where the new element was added.</param>
|
---|
342 | protected virtual void OnItemAdded(IItem item, int index) {
|
---|
343 | if (ItemAdded != null)
|
---|
344 | ItemAdded(this, new ItemIndexEventArgs(item, index));
|
---|
345 | OnChanged();
|
---|
346 | }
|
---|
347 | /// <summary>
|
---|
348 | /// Occurs when an element is deleted from the list.
|
---|
349 | /// </summary>
|
---|
350 | public event EventHandler<ItemIndexEventArgs> ItemRemoved;
|
---|
351 | /// <summary>
|
---|
352 | /// Fires a new <c>ItemRemoved</c> event.
|
---|
353 | /// </summary>
|
---|
354 | /// <remarks>Calls <see cref="HeuristicLab.Core.ItemBase.OnChanged"/>.</remarks>
|
---|
355 | /// <param name="item">The removed element.</param>
|
---|
356 | /// <param name="index">The position from where the element was removed.</param>
|
---|
357 | protected virtual void OnItemRemoved(IItem item, int index) {
|
---|
358 | if (ItemRemoved != null)
|
---|
359 | ItemRemoved(this, new ItemIndexEventArgs(item, index));
|
---|
360 | OnChanged();
|
---|
361 | }
|
---|
362 | /// <summary>
|
---|
363 | /// Occurs when the list is emptied.
|
---|
364 | /// </summary>
|
---|
365 | public event EventHandler Cleared;
|
---|
366 | /// <summary>
|
---|
367 | /// Fires a new <c>Cleared</c> event.
|
---|
368 | /// </summary>
|
---|
369 | /// <remarks>Calls <see cref="HeuristicLab.Core.ItemBase.OnChanged"/>.</remarks>
|
---|
370 | protected virtual void OnCleared() {
|
---|
371 | if (Cleared != null)
|
---|
372 | Cleared(this, new EventArgs());
|
---|
373 | OnChanged();
|
---|
374 | }
|
---|
375 | }
|
---|
376 | }
|
---|