Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Data/3.2/ItemList_T.cs @ 4456

Last change on this file since 4456 was 2474, checked in by swagner, 15 years ago

Implemented generic EventArgs (#796)

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