Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/sources/HeuristicLab.Data/3.2/ItemList_T.cs @ 2716

Last change on this file since 2716 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

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