[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++)
|
---|
| 49 | destination.list.Add((T)Auxiliary.Clone(list[i],clonedObjects));
|
---|
| 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
|
---|
| 142 | public void AddRange(IEnumerable<T> collection) {
|
---|
| 143 | foreach (T obj in collection) {
|
---|
| 144 | this.Add(obj);
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | public bool Exists(Predicate<T> match) {
|
---|
| 149 | return list.Exists(match);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[60] | 152 | public T Find(Predicate<T> match) {
|
---|
| 153 | return list.Find(match);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[53] | 156 | public List<T> FindAll(Predicate<T> match) {
|
---|
| 157 | return list.FindAll(match);
|
---|
| 158 | }
|
---|
[60] | 159 |
|
---|
| 160 | public int FindIndex(Predicate<T> match) {
|
---|
| 161 | return list.FindIndex(match);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | public void Sort() {
|
---|
| 165 | list.Sort();
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | public void Sort(IComparer<T> comparer) {
|
---|
| 169 | list.Sort(comparer);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | public void Sort(Comparison<T> comparison) {
|
---|
| 173 | list.Sort(comparison);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | public void Reverse() {
|
---|
| 177 | list.Reverse();
|
---|
| 178 | }
|
---|
[53] | 179 | #endregion
|
---|
| 180 |
|
---|
[2] | 181 | public event EventHandler<ItemIndexEventArgs> ItemAdded;
|
---|
| 182 | protected virtual void OnItemAdded(IItem item, int index) {
|
---|
| 183 | if (ItemAdded != null)
|
---|
| 184 | ItemAdded(this, new ItemIndexEventArgs(item, index));
|
---|
| 185 | OnChanged();
|
---|
| 186 | }
|
---|
| 187 | public event EventHandler<ItemIndexEventArgs> ItemRemoved;
|
---|
| 188 | protected virtual void OnItemRemoved(IItem item, int index) {
|
---|
| 189 | if (ItemRemoved != null)
|
---|
| 190 | ItemRemoved(this, new ItemIndexEventArgs(item, index));
|
---|
| 191 | OnChanged();
|
---|
| 192 | }
|
---|
| 193 | public event EventHandler Cleared;
|
---|
| 194 | protected virtual void OnCleared() {
|
---|
| 195 | if (Cleared != null)
|
---|
| 196 | Cleared(this, new EventArgs());
|
---|
| 197 | OnChanged();
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 | }
|
---|