Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1347 was 737, checked in by vdorfer, 16 years ago

Created API documentation for HeuristLab.Data namespace (#331)

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