Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.2/ItemList_T.cs @ 2716

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

Implemented generic EventArgs (#796)

File size: 15.4 KB
RevLine 
[2]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;
[2474]27using HeuristicLab.Common;
[2]28using HeuristicLab.Core;
29
30namespace HeuristicLab.Data {
[737]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>
[40]35  public class ItemList<T> : ItemBase, IList<T> where T : IItem {
[68]36    private List<T> list;
[2]37
[737]38    /// <summary>
39    /// Initializes a new instance of the class <see cref="ItemList&lt;T&gt;"/>.
40    /// </summary>
[2]41    public ItemList() {
[40]42      list = new List<T>();
[2]43    }
44
[737]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>
[2]49    public override IView CreateView() {
[40]50      return new ItemListView<T>(this);
[2]51    }
52
[737]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>
[2]59    public override object Clone(IDictionary<Guid, object> clonedObjects) {
[40]60      ItemList<T> clone = new ItemList<T>();
[2]61      clonedObjects.Add(Guid, clone);
[68]62      CloneElements(clone, clonedObjects);
[2]63      return clone;
64    }
[737]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>
[68]73    protected void CloneElements(ItemList<T> destination, IDictionary<Guid, object> clonedObjects) {
74      for (int i = 0; i < list.Count; i++)
[69]75        destination.list.Add((T) Auxiliary.Clone(list[i], clonedObjects));
[68]76    }
[2]77
[737]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>
[53]87    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
[2]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    }
[737]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>
[53]99    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
[2]100      base.Populate(node, restoredObjects);
101      for (int i = 0; i < node.ChildNodes.Count; i++)
[53]102        list.Add((T) PersistenceManager.Restore(node.ChildNodes[i], restoredObjects));
[2]103    }
104
[737]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>
[2]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
[40]124    #region IList<T> Members
[737]125    /// <inheritdoc cref="List&lt;T&gt;.IndexOf(T)"/>
[40]126    public int IndexOf(T item) {
[2]127      return list.IndexOf(item);
128    }
[737]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>
[40]137    public void Insert(int index, T item) {
[2]138      list.Insert(index, item);
139      OnItemAdded(item, index);
140    }
[737]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>
[2]148    public void RemoveAt(int index) {
149      IItem item = list[index];
150      list.RemoveAt(index);
151      OnItemRemoved(item, index);
152    }
[737]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>
[40]158    public T this[int index] {
[2]159      get { return list[index]; }
[40]160      set { list[index] = value; }
[2]161    }
162    #endregion
163
[40]164    #region ICollection<T> Members
[737]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>
[40]172    public void Add(T item) {
[2]173      list.Add(item);
174      OnItemAdded(item, list.Count - 1);
175    }
[737]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>
[2]182    public void Clear() {
183      list.Clear();
184      OnCleared();
185    }
[737]186    /// <inheritdoc cref="List&lt;T&gt;.Contains"/>
[40]187    public bool Contains(T item) {
[2]188      return list.Contains(item);
189    }
[737]190    /// <inheritdoc cref="List&lt;T&gt;.CopyTo(T[], int)"/>
[40]191    public void CopyTo(T[] array, int arrayIndex) {
[2]192      list.CopyTo(array, arrayIndex);
193    }
[737]194    /// <inheritdoc cref="List&lt;T&gt;.Count"/>
[2]195    public int Count {
196      get { return list.Count; }
197    }
[737]198    /// <summary>
199    /// Checks whether the current list is read-only.
200    /// </summary>
201    /// <remarks>Always returns <c>false</c>.</remarks>
[2]202    public bool IsReadOnly {
203      get { return false; }
204    }
[737]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>
[40]212    public bool Remove(T item) {
[2]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
[40]223    #region IEnumerable<T> Members
[737]224    /// <inheritdoc cref="List&lt;T&gt;.GetEnumerator"/>
[40]225    public IEnumerator<T> GetEnumerator() {
[2]226      return list.GetEnumerator();
227    }
228    #endregion
229
230    #region IEnumerable Members
[737]231    /// <inheritdoc cref="List&lt;T&gt;.GetEnumerator"/>
[2]232    IEnumerator IEnumerable.GetEnumerator() {
233      return list.GetEnumerator();
234    }
235    #endregion
236
[53]237    #region List<T> Methods
[737]238    /// <inheritdoc cref="List&lt;T&gt;.LastIndexOf(T)"/>
[69]239    public int LastIndexOf(T item) {
240      return list.LastIndexOf(item);
241    }
242
[737]243    /// <inheritdoc cref="List&lt;T&gt;.LastIndexOf(T, int)"/>
[69]244    public int LastIndexOf(T item, int index) {
245      return list.LastIndexOf(item, index);
246    }
247
[737]248    /// <inheritdoc cref="List&lt;T&gt;.LastIndexOf(T, int, int)"/>
[69]249    public int LastIndexOf(T item, int index, int count) {
250      return list.LastIndexOf(item, index, count);
251    }
252
[737]253    /// <inheritdoc cref="List&lt;T&gt;.IndexOf(T, int)"/>
[69]254    public int IndexOf(T item, int index) {
255      return list.IndexOf(item, index);
256    }
257
[737]258    /// <inheritdoc cref="List&lt;T&gt;.IndexOf(T, int, int)"/>
[69]259    public int IndexOf(T item, int index, int count) {
260      return list.IndexOf(item, index, count);
261    }
[737]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>
[53]266    public void AddRange(IEnumerable<T> collection) {
267      foreach (T obj in collection) {
268        this.Add(obj);
269      }
270    }
271
[737]272    /// <inheritdoc cref="List&lt;T&gt;.Exists"/>
[53]273    public bool Exists(Predicate<T> match) {
274      return list.Exists(match);
275    }
276
[737]277    /// <inheritdoc cref="List&lt;T&gt;.BinarySearch(T)"/>
[69]278    public int BinarySearch(T item) {
279      return list.BinarySearch(item);
280    }
281
[737]282    /// <inheritdoc cref="List&lt;T&gt;.BinarySearch(T, System.Collections.Generic.IComparer&lt;T&gt;)"/>
[69]283    public int BinarySearch(T item, IComparer<T> comparer) {
284      return list.BinarySearch(item, comparer);
285    }
286
[737]287    /// <inheritdoc cref="List&lt;T&gt;.BinarySearch(int, int, T, System.Collections.Generic.IComparer&lt;T&gt;)"/>
[69]288    public int BinarySearch(int index, int count, T item, IComparer<T> comparer) {
289      return list.BinarySearch(index, count, item, comparer);
290    }
[737]291    /// <inheritdoc cref="List&lt;T&gt;.Find"/>
[60]292    public T Find(Predicate<T> match) {
293      return list.Find(match);
294    }
295
[737]296    /// <inheritdoc cref="List&lt;T&gt;.FindAll"/>
[53]297    public List<T> FindAll(Predicate<T> match) {
298      return list.FindAll(match);
299    }
[60]300
[737]301    /// <inheritdoc cref="List&lt;T&gt;.FindIndex(System.Predicate&lt;T&gt;)"/>
[60]302    public int FindIndex(Predicate<T> match) {
[69]303      return list.FindIndex(match);
[60]304    }
305
[737]306    /// <inheritdoc cref="List&lt;T&gt;.FindLast"/>
[69]307    public T FindLast(Predicate<T> match) {
308      return list.FindLast(match);
309    }
310
[737]311    /// <inheritdoc cref="List&lt;T&gt;.FindLastIndex(System.Predicate&lt;T&gt;)"/>
[69]312    public int FindLastIndex(Predicate<T> match) {
313      return list.FindLastIndex(match);
314    }
315
[737]316    /// <inheritdoc cref="List&lt;T&gt;.Sort()"/>
[60]317    public void Sort() {
[69]318      list.Sort();
[60]319    }
320
[737]321    /// <inheritdoc cref="List&lt;T&gt;.Sort(System.Collections.Generic.IComparer&lt;T&gt;)"/>
[60]322    public void Sort(IComparer<T> comparer) {
[69]323      list.Sort(comparer);
[60]324    }
325
[737]326    /// <inheritdoc cref="List&lt;T&gt;.Sort(System.Comparison&lt;T&gt;)"/>
[60]327    public void Sort(Comparison<T> comparison) {
[69]328      list.Sort(comparison);
329    }
[60]330
[737]331    /// <inheritdoc cref="List&lt;T&gt;.Reverse()"/>
[60]332    public void Reverse() {
[69]333      list.Reverse();
[60]334    }
[69]335
[737]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>
[69]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
[737]350    /// <inheritdoc cref="List&lt;T&gt;.TrueForAll"/>
[69]351    public bool TrueForAll(Predicate<T> match) {
352      return list.TrueForAll(match);
353    }
354
[53]355    #endregion
356
[737]357    /// <summary>
358    /// Occurs where a new item is added to the list.
359    /// </summary>
[2474]360    public event EventHandler<EventArgs<IItem, int>> ItemAdded;
[737]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>
[2]367    protected virtual void OnItemAdded(IItem item, int index) {
368      if (ItemAdded != null)
[2474]369        ItemAdded(this, new EventArgs<IItem, int>(item, index));
[2]370      OnChanged();
371    }
[737]372    /// <summary>
373    /// Occurs when an element is deleted from the list.
374    /// </summary>
[2474]375    public event EventHandler<EventArgs<IItem, int>> ItemRemoved;
[737]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>
[2]382    protected virtual void OnItemRemoved(IItem item, int index) {
383      if (ItemRemoved != null)
[2474]384        ItemRemoved(this, new EventArgs<IItem, int>(item, index));
[2]385      OnChanged();
386    }
[737]387    /// <summary>
388    /// Occurs when the list is emptied.
389    /// </summary>
[2]390    public event EventHandler Cleared;
[737]391    /// <summary>
392    /// Fires a new <c>Cleared</c> event.
393    /// </summary>
394    /// <remarks>Calls <see cref="HeuristicLab.Core.ItemBase.OnChanged"/>.</remarks>
[2]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.