Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Data/3.3/ItemList_T.cs @ 4071

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

Implemented generic EventArgs (#796)

File size: 13.8 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    /// 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>
52    public override IView CreateView() {
53      return new ItemListView<T>(this);
54    }
55
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>
62    public override object Clone(IDictionary<Guid, object> clonedObjects) {
63      ItemList<T> clone = new ItemList<T>();
64      clonedObjects.Add(Guid, clone);
65      CloneElements(clone, clonedObjects);
66      return clone;
67    }
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>
76    protected void CloneElements(ItemList<T> destination, IDictionary<Guid, object> clonedObjects) {
77      for (int i = 0; i < list.Count; i++)
78        destination.list.Add((T) Auxiliary.Clone(list[i], clonedObjects));
79    }
80
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>
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
100    #region IList<T> Members
101    /// <inheritdoc cref="List&lt;T&gt;.IndexOf(T)"/>
102    public int IndexOf(T item) {
103      return list.IndexOf(item);
104    }
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>
113    public void Insert(int index, T item) {
114      list.Insert(index, item);
115      OnItemAdded(item, index);
116    }
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>
124    public void RemoveAt(int index) {
125      IItem item = list[index];
126      list.RemoveAt(index);
127      OnItemRemoved(item, index);
128    }
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>
134    public T this[int index] {
135      get { return list[index]; }
136      set { list[index] = value; }
137    }
138    #endregion
139
140    #region ICollection<T> Members
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>
148    public void Add(T item) {
149      list.Add(item);
150      OnItemAdded(item, list.Count - 1);
151    }
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>
158    public void Clear() {
159      list.Clear();
160      OnCleared();
161    }
162    /// <inheritdoc cref="List&lt;T&gt;.Contains"/>
163    public bool Contains(T item) {
164      return list.Contains(item);
165    }
166    /// <inheritdoc cref="List&lt;T&gt;.CopyTo(T[], int)"/>
167    public void CopyTo(T[] array, int arrayIndex) {
168      list.CopyTo(array, arrayIndex);
169    }
170    /// <inheritdoc cref="List&lt;T&gt;.Count"/>
171    public int Count {
172      get { return list.Count; }
173    }
174    /// <summary>
175    /// Checks whether the current list is read-only.
176    /// </summary>
177    /// <remarks>Always returns <c>false</c>.</remarks>
178    public bool IsReadOnly {
179      get { return false; }
180    }
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>
188    public bool Remove(T item) {
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
199    #region IEnumerable<T> Members
200    /// <inheritdoc cref="List&lt;T&gt;.GetEnumerator"/>
201    public IEnumerator<T> GetEnumerator() {
202      return list.GetEnumerator();
203    }
204    #endregion
205
206    #region IEnumerable Members
207    /// <inheritdoc cref="List&lt;T&gt;.GetEnumerator"/>
208    IEnumerator IEnumerable.GetEnumerator() {
209      return list.GetEnumerator();
210    }
211    #endregion
212
213    #region List<T> Methods
214    /// <inheritdoc cref="List&lt;T&gt;.LastIndexOf(T)"/>
215    public int LastIndexOf(T item) {
216      return list.LastIndexOf(item);
217    }
218
219    /// <inheritdoc cref="List&lt;T&gt;.LastIndexOf(T, int)"/>
220    public int LastIndexOf(T item, int index) {
221      return list.LastIndexOf(item, index);
222    }
223
224    /// <inheritdoc cref="List&lt;T&gt;.LastIndexOf(T, int, int)"/>
225    public int LastIndexOf(T item, int index, int count) {
226      return list.LastIndexOf(item, index, count);
227    }
228
229    /// <inheritdoc cref="List&lt;T&gt;.IndexOf(T, int)"/>
230    public int IndexOf(T item, int index) {
231      return list.IndexOf(item, index);
232    }
233
234    /// <inheritdoc cref="List&lt;T&gt;.IndexOf(T, int, int)"/>
235    public int IndexOf(T item, int index, int count) {
236      return list.IndexOf(item, index, count);
237    }
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>
242    public void AddRange(IEnumerable<T> collection) {
243      foreach (T obj in collection) {
244        this.Add(obj);
245      }
246    }
247
248    /// <inheritdoc cref="List&lt;T&gt;.Exists"/>
249    public bool Exists(Predicate<T> match) {
250      return list.Exists(match);
251    }
252
253    /// <inheritdoc cref="List&lt;T&gt;.BinarySearch(T)"/>
254    public int BinarySearch(T item) {
255      return list.BinarySearch(item);
256    }
257
258    /// <inheritdoc cref="List&lt;T&gt;.BinarySearch(T, System.Collections.Generic.IComparer&lt;T&gt;)"/>
259    public int BinarySearch(T item, IComparer<T> comparer) {
260      return list.BinarySearch(item, comparer);
261    }
262
263    /// <inheritdoc cref="List&lt;T&gt;.BinarySearch(int, int, T, System.Collections.Generic.IComparer&lt;T&gt;)"/>
264    public int BinarySearch(int index, int count, T item, IComparer<T> comparer) {
265      return list.BinarySearch(index, count, item, comparer);
266    }
267    /// <inheritdoc cref="List&lt;T&gt;.Find"/>
268    public T Find(Predicate<T> match) {
269      return list.Find(match);
270    }
271
272    /// <inheritdoc cref="List&lt;T&gt;.FindAll"/>
273    public List<T> FindAll(Predicate<T> match) {
274      return list.FindAll(match);
275    }
276
277    /// <inheritdoc cref="List&lt;T&gt;.FindIndex(System.Predicate&lt;T&gt;)"/>
278    public int FindIndex(Predicate<T> match) {
279      return list.FindIndex(match);
280    }
281
282    /// <inheritdoc cref="List&lt;T&gt;.FindLast"/>
283    public T FindLast(Predicate<T> match) {
284      return list.FindLast(match);
285    }
286
287    /// <inheritdoc cref="List&lt;T&gt;.FindLastIndex(System.Predicate&lt;T&gt;)"/>
288    public int FindLastIndex(Predicate<T> match) {
289      return list.FindLastIndex(match);
290    }
291
292    /// <inheritdoc cref="List&lt;T&gt;.Sort()"/>
293    public void Sort() {
294      list.Sort();
295    }
296
297    /// <inheritdoc cref="List&lt;T&gt;.Sort(System.Collections.Generic.IComparer&lt;T&gt;)"/>
298    public void Sort(IComparer<T> comparer) {
299      list.Sort(comparer);
300    }
301
302    /// <inheritdoc cref="List&lt;T&gt;.Sort(System.Comparison&lt;T&gt;)"/>
303    public void Sort(Comparison<T> comparison) {
304      list.Sort(comparison);
305    }
306
307    /// <inheritdoc cref="List&lt;T&gt;.Reverse()"/>
308    public void Reverse() {
309      list.Reverse();
310    }
311
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>
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
326    /// <inheritdoc cref="List&lt;T&gt;.TrueForAll"/>
327    public bool TrueForAll(Predicate<T> match) {
328      return list.TrueForAll(match);
329    }
330
331    #endregion
332
333    /// <summary>
334    /// Occurs where a new item is added to the list.
335    /// </summary>
336    public event EventHandler<EventArgs<IItem, int>> ItemAdded;
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>
343    protected virtual void OnItemAdded(IItem item, int index) {
344      if (ItemAdded != null)
345        ItemAdded(this, new EventArgs<IItem, int>(item, index));
346      OnChanged();
347    }
348    /// <summary>
349    /// Occurs when an element is deleted from the list.
350    /// </summary>
351    public event EventHandler<EventArgs<IItem, int>> ItemRemoved;
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>
358    protected virtual void OnItemRemoved(IItem item, int index) {
359      if (ItemRemoved != null)
360        ItemRemoved(this, new EventArgs<IItem, int>(item, index));
361      OnChanged();
362    }
363    /// <summary>
364    /// Occurs when the list is emptied.
365    /// </summary>
366    public event EventHandler Cleared;
367    /// <summary>
368    /// Fires a new <c>Cleared</c> event.
369    /// </summary>
370    /// <remarks>Calls <see cref="HeuristicLab.Core.ItemBase.OnChanged"/>.</remarks>
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.