Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3500 was 2793, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

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