Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2273 was 1823, checked in by epitzer, 16 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

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