Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/ItemList_T.cs @ 2587

Last change on this file since 2587 was 2526, checked in by swagner, 15 years ago

Refactored cloning (#806)

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