#region License Information /* HeuristicLab * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Xml; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Data { /// /// Represents a list of items where the items are of the type . /// /// The type of the items in this list. must implement . public class ItemList : ItemBase, IList where T : IItem { [Storable] private List list; /// /// Initializes a new instance of the class . /// public ItemList() { list = new List(); } /// /// Creates a new instance of . /// /// The created instance as . public override IView CreateView() { return new ItemListView(this); } /// /// Clones the current instance. /// /// Saves the cloned instance in the dictionary . /// A dictionary of all already cloned objects. /// The cloned instance as . public override object Clone(IDictionary clonedObjects) { ItemList clone = new ItemList(); clonedObjects.Add(Guid, clone); CloneElements(clone, clonedObjects); return clone; } /// /// Clones all elements in the current list. /// /// Clones only elements that have not already been cloned /// (and therefore exist in the dictionary ). /// The where to save the cloned objects. /// A dictionary of all already cloned objects. protected void CloneElements(ItemList destination, IDictionary clonedObjects) { for (int i = 0; i < list.Count; i++) destination.list.Add((T) Auxiliary.Clone(list[i], clonedObjects)); } /// /// The string representation of the list. /// /// The elements of the list as string, each element separated by a semicolon.
/// If the list is empty, "Empty List" is returned.
public override string ToString() { if (list.Count > 0) { StringBuilder builder = new StringBuilder(); builder.Append(list[0].ToString()); for (int i = 1; i < list.Count; i++) { builder.Append(";"); builder.Append(list[i].ToString()); } return builder.ToString(); } else { return "Empty List"; } } #region IList Members /// public int IndexOf(T item) { return list.IndexOf(item); } ///// ///// Inserts a specified at a specific position . ///// ///// Calls . ///// The position where to insert the . ///// The element to insert. /// /// Calls . public void Insert(int index, T item) { list.Insert(index, item); OnItemAdded(item, index); } ///// ///// Removes the element at the specified . ///// ///// Calls . ///// The position where to remove the element. /// /// Calls . public void RemoveAt(int index) { IItem item = list[index]; list.RemoveAt(index); OnItemRemoved(item, index); } /// /// Gets or sets the element at the specified . /// /// The position where to get or set the element. /// The element at the specified . public T this[int index] { get { return list[index]; } set { list[index] = value; } } #endregion #region ICollection Members ///// ///// Adds an item to the current list. ///// ///// Calls . ///// The element to add. /// /// Calls . public void Add(T item) { list.Add(item); OnItemAdded(item, list.Count - 1); } ///// ///// Empties the list. ///// ///// Calls . /// /// Calls . public void Clear() { list.Clear(); OnCleared(); } /// public bool Contains(T item) { return list.Contains(item); } /// public void CopyTo(T[] array, int arrayIndex) { list.CopyTo(array, arrayIndex); } /// public int Count { get { return list.Count; } } /// /// Checks whether the current list is read-only. /// /// Always returns false. public bool IsReadOnly { get { return false; } } /// /// Removes the specified . /// /// If the can be successfully removed, /// is called. /// The element to remove. /// true, if the element could be removed successfully, false otherwise. public bool Remove(T item) { int index = list.IndexOf(item); if (list.Remove(item)) { OnItemRemoved(item, index); return true; } else { return false; } } #endregion #region IEnumerable Members /// public IEnumerator GetEnumerator() { return list.GetEnumerator(); } #endregion #region IEnumerable Members /// IEnumerator IEnumerable.GetEnumerator() { return list.GetEnumerator(); } #endregion #region List Methods /// public int LastIndexOf(T item) { return list.LastIndexOf(item); } /// public int LastIndexOf(T item, int index) { return list.LastIndexOf(item, index); } /// public int LastIndexOf(T item, int index, int count) { return list.LastIndexOf(item, index, count); } /// public int IndexOf(T item, int index) { return list.IndexOf(item, index); } /// public int IndexOf(T item, int index, int count) { return list.IndexOf(item, index, count); } /// /// Adds all the elements in the specified to the current list. /// /// The elements to add to the current list. public void AddRange(IEnumerable collection) { foreach (T obj in collection) { this.Add(obj); } } /// public bool Exists(Predicate match) { return list.Exists(match); } /// public int BinarySearch(T item) { return list.BinarySearch(item); } /// public int BinarySearch(T item, IComparer comparer) { return list.BinarySearch(item, comparer); } /// public int BinarySearch(int index, int count, T item, IComparer comparer) { return list.BinarySearch(index, count, item, comparer); } /// public T Find(Predicate match) { return list.Find(match); } /// public List FindAll(Predicate match) { return list.FindAll(match); } /// public int FindIndex(Predicate match) { return list.FindIndex(match); } /// public T FindLast(Predicate match) { return list.FindLast(match); } /// public int FindLastIndex(Predicate match) { return list.FindLastIndex(match); } /// public void Sort() { list.Sort(); } /// public void Sort(IComparer comparer) { list.Sort(comparer); } /// public void Sort(Comparison comparison) { list.Sort(comparison); } /// public void Reverse() { list.Reverse(); } /// /// Converts all elements in the current list to a specified type . /// /// The type to convert the items to, which must implement . /// A delegate that converts elements from type T to type . /// A list containing the converted elements. public ItemList ConvertAll(Converter converter) where TOutput : IItem { ItemList targetList = new ItemList(); foreach (T item in list) { targetList.Add(converter.Invoke(item)); } return targetList; } /// public bool TrueForAll(Predicate match) { return list.TrueForAll(match); } #endregion /// /// Occurs where a new item is added to the list. /// public event EventHandler ItemAdded; /// /// Fires a new ItemAdded event. /// /// Calls . /// The added element. /// The position where the new element was added. protected virtual void OnItemAdded(IItem item, int index) { if (ItemAdded != null) ItemAdded(this, new ItemIndexEventArgs(item, index)); OnChanged(); } /// /// Occurs when an element is deleted from the list. /// public event EventHandler ItemRemoved; /// /// Fires a new ItemRemoved event. /// /// Calls . /// The removed element. /// The position from where the element was removed. protected virtual void OnItemRemoved(IItem item, int index) { if (ItemRemoved != null) ItemRemoved(this, new ItemIndexEventArgs(item, index)); OnChanged(); } /// /// Occurs when the list is emptied. /// public event EventHandler Cleared; /// /// Fires a new Cleared event. /// /// Calls . protected virtual void OnCleared() { if (Cleared != null) Cleared(this, new EventArgs()); OnChanged(); } } }