1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2009 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.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using System.Xml;
|
---|
27 | using System.Collections;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Common {
|
---|
30 | public class NotificationDictionary<TKey, TValue> : IDictionary<TKey, TValue> {
|
---|
31 |
|
---|
32 | public NotificationDictionary() {
|
---|
33 | this.myDictionary = new Dictionary<TKey, TValue>();
|
---|
34 | }
|
---|
35 |
|
---|
36 | #region Event Handler
|
---|
37 |
|
---|
38 | public event EventHandler BeforeInsertEvent;
|
---|
39 | public event EventHandler AfterInsertEvent;
|
---|
40 | public event EventHandler BeforeRemoveEvent;
|
---|
41 | public event EventHandler AfterRemoveEvent;
|
---|
42 | public event EventHandler ChangeEvent;
|
---|
43 | public event EventHandler ClearEvent;
|
---|
44 |
|
---|
45 | private Dictionary<TKey, TValue> myDictionary;
|
---|
46 |
|
---|
47 | protected virtual void OnBeforeInsert() {
|
---|
48 | if (BeforeInsertEvent != null)
|
---|
49 | BeforeInsertEvent(this, new EventArgs());
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected virtual void OnAfterInsert() {
|
---|
53 | if (AfterInsertEvent != null)
|
---|
54 | AfterInsertEvent(this, new EventArgs());
|
---|
55 | }
|
---|
56 |
|
---|
57 | protected virtual void OnBeforeRemove() {
|
---|
58 | if (BeforeRemoveEvent != null)
|
---|
59 | BeforeRemoveEvent(this, new EventArgs());
|
---|
60 | }
|
---|
61 |
|
---|
62 | protected virtual void OnAfterRemove() {
|
---|
63 | if (AfterRemoveEvent != null)
|
---|
64 | AfterRemoveEvent(this, new EventArgs());
|
---|
65 | }
|
---|
66 |
|
---|
67 | protected virtual void OnChange() {
|
---|
68 | if (ChangeEvent != null)
|
---|
69 | ChangeEvent(this, new EventArgs());
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected virtual void OnClear() {
|
---|
73 | if (ClearEvent != null)
|
---|
74 | ClearEvent(this, new EventArgs());
|
---|
75 | }
|
---|
76 |
|
---|
77 | #endregion
|
---|
78 |
|
---|
79 | #region IDictionary<TKey,TValue> Members
|
---|
80 |
|
---|
81 | public void Add(TKey key, TValue value) {
|
---|
82 | OnBeforeInsert();
|
---|
83 | myDictionary.Add(key, value);
|
---|
84 | OnAfterInsert();
|
---|
85 | }
|
---|
86 |
|
---|
87 | public bool ContainsKey(TKey key) {
|
---|
88 | return myDictionary.ContainsKey(key);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public ICollection<TKey> Keys {
|
---|
92 | get { return myDictionary.Keys; }
|
---|
93 | }
|
---|
94 |
|
---|
95 | public bool Remove(TKey key) {
|
---|
96 | OnBeforeRemove();
|
---|
97 | bool returnVal = myDictionary.Remove(key);
|
---|
98 | OnAfterRemove();
|
---|
99 | return returnVal;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public bool TryGetValue(TKey key, out TValue value) {
|
---|
103 | return TryGetValue(key, out value);
|
---|
104 | }
|
---|
105 |
|
---|
106 | public ICollection<TValue> Values {
|
---|
107 | get { return myDictionary.Values; }
|
---|
108 | }
|
---|
109 |
|
---|
110 | public TValue this[TKey key] {
|
---|
111 | get {
|
---|
112 | return myDictionary[key];
|
---|
113 | }
|
---|
114 | set {
|
---|
115 | if (!value.Equals(myDictionary[key])) {
|
---|
116 | myDictionary[key] = value;
|
---|
117 | OnChange();
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | #endregion
|
---|
123 |
|
---|
124 | #region ICollection<KeyValuePair<TKey,TValue>> Members
|
---|
125 |
|
---|
126 | public void Add(KeyValuePair<TKey, TValue> item) {
|
---|
127 | OnBeforeInsert();
|
---|
128 | myDictionary.Add(item.Key, item.Value);
|
---|
129 | OnAfterInsert();
|
---|
130 | }
|
---|
131 |
|
---|
132 | public void Clear() {
|
---|
133 | myDictionary.Clear();
|
---|
134 | OnClear();
|
---|
135 | }
|
---|
136 |
|
---|
137 | public bool Contains(KeyValuePair<TKey, TValue> item) {
|
---|
138 | return myDictionary.Contains(item);
|
---|
139 | }
|
---|
140 |
|
---|
141 | public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
|
---|
142 | ((IDictionary<TKey, TValue>)myDictionary).CopyTo(array, arrayIndex);
|
---|
143 | }
|
---|
144 |
|
---|
145 | public int Count {
|
---|
146 | get { return myDictionary.Count; }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public bool IsReadOnly {
|
---|
150 | get { return false; }
|
---|
151 | }
|
---|
152 |
|
---|
153 | public bool Remove(KeyValuePair<TKey, TValue> item) {
|
---|
154 | OnBeforeRemove();
|
---|
155 | bool returnVal = myDictionary.Remove(item.Key);
|
---|
156 | OnAfterRemove();
|
---|
157 | return returnVal;
|
---|
158 | }
|
---|
159 |
|
---|
160 | #endregion
|
---|
161 |
|
---|
162 | #region IEnumerable<KeyValuePair<TKey,TValue>> Members
|
---|
163 |
|
---|
164 | public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
|
---|
165 | return myDictionary.GetEnumerator();
|
---|
166 | }
|
---|
167 |
|
---|
168 | #endregion
|
---|
169 |
|
---|
170 | #region IEnumerable Members
|
---|
171 |
|
---|
172 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
|
---|
173 | return myDictionary.GetEnumerator();
|
---|
174 | }
|
---|
175 |
|
---|
176 | #endregion
|
---|
177 | }
|
---|
178 | }
|
---|