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