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