1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.ComponentModel;
|
---|
26 | using System.Linq;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Collections {
|
---|
30 | [Serializable]
|
---|
31 | public class ObservableDictionary<TKey, TValue> : IObservableDictionary<TKey, TValue> {
|
---|
32 | [Storable]
|
---|
33 | private Dictionary<TKey, TValue> dict;
|
---|
34 |
|
---|
35 | #region Properties
|
---|
36 | public ICollection<TKey> Keys {
|
---|
37 | get { return dict.Keys; }
|
---|
38 | }
|
---|
39 | public ICollection<TValue> Values {
|
---|
40 | get { return dict.Values; }
|
---|
41 | }
|
---|
42 | public int Count {
|
---|
43 | get { return dict.Count; }
|
---|
44 | }
|
---|
45 | public IEqualityComparer<TKey> Comparer {
|
---|
46 | get { return dict.Comparer; }
|
---|
47 | }
|
---|
48 | bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
|
---|
49 | get { return ((ICollection<KeyValuePair<TKey, TValue>>)dict).IsReadOnly; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public TValue this[TKey key] {
|
---|
53 | get {
|
---|
54 | return dict[key];
|
---|
55 | }
|
---|
56 | set {
|
---|
57 | if (dict.ContainsKey(key)) {
|
---|
58 | KeyValuePair<TKey, TValue> item = new KeyValuePair<TKey, TValue>(key, dict[key]);
|
---|
59 | dict[key] = value;
|
---|
60 | OnItemsReplaced(new KeyValuePair<TKey, TValue>[] { new KeyValuePair<TKey, TValue>(key, value) }, new KeyValuePair<TKey, TValue>[] { item });
|
---|
61 | } else {
|
---|
62 | dict[key] = value;
|
---|
63 | OnItemsAdded(new KeyValuePair<TKey, TValue>[] { new KeyValuePair<TKey, TValue>(key, value) });
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 | #endregion
|
---|
68 |
|
---|
69 | #region Constructors
|
---|
70 | public ObservableDictionary() {
|
---|
71 | dict = new Dictionary<TKey, TValue>();
|
---|
72 | }
|
---|
73 | public ObservableDictionary(int capacity) {
|
---|
74 | dict = new Dictionary<TKey, TValue>(capacity);
|
---|
75 | }
|
---|
76 | public ObservableDictionary(IEqualityComparer<TKey> comparer) {
|
---|
77 | dict = new Dictionary<TKey, TValue>(comparer);
|
---|
78 | }
|
---|
79 | public ObservableDictionary(IDictionary<TKey, TValue> dictionary) {
|
---|
80 | dict = new Dictionary<TKey, TValue>(dictionary);
|
---|
81 | }
|
---|
82 | public ObservableDictionary(int capacity, IEqualityComparer<TKey> comparer) {
|
---|
83 | dict = new Dictionary<TKey, TValue>(capacity, comparer);
|
---|
84 | }
|
---|
85 | public ObservableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) {
|
---|
86 | dict = new Dictionary<TKey, TValue>(dictionary, comparer);
|
---|
87 | }
|
---|
88 | #endregion
|
---|
89 |
|
---|
90 | #region Access
|
---|
91 | public bool ContainsKey(TKey key) {
|
---|
92 | return dict.ContainsKey(key);
|
---|
93 | }
|
---|
94 | public bool ContainsValue(TValue value) {
|
---|
95 | return dict.ContainsValue(value);
|
---|
96 | }
|
---|
97 | bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) {
|
---|
98 | return dict.Contains(item);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public bool TryGetValue(TKey key, out TValue value) {
|
---|
102 | return dict.TryGetValue(key, out value);
|
---|
103 | }
|
---|
104 | #endregion
|
---|
105 |
|
---|
106 | #region Manipulation
|
---|
107 | public void Add(TKey key, TValue value) {
|
---|
108 | dict.Add(key, value);
|
---|
109 | OnPropertyChanged("Item[]");
|
---|
110 | OnPropertyChanged("Keys");
|
---|
111 | OnPropertyChanged("Values");
|
---|
112 | OnPropertyChanged("Count");
|
---|
113 | OnItemsAdded(new KeyValuePair<TKey, TValue>[] { new KeyValuePair<TKey, TValue>(key, value) });
|
---|
114 | }
|
---|
115 | void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) {
|
---|
116 | ((ICollection<KeyValuePair<TKey, TValue>>)dict).Add(item);
|
---|
117 | OnPropertyChanged("Item[]");
|
---|
118 | OnPropertyChanged("Keys");
|
---|
119 | OnPropertyChanged("Values");
|
---|
120 | OnPropertyChanged("Count");
|
---|
121 | OnItemsAdded(new KeyValuePair<TKey, TValue>[] { item });
|
---|
122 | }
|
---|
123 |
|
---|
124 | public bool Remove(TKey key) {
|
---|
125 | TValue value;
|
---|
126 | if (dict.TryGetValue(key, out value)) {
|
---|
127 | dict.Remove(key);
|
---|
128 | OnPropertyChanged("Item[]");
|
---|
129 | OnPropertyChanged("Keys");
|
---|
130 | OnPropertyChanged("Values");
|
---|
131 | OnPropertyChanged("Count");
|
---|
132 | OnItemsRemoved(new KeyValuePair<TKey, TValue>[] { new KeyValuePair<TKey, TValue>(key, value) });
|
---|
133 | return true;
|
---|
134 | }
|
---|
135 | return false;
|
---|
136 | }
|
---|
137 | bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) {
|
---|
138 | if (((ICollection<KeyValuePair<TKey, TValue>>)dict).Remove(item)) {
|
---|
139 | OnPropertyChanged("Item[]");
|
---|
140 | OnPropertyChanged("Keys");
|
---|
141 | OnPropertyChanged("Values");
|
---|
142 | OnPropertyChanged("Count");
|
---|
143 | OnItemsRemoved(new KeyValuePair<TKey, TValue>[] { item });
|
---|
144 | return true;
|
---|
145 | }
|
---|
146 | return false;
|
---|
147 | }
|
---|
148 |
|
---|
149 | public void Clear() {
|
---|
150 | if (dict.Count > 0) {
|
---|
151 | KeyValuePair<TKey, TValue>[] items = dict.ToArray();
|
---|
152 | dict.Clear();
|
---|
153 | OnPropertyChanged("Item[]");
|
---|
154 | OnPropertyChanged("Keys");
|
---|
155 | OnPropertyChanged("Values");
|
---|
156 | OnPropertyChanged("Count");
|
---|
157 | OnCollectionReset(new KeyValuePair<TKey, TValue>[0], items);
|
---|
158 | }
|
---|
159 | }
|
---|
160 | #endregion
|
---|
161 |
|
---|
162 | #region Conversion
|
---|
163 | public ReadOnlyObservableDictionary<TKey, TValue> AsReadOnly() {
|
---|
164 | return new ReadOnlyObservableDictionary<TKey, TValue>(this);
|
---|
165 | }
|
---|
166 | void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
|
---|
167 | ((ICollection<KeyValuePair<TKey, TValue>>)dict).CopyTo(array, arrayIndex);
|
---|
168 | }
|
---|
169 | #endregion
|
---|
170 |
|
---|
171 | #region Enumeration
|
---|
172 | public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
|
---|
173 | return dict.GetEnumerator();
|
---|
174 | }
|
---|
175 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
176 | return dict.GetEnumerator();
|
---|
177 | }
|
---|
178 | #endregion
|
---|
179 |
|
---|
180 | #region Events
|
---|
181 | [field: NonSerialized]
|
---|
182 | public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsAdded;
|
---|
183 | protected virtual void OnItemsAdded(IEnumerable<KeyValuePair<TKey, TValue>> items) {
|
---|
184 | if (ItemsAdded != null)
|
---|
185 | ItemsAdded(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
|
---|
186 | }
|
---|
187 |
|
---|
188 | [field: NonSerialized]
|
---|
189 | public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsRemoved;
|
---|
190 | protected virtual void OnItemsRemoved(IEnumerable<KeyValuePair<TKey, TValue>> items) {
|
---|
191 | if (ItemsRemoved != null)
|
---|
192 | ItemsRemoved(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
|
---|
193 | }
|
---|
194 |
|
---|
195 | [field: NonSerialized]
|
---|
196 | public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsReplaced;
|
---|
197 | protected virtual void OnItemsReplaced(IEnumerable<KeyValuePair<TKey, TValue>> items, IEnumerable<KeyValuePair<TKey, TValue>> oldItems) {
|
---|
198 | if (ItemsReplaced != null)
|
---|
199 | ItemsReplaced(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
|
---|
200 | }
|
---|
201 |
|
---|
202 | [field: NonSerialized]
|
---|
203 | public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> CollectionReset;
|
---|
204 | protected virtual void OnCollectionReset(IEnumerable<KeyValuePair<TKey, TValue>> items, IEnumerable<KeyValuePair<TKey, TValue>> oldItems) {
|
---|
205 | if (CollectionReset != null)
|
---|
206 | CollectionReset(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
|
---|
207 | }
|
---|
208 |
|
---|
209 | [field: NonSerialized]
|
---|
210 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
211 | protected virtual void OnPropertyChanged(string propertyName) {
|
---|
212 | if (PropertyChanged != null)
|
---|
213 | PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
---|
214 | }
|
---|
215 | #endregion
|
---|
216 | }
|
---|
217 | }
|
---|