Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/ReadOnlyObservableDictionary.cs @ 2867

Last change on this file since 2867 was 2790, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments
  • added additional plugins HeuristicLab.Evolutionary, HeuristicLab.Permutation, HeuristicLab.Selection, and HeuristicLab.Routing.TSP
File size: 6.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;
24using System.Collections.Generic;
25using System.ComponentModel;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Collections {
29  [Serializable]
30  public class ReadOnlyObservableDictionary<TKey, TValue> : IObservableDictionary<TKey, TValue> {
31    [Storable]
32    private IObservableDictionary<TKey, TValue> dict;
33
34    #region Properties
35    public ICollection<TKey> Keys {
36      get { return dict.Keys; }
37    }
38    public ICollection<TValue> Values {
39      get { return dict.Values; }
40    }
41    public int Count {
42      get { return dict.Count; }
43    }
44    bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
45      get { return true; }
46    }
47
48    public TValue this[TKey key] {
49      get { return dict[key]; }
50    }
51    TValue IDictionary<TKey, TValue>.this[TKey key] {
52      get { return dict[key]; }
53      set { throw new NotSupportedException(); }
54    }
55    #endregion
56
57    #region Constructors
58    public ReadOnlyObservableDictionary(IObservableDictionary<TKey, TValue> dictionary) {
59      if (dictionary == null) throw new ArgumentNullException();
60      dict = dictionary;
61      dict.ItemsAdded += new CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>>(dict_ItemsAdded);
62      dict.ItemsRemoved += new CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>>(dict_ItemsRemoved);
63      dict.ItemsReplaced += new CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>>(dict_ItemsReplaced);
64      dict.CollectionReset += new CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>>(dict_CollectionReset);
65      dict.PropertyChanged += new PropertyChangedEventHandler(dict_PropertyChanged);
66    }
67    #endregion
68
69    #region Access
70    public bool ContainsKey(TKey key) {
71      return dict.ContainsKey(key);
72    }
73    bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) {
74      return dict.Contains(item);
75    }
76
77    public bool TryGetValue(TKey key, out TValue value) {
78      return dict.TryGetValue(key, out value);
79    }
80    #endregion
81
82    #region Manipulation
83    void IDictionary<TKey, TValue>.Add(TKey key, TValue value) {
84      throw new NotSupportedException();
85    }
86    void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) {
87      throw new NotSupportedException();
88    }
89
90    bool IDictionary<TKey, TValue>.Remove(TKey key) {
91      throw new NotSupportedException();
92    }
93    bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) {
94      throw new NotSupportedException();
95    }
96
97    void ICollection<KeyValuePair<TKey, TValue>>.Clear() {
98      throw new NotSupportedException();
99    }
100    #endregion
101
102    #region Conversion
103    void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
104      ((ICollection<KeyValuePair<TKey, TValue>>)dict).CopyTo(array, arrayIndex);
105    }
106    #endregion
107
108    #region Enumeration
109    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
110      return dict.GetEnumerator();
111    }
112    IEnumerator IEnumerable.GetEnumerator() {
113      return ((IEnumerable)dict).GetEnumerator();
114    }
115    #endregion
116
117    #region Events
118    [field: NonSerialized]
119    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsAdded;
120    protected virtual void OnItemsAdded(IEnumerable<KeyValuePair<TKey, TValue>> items) {
121      if (ItemsAdded != null)
122        ItemsAdded(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
123    }
124
125    [field: NonSerialized]
126    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsRemoved;
127    protected virtual void OnItemsRemoved(IEnumerable<KeyValuePair<TKey, TValue>> items) {
128      if (ItemsRemoved != null)
129        ItemsRemoved(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
130    }
131
132    [field: NonSerialized]
133    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsReplaced;
134    protected virtual void OnItemsReplaced(IEnumerable<KeyValuePair<TKey, TValue>> items, IEnumerable<KeyValuePair<TKey, TValue>> oldItems) {
135      if (ItemsReplaced != null)
136        ItemsReplaced(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
137    }
138
139    [field: NonSerialized]
140    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> CollectionReset;
141    protected virtual void OnCollectionReset(IEnumerable<KeyValuePair<TKey, TValue>> items, IEnumerable<KeyValuePair<TKey, TValue>> oldItems) {
142      if (CollectionReset != null)
143        CollectionReset(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
144    }
145
146    [field: NonSerialized]
147    public event PropertyChangedEventHandler PropertyChanged;
148    protected virtual void OnPropertyChanged(string propertyName) {
149      if (PropertyChanged != null)
150        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
151    }
152
153    private void dict_ItemsAdded(object sender, CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>> e) {
154      OnItemsAdded(e.Items);
155    }
156    private void dict_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>> e) {
157      OnItemsRemoved(e.Items);
158    }
159    private void dict_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>> e) {
160      OnItemsReplaced(e.Items, e.OldItems);
161    }
162    private void dict_CollectionReset(object sender, CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>> e) {
163      OnCollectionReset(e.Items, e.OldItems);
164    }
165    private void dict_PropertyChanged(object sender, PropertyChangedEventArgs e) {
166      if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Keys") || e.PropertyName.Equals("Values") || e.PropertyName.Equals("Count"))
167        OnPropertyChanged(e.PropertyName);
168    }
169    #endregion
170  }
171}
Note: See TracBrowser for help on using the repository browser.