Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Common/3.2/NotificationDictionary.cs @ 2345

Last change on this file since 2345 was 2345, checked in by abeham, 15 years ago

Added NotificationDictionary to HeuristicLab.Common (#737)

File size: 4.7 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Xml;
27using System.Collections;
28
29namespace 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    }
85
86    public bool ContainsKey(TKey key) {
87      return myDictionary.ContainsKey(key);
88    }
89
90    public ICollection<TKey> Keys {
91      get { return myDictionary.Keys; }
92    }
93
94    public bool Remove(TKey key) {
95      OnBeforeRemove();
96      bool returnVal = myDictionary.Remove(key);
97      OnAfterRemove();
98      return returnVal;
99    }
100
101    public bool TryGetValue(TKey key, out TValue value) {
102      return TryGetValue(key, out value);
103    }
104
105    public ICollection<TValue> Values {
106      get { return myDictionary.Values; }
107    }
108
109    public TValue this[TKey key] {
110      get {
111        return myDictionary[key];
112      }
113      set {
114        if (!value.Equals(myDictionary[key])) {
115          myDictionary[key] = value;
116          OnChange();
117        }
118      }
119    }
120
121    #endregion
122
123    #region ICollection<KeyValuePair<TKey,TValue>> Members
124
125    public void Add(KeyValuePair<TKey, TValue> item) {
126      OnBeforeInsert();
127      myDictionary.Add(item.Key, item.Value);
128      OnAfterInsert();
129    }
130
131    public void Clear() {
132      myDictionary.Clear();
133      OnClear();
134    }
135
136    public bool Contains(KeyValuePair<TKey, TValue> item) {
137      return myDictionary.Contains(item);
138    }
139
140    public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
141      ((IDictionary<TKey, TValue>)myDictionary).CopyTo(array, arrayIndex);
142    }
143
144    public int Count {
145      get { return myDictionary.Count; }
146    }
147
148    public bool IsReadOnly {
149      get { return false; }
150    }
151
152    public bool Remove(KeyValuePair<TKey, TValue> item) {
153      OnBeforeRemove();
154      bool returnVal = myDictionary.Remove(item.Key);
155      OnAfterRemove();
156      return returnVal;
157    }
158
159    #endregion
160
161    #region IEnumerable<KeyValuePair<TKey,TValue>> Members
162
163    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
164      return myDictionary.GetEnumerator();
165    }
166
167    #endregion
168
169    #region IEnumerable Members
170
171    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
172      return myDictionary.GetEnumerator();
173    }
174
175    #endregion
176  }
177}
Note: See TracBrowser for help on using the repository browser.