Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Data/ItemDictionary_T.cs @ 12391

Last change on this file since 12391 was 187, checked in by mkofler, 17 years ago

Added ItemDictionary and related classes (View, KeyValueEventArgs, EditKeyValueDialog) to the Core and Data projects as per Andreas' request.

File size: 5.9 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Text;
5using System.Xml;
6using HeuristicLab.Core;
7
8namespace HeuristicLab.Data {
9  public class ItemDictionary<K,V> : ItemBase, IDictionary<K,V>
10    where K : IItem
11    where V : IItem{
12    private Dictionary<K, V> dict;
13
14    public Dictionary<K, V> Dictionary {
15      get { return dict; }
16    }
17
18    public ItemDictionary() {
19      dict = new Dictionary<K, V>(new IItemKeyComparer<K>());
20    }
21
22    #region ItemBase Members
23    public override IView CreateView() {
24      return new ItemDictionaryView<K,V>(this);
25    }
26
27    public override object Clone(IDictionary<Guid, object> clonedObjects) {
28      ItemDictionary<K,V> clone = new ItemDictionary<K,V>();
29      clonedObjects.Add(Guid, clone);
30      foreach (KeyValuePair<K, V> item in dict) {
31        clone.dict.Add((K) Auxiliary.Clone(item.Key, clonedObjects), (V) Auxiliary.Clone(item.Value, clonedObjects));
32      }
33      return clone;
34    }
35
36    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
37      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
38      foreach (KeyValuePair<K, V> item in dict) {
39        XmlNode keyNode = PersistenceManager.Persist("Key", item.Key, document, persistedObjects);
40        XmlNode valueNode = PersistenceManager.Persist("Val", item.Value, document, persistedObjects);
41        XmlNode pairNode = document.CreateNode(XmlNodeType.Element, "KeyValuePair", null);
42        pairNode.AppendChild(keyNode);
43        pairNode.AppendChild(valueNode);
44        node.AppendChild(pairNode);
45      }
46      return node;
47    }
48
49    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
50      base.Populate(node, restoredObjects);
51      for (int i = 0; i < node.ChildNodes.Count; i++) {
52        K key = (K) PersistenceManager.Restore(node.ChildNodes[i].SelectSingleNode("Key"), restoredObjects);
53        V val = (V) PersistenceManager.Restore(node.ChildNodes[i].SelectSingleNode("Val"), restoredObjects);
54        dict[key] = val;
55      }
56    }
57
58    public override string ToString() {
59      if (dict.Count > 0) {
60        StringBuilder builder = new StringBuilder();
61        foreach (KeyValuePair<K, V> item in dict) {
62          builder.Append(item.Key.ToString());
63          builder.Append(":");
64          builder.Append(item.Value.ToString());
65          builder.Append("; ");
66        }
67        return builder.ToString();
68      } else {
69        return "Empty Dictionary";
70      }
71    }
72    #endregion
73
74    #region IDictionary<K,V> Members
75
76    public void Add(K key, V value) {
77      dict.Add(key, value);
78      OnItemAdded(key, value);
79    }
80
81    public bool ContainsKey(K key) {
82      return dict.ContainsKey(key);
83    }
84
85    public ICollection<K> Keys {
86      get { return dict.Keys; }
87    }
88
89    public bool Remove(K key) {
90      V value = dict[key];
91      bool removed = dict.Remove(key);
92      OnItemRemoved(key, value);
93      return removed;
94    }
95
96    public bool TryGetValue(K key, out V value) {
97      return dict.TryGetValue(key, out value);
98    }
99
100    public ICollection<V> Values {
101      get { return dict.Values; }
102    }
103
104    public V this[K key] {
105      get { return dict[key]; }
106      set { dict[key] = value; }
107    }
108
109    #endregion
110
111    #region ICollection<KeyValuePair<K,V>> Members
112
113    public void Add(KeyValuePair<K, V> item) {
114      dict.Add(item.Key, item.Value);
115      OnItemAdded(item.Key, item.Value);
116    }
117
118    public void Clear() {
119      dict.Clear();
120      OnCleared();
121    }
122
123    public bool Contains(KeyValuePair<K, V> item) {
124      return (dict.ContainsKey(item.Key) && dict[item.Key].Equals(item.Value));
125    }
126
127    public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex) {
128      throw new NotImplementedException();
129    }
130
131    public int Count {
132      get { return dict.Count; }
133    }
134
135    public bool IsReadOnly {
136      get { return false; }
137    }
138
139    public bool Remove(KeyValuePair<K, V> item) {
140      bool removed = dict.Remove(item.Key);
141      if (removed) {
142        OnItemRemoved(item.Key, item.Value);
143      }
144      return removed;
145    }
146    #endregion
147
148    #region IEnumerable<KeyValuePair<K,V>> Members
149    public IEnumerator<KeyValuePair<K, V>> GetEnumerator() {
150      return dict.GetEnumerator();
151    }
152    #endregion
153
154    #region IEnumerable Members
155    IEnumerator IEnumerable.GetEnumerator() {
156      return dict.GetEnumerator();
157    }
158    #endregion
159
160    #region Event Handler
161    public event EventHandler<KeyValueEventArgs> ItemAdded;
162    protected virtual void OnItemAdded(K key, V value) {
163      if (ItemAdded != null)
164        ItemAdded(this, new KeyValueEventArgs(key, value));
165      OnChanged();
166    }
167
168    public event EventHandler<KeyValueEventArgs> ItemRemoved;
169    protected virtual void OnItemRemoved(K key, V value) {
170      if (ItemRemoved != null)
171        ItemRemoved(this, new KeyValueEventArgs(key, value));
172      OnChanged();
173    }
174
175    public event EventHandler Cleared;
176    protected virtual void OnCleared() {
177      if (Cleared != null)
178        Cleared(this, new EventArgs());
179      OnChanged();
180    }
181    #endregion
182
183    #region IEqualityComparer
184    internal sealed class IItemKeyComparer<T> : IEqualityComparer<T>
185        where T : IItem {
186      public bool Equals(T x, T y) {
187        if (x is IComparable) {
188          return (((IComparable) x).CompareTo(y) == 0);
189        }
190        if (y is IComparable) {
191          return (((IComparable) y).CompareTo(x) == 0);
192        }
193        return x.Equals(y);
194      }
195
196      public int GetHashCode(T obj) {
197        if (obj is IObjectData) {
198          return ((IObjectData) obj).Data.GetHashCode();
199        }
200        return obj.Guid.GetHashCode();
201      }
202    }
203    #endregion
204  }
205}
Note: See TracBrowser for help on using the repository browser.