Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented generic EventArgs (#796)

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