[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections;
|
---|
| 24 | using System.Collections.Generic;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using System.Xml;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Data {
|
---|
[40] | 30 | public class ItemList<T> : ItemBase, IList<T> where T : IItem {
|
---|
[68] | 31 | private List<T> list;
|
---|
[2] | 32 |
|
---|
| 33 | public ItemList() {
|
---|
[40] | 34 | list = new List<T>();
|
---|
[2] | 35 | }
|
---|
| 36 |
|
---|
| 37 | public override IView CreateView() {
|
---|
[40] | 38 | return new ItemListView<T>(this);
|
---|
[2] | 39 | }
|
---|
| 40 |
|
---|
| 41 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
[40] | 42 | ItemList<T> clone = new ItemList<T>();
|
---|
[2] | 43 | clonedObjects.Add(Guid, clone);
|
---|
[68] | 44 | CloneElements(clone, clonedObjects);
|
---|
[2] | 45 | return clone;
|
---|
| 46 | }
|
---|
[68] | 47 | protected void CloneElements(ItemList<T> destination, IDictionary<Guid, object> clonedObjects) {
|
---|
| 48 | for (int i = 0; i < list.Count; i++)
|
---|
[69] | 49 | destination.list.Add((T) Auxiliary.Clone(list[i], clonedObjects));
|
---|
[68] | 50 | }
|
---|
[2] | 51 |
|
---|
[53] | 52 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
[2] | 53 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
| 54 | for (int i = 0; i < list.Count; i++)
|
---|
| 55 | node.AppendChild(PersistenceManager.Persist(list[i], document, persistedObjects));
|
---|
| 56 | return node;
|
---|
| 57 | }
|
---|
[53] | 58 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
[2] | 59 | base.Populate(node, restoredObjects);
|
---|
| 60 | for (int i = 0; i < node.ChildNodes.Count; i++)
|
---|
[53] | 61 | list.Add((T) PersistenceManager.Restore(node.ChildNodes[i], restoredObjects));
|
---|
[2] | 62 | }
|
---|
| 63 |
|
---|
| 64 | public override string ToString() {
|
---|
| 65 | if (list.Count > 0) {
|
---|
| 66 | StringBuilder builder = new StringBuilder();
|
---|
| 67 | builder.Append(list[0].ToString());
|
---|
| 68 | for (int i = 1; i < list.Count; i++) {
|
---|
| 69 | builder.Append(";");
|
---|
| 70 | builder.Append(list[i].ToString());
|
---|
| 71 | }
|
---|
| 72 | return builder.ToString();
|
---|
| 73 | } else {
|
---|
| 74 | return "Empty List";
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[40] | 78 | #region IList<T> Members
|
---|
| 79 | public int IndexOf(T item) {
|
---|
[2] | 80 | return list.IndexOf(item);
|
---|
| 81 | }
|
---|
[40] | 82 | public void Insert(int index, T item) {
|
---|
[2] | 83 | list.Insert(index, item);
|
---|
| 84 | OnItemAdded(item, index);
|
---|
| 85 | }
|
---|
| 86 | public void RemoveAt(int index) {
|
---|
| 87 | IItem item = list[index];
|
---|
| 88 | list.RemoveAt(index);
|
---|
| 89 | OnItemRemoved(item, index);
|
---|
| 90 | }
|
---|
[40] | 91 | public T this[int index] {
|
---|
[2] | 92 | get { return list[index]; }
|
---|
[40] | 93 | set { list[index] = value; }
|
---|
[2] | 94 | }
|
---|
| 95 | #endregion
|
---|
| 96 |
|
---|
[40] | 97 | #region ICollection<T> Members
|
---|
| 98 | public void Add(T item) {
|
---|
[2] | 99 | list.Add(item);
|
---|
| 100 | OnItemAdded(item, list.Count - 1);
|
---|
| 101 | }
|
---|
| 102 | public void Clear() {
|
---|
| 103 | list.Clear();
|
---|
| 104 | OnCleared();
|
---|
| 105 | }
|
---|
[40] | 106 | public bool Contains(T item) {
|
---|
[2] | 107 | return list.Contains(item);
|
---|
| 108 | }
|
---|
[40] | 109 | public void CopyTo(T[] array, int arrayIndex) {
|
---|
[2] | 110 | list.CopyTo(array, arrayIndex);
|
---|
| 111 | }
|
---|
| 112 | public int Count {
|
---|
| 113 | get { return list.Count; }
|
---|
| 114 | }
|
---|
| 115 | public bool IsReadOnly {
|
---|
| 116 | get { return false; }
|
---|
| 117 | }
|
---|
[40] | 118 | public bool Remove(T item) {
|
---|
[2] | 119 | int index = list.IndexOf(item);
|
---|
| 120 | if (list.Remove(item)) {
|
---|
| 121 | OnItemRemoved(item, index);
|
---|
| 122 | return true;
|
---|
| 123 | } else {
|
---|
| 124 | return false;
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | #endregion
|
---|
| 128 |
|
---|
[40] | 129 | #region IEnumerable<T> Members
|
---|
| 130 | public IEnumerator<T> GetEnumerator() {
|
---|
[2] | 131 | return list.GetEnumerator();
|
---|
| 132 | }
|
---|
| 133 | #endregion
|
---|
| 134 |
|
---|
| 135 | #region IEnumerable Members
|
---|
| 136 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 137 | return list.GetEnumerator();
|
---|
| 138 | }
|
---|
| 139 | #endregion
|
---|
| 140 |
|
---|
[53] | 141 | #region List<T> Methods
|
---|
[69] | 142 | public int LastIndexOf(T item) {
|
---|
| 143 | return list.LastIndexOf(item);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | public int LastIndexOf(T item, int index) {
|
---|
| 147 | return list.LastIndexOf(item, index);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | public int LastIndexOf(T item, int index, int count) {
|
---|
| 151 | return list.LastIndexOf(item, index, count);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | public int IndexOf(T item, int index) {
|
---|
| 155 | return list.IndexOf(item, index);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | public int IndexOf(T item, int index, int count) {
|
---|
| 159 | return list.IndexOf(item, index, count);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[53] | 162 | public void AddRange(IEnumerable<T> collection) {
|
---|
| 163 | foreach (T obj in collection) {
|
---|
| 164 | this.Add(obj);
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | public bool Exists(Predicate<T> match) {
|
---|
| 169 | return list.Exists(match);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[69] | 172 | public int BinarySearch(T item) {
|
---|
| 173 | return list.BinarySearch(item);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | public int BinarySearch(T item, IComparer<T> comparer) {
|
---|
| 177 | return list.BinarySearch(item, comparer);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | public int BinarySearch(int index, int count, T item, IComparer<T> comparer) {
|
---|
| 181 | return list.BinarySearch(index, count, item, comparer);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[60] | 184 | public T Find(Predicate<T> match) {
|
---|
| 185 | return list.Find(match);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[53] | 188 | public List<T> FindAll(Predicate<T> match) {
|
---|
| 189 | return list.FindAll(match);
|
---|
| 190 | }
|
---|
[60] | 191 |
|
---|
| 192 | public int FindIndex(Predicate<T> match) {
|
---|
[69] | 193 | return list.FindIndex(match);
|
---|
[60] | 194 | }
|
---|
| 195 |
|
---|
[69] | 196 | public T FindLast(Predicate<T> match) {
|
---|
| 197 | return list.FindLast(match);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | public int FindLastIndex(Predicate<T> match) {
|
---|
| 201 | return list.FindLastIndex(match);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[60] | 204 | public void Sort() {
|
---|
[69] | 205 | list.Sort();
|
---|
[60] | 206 | }
|
---|
| 207 |
|
---|
| 208 | public void Sort(IComparer<T> comparer) {
|
---|
[69] | 209 | list.Sort(comparer);
|
---|
[60] | 210 | }
|
---|
| 211 |
|
---|
| 212 | public void Sort(Comparison<T> comparison) {
|
---|
[69] | 213 | list.Sort(comparison);
|
---|
| 214 | }
|
---|
[60] | 215 |
|
---|
| 216 | public void Reverse() {
|
---|
[69] | 217 | list.Reverse();
|
---|
[60] | 218 | }
|
---|
[69] | 219 |
|
---|
| 220 | public ItemList<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter) where TOutput : IItem {
|
---|
| 221 | ItemList<TOutput> targetList = new ItemList<TOutput>();
|
---|
| 222 | foreach (T item in list) {
|
---|
| 223 | targetList.Add(converter.Invoke(item));
|
---|
| 224 | }
|
---|
| 225 | return targetList;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | public bool TrueForAll(Predicate<T> match) {
|
---|
| 229 | return list.TrueForAll(match);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[53] | 232 | #endregion
|
---|
| 233 |
|
---|
[2] | 234 | public event EventHandler<ItemIndexEventArgs> ItemAdded;
|
---|
| 235 | protected virtual void OnItemAdded(IItem item, int index) {
|
---|
| 236 | if (ItemAdded != null)
|
---|
| 237 | ItemAdded(this, new ItemIndexEventArgs(item, index));
|
---|
| 238 | OnChanged();
|
---|
| 239 | }
|
---|
| 240 | public event EventHandler<ItemIndexEventArgs> ItemRemoved;
|
---|
| 241 | protected virtual void OnItemRemoved(IItem item, int index) {
|
---|
| 242 | if (ItemRemoved != null)
|
---|
| 243 | ItemRemoved(this, new ItemIndexEventArgs(item, index));
|
---|
| 244 | OnChanged();
|
---|
| 245 | }
|
---|
| 246 | public event EventHandler Cleared;
|
---|
| 247 | protected virtual void OnCleared() {
|
---|
| 248 | if (Cleared != null)
|
---|
| 249 | Cleared(this, new EventArgs());
|
---|
| 250 | OnChanged();
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
| 253 | }
|
---|