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
Line 
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;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Common;
30
31namespace HeuristicLab.Data {
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>
36  public class ItemList<T> : ItemBase, IList<T> where T : IItem {
37
38    [Storable]
39    private List<T> list;
40
41    /// <summary>
42    /// Initializes a new instance of the class <see cref="ItemList&lt;T&gt;"/>.
43    /// </summary>
44    public ItemList() {
45      list = new List<T>();
46    }
47
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>
54    public override IItem Clone(ICloner cloner) {
55      ItemList<T> clone = new ItemList<T>();
56      cloner.RegisterClonedObject(this, clone);
57      CloneElements(cloner, clone);
58      return clone;
59    }
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>
68    protected void CloneElements(ICloner cloner, ItemList<T> destination) {
69      for (int i = 0; i < list.Count; i++)
70        destination.list.Add((T)cloner.Clone(list[i]));
71    }
72
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>
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
92    #region IList<T> Members
93    /// <inheritdoc cref="List&lt;T&gt;.IndexOf(T)"/>
94    public int IndexOf(T item) {
95      return list.IndexOf(item);
96    }
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>
105    public void Insert(int index, T item) {
106      list.Insert(index, item);
107      OnItemAdded(item, index);
108    }
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>
116    public void RemoveAt(int index) {
117      IItem item = list[index];
118      list.RemoveAt(index);
119      OnItemRemoved(item, index);
120    }
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>
126    public T this[int index] {
127      get { return list[index]; }
128      set { list[index] = value; }
129    }
130    #endregion
131
132    #region ICollection<T> Members
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>
140    public void Add(T item) {
141      list.Add(item);
142      OnItemAdded(item, list.Count - 1);
143    }
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>
150    public void Clear() {
151      list.Clear();
152      OnCleared();
153    }
154    /// <inheritdoc cref="List&lt;T&gt;.Contains"/>
155    public bool Contains(T item) {
156      return list.Contains(item);
157    }
158    /// <inheritdoc cref="List&lt;T&gt;.CopyTo(T[], int)"/>
159    public void CopyTo(T[] array, int arrayIndex) {
160      list.CopyTo(array, arrayIndex);
161    }
162    /// <inheritdoc cref="List&lt;T&gt;.Count"/>
163    public int Count {
164      get { return list.Count; }
165    }
166    /// <summary>
167    /// Checks whether the current list is read-only.
168    /// </summary>
169    /// <remarks>Always returns <c>false</c>.</remarks>
170    public bool IsReadOnly {
171      get { return false; }
172    }
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>
180    public bool Remove(T item) {
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
191    #region IEnumerable<T> Members
192    /// <inheritdoc cref="List&lt;T&gt;.GetEnumerator"/>
193    public IEnumerator<T> GetEnumerator() {
194      return list.GetEnumerator();
195    }
196    #endregion
197
198    #region IEnumerable Members
199    /// <inheritdoc cref="List&lt;T&gt;.GetEnumerator"/>
200    IEnumerator IEnumerable.GetEnumerator() {
201      return list.GetEnumerator();
202    }
203    #endregion
204
205    #region List<T> Methods
206    /// <inheritdoc cref="List&lt;T&gt;.LastIndexOf(T)"/>
207    public int LastIndexOf(T item) {
208      return list.LastIndexOf(item);
209    }
210
211    /// <inheritdoc cref="List&lt;T&gt;.LastIndexOf(T, int)"/>
212    public int LastIndexOf(T item, int index) {
213      return list.LastIndexOf(item, index);
214    }
215
216    /// <inheritdoc cref="List&lt;T&gt;.LastIndexOf(T, int, int)"/>
217    public int LastIndexOf(T item, int index, int count) {
218      return list.LastIndexOf(item, index, count);
219    }
220
221    /// <inheritdoc cref="List&lt;T&gt;.IndexOf(T, int)"/>
222    public int IndexOf(T item, int index) {
223      return list.IndexOf(item, index);
224    }
225
226    /// <inheritdoc cref="List&lt;T&gt;.IndexOf(T, int, int)"/>
227    public int IndexOf(T item, int index, int count) {
228      return list.IndexOf(item, index, count);
229    }
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>
234    public void AddRange(IEnumerable<T> collection) {
235      foreach (T obj in collection) {
236        this.Add(obj);
237      }
238    }
239
240    /// <inheritdoc cref="List&lt;T&gt;.Exists"/>
241    public bool Exists(Predicate<T> match) {
242      return list.Exists(match);
243    }
244
245    /// <inheritdoc cref="List&lt;T&gt;.BinarySearch(T)"/>
246    public int BinarySearch(T item) {
247      return list.BinarySearch(item);
248    }
249
250    /// <inheritdoc cref="List&lt;T&gt;.BinarySearch(T, System.Collections.Generic.IComparer&lt;T&gt;)"/>
251    public int BinarySearch(T item, IComparer<T> comparer) {
252      return list.BinarySearch(item, comparer);
253    }
254
255    /// <inheritdoc cref="List&lt;T&gt;.BinarySearch(int, int, T, System.Collections.Generic.IComparer&lt;T&gt;)"/>
256    public int BinarySearch(int index, int count, T item, IComparer<T> comparer) {
257      return list.BinarySearch(index, count, item, comparer);
258    }
259    /// <inheritdoc cref="List&lt;T&gt;.Find"/>
260    public T Find(Predicate<T> match) {
261      return list.Find(match);
262    }
263
264    /// <inheritdoc cref="List&lt;T&gt;.FindAll"/>
265    public List<T> FindAll(Predicate<T> match) {
266      return list.FindAll(match);
267    }
268
269    /// <inheritdoc cref="List&lt;T&gt;.FindIndex(System.Predicate&lt;T&gt;)"/>
270    public int FindIndex(Predicate<T> match) {
271      return list.FindIndex(match);
272    }
273
274    /// <inheritdoc cref="List&lt;T&gt;.FindLast"/>
275    public T FindLast(Predicate<T> match) {
276      return list.FindLast(match);
277    }
278
279    /// <inheritdoc cref="List&lt;T&gt;.FindLastIndex(System.Predicate&lt;T&gt;)"/>
280    public int FindLastIndex(Predicate<T> match) {
281      return list.FindLastIndex(match);
282    }
283
284    /// <inheritdoc cref="List&lt;T&gt;.Sort()"/>
285    public void Sort() {
286      list.Sort();
287    }
288
289    /// <inheritdoc cref="List&lt;T&gt;.Sort(System.Collections.Generic.IComparer&lt;T&gt;)"/>
290    public void Sort(IComparer<T> comparer) {
291      list.Sort(comparer);
292    }
293
294    /// <inheritdoc cref="List&lt;T&gt;.Sort(System.Comparison&lt;T&gt;)"/>
295    public void Sort(Comparison<T> comparison) {
296      list.Sort(comparison);
297    }
298
299    /// <inheritdoc cref="List&lt;T&gt;.Reverse()"/>
300    public void Reverse() {
301      list.Reverse();
302    }
303
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>
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
318    /// <inheritdoc cref="List&lt;T&gt;.TrueForAll"/>
319    public bool TrueForAll(Predicate<T> match) {
320      return list.TrueForAll(match);
321    }
322
323    #endregion
324
325    /// <summary>
326    /// Occurs where a new item is added to the list.
327    /// </summary>
328    public event EventHandler<EventArgs<IItem, int>> ItemAdded;
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>
335    protected virtual void OnItemAdded(IItem item, int index) {
336      if (ItemAdded != null)
337        ItemAdded(this, new EventArgs<IItem, int>(item, index));
338      OnChanged();
339    }
340    /// <summary>
341    /// Occurs when an element is deleted from the list.
342    /// </summary>
343    public event EventHandler<EventArgs<IItem, int>> ItemRemoved;
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>
350    protected virtual void OnItemRemoved(IItem item, int index) {
351      if (ItemRemoved != null)
352        ItemRemoved(this, new EventArgs<IItem, int>(item, index));
353      OnChanged();
354    }
355    /// <summary>
356    /// Occurs when the list is emptied.
357    /// </summary>
358    public event EventHandler Cleared;
359    /// <summary>
360    /// Fires a new <c>Cleared</c> event.
361    /// </summary>
362    /// <remarks>Calls <see cref="HeuristicLab.Core.ItemBase.OnChanged"/>.</remarks>
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.