Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/ReadOnlyObservableKeyedCollection.cs @ 2790

Last change on this file since 2790 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: 5.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 ReadOnlyObservableKeyedCollection<TKey, TItem> : IObservableKeyedCollection<TKey, TItem> {
31    [Storable]
32    private IObservableKeyedCollection<TKey, TItem> collection;
33
34    #region Properties
35    public int Count {
36      get { return collection.Count; }
37    }
38    bool ICollection<TItem>.IsReadOnly {
39      get { return true; }
40    }
41
42    public TItem this[TKey key] {
43      get {
44        return collection[key];
45      }
46    }
47    #endregion
48
49    #region Constructors
50    public ReadOnlyObservableKeyedCollection(IObservableKeyedCollection<TKey, TItem> collection) {
51      if (collection == null) throw new ArgumentNullException();
52      this.collection = collection;
53      collection.ItemsAdded += new CollectionItemsChangedEventHandler<TItem>(collection_ItemsAdded);
54      collection.ItemsRemoved += new CollectionItemsChangedEventHandler<TItem>(collection_ItemsRemoved);
55      collection.ItemsReplaced += new CollectionItemsChangedEventHandler<TItem>(collection_ItemsReplaced);
56      collection.CollectionReset += new CollectionItemsChangedEventHandler<TItem>(collection_CollectionReset);
57      collection.PropertyChanged += new PropertyChangedEventHandler(collection_PropertyChanged);
58    }
59    #endregion
60
61    #region Access
62    public bool ContainsKey(TKey key) {
63      return collection.ContainsKey(key);
64    }
65    public bool Contains(TItem item) {
66      return collection.Contains(item);
67    }
68
69    public bool TryGetValue(TKey key, out TItem item) {
70      return collection.TryGetValue(key, out item);
71    }
72    #endregion
73
74    #region Manipulation
75    void ICollection<TItem>.Add(TItem item) {
76      throw new NotSupportedException();
77    }
78
79    bool IObservableKeyedCollection<TKey, TItem>.Remove(TKey key) {
80      throw new NotSupportedException();
81    }
82    bool ICollection<TItem>.Remove(TItem item) {
83      throw new NotSupportedException();
84    }
85
86    void ICollection<TItem>.Clear() {
87      throw new NotSupportedException();
88    }
89    #endregion
90
91    #region Conversion
92    public void CopyTo(TItem[] array, int arrayIndex) {
93      collection.CopyTo(array, arrayIndex);
94    }
95    #endregion
96
97    #region Enumeration
98    public IEnumerator<TItem> GetEnumerator() {
99      return ((IEnumerable<TItem>)collection).GetEnumerator();
100    }
101    IEnumerator IEnumerable.GetEnumerator() {
102      return ((IEnumerable)collection).GetEnumerator();
103    }
104    #endregion
105
106    #region Events
107    [field: NonSerialized]
108    public event CollectionItemsChangedEventHandler<TItem> ItemsAdded;
109    protected virtual void OnItemsAdded(IEnumerable<TItem> items) {
110      if (ItemsAdded != null)
111        ItemsAdded(this, new CollectionItemsChangedEventArgs<TItem>(items));
112    }
113
114    [field: NonSerialized]
115    public event CollectionItemsChangedEventHandler<TItem> ItemsRemoved;
116    protected virtual void OnItemsRemoved(IEnumerable<TItem> items) {
117      if (ItemsRemoved != null)
118        ItemsRemoved(this, new CollectionItemsChangedEventArgs<TItem>(items));
119    }
120
121    [field: NonSerialized]
122    public event CollectionItemsChangedEventHandler<TItem> ItemsReplaced;
123    protected virtual void OnItemsReplaced(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
124      if (ItemsReplaced != null)
125        ItemsReplaced(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
126    }
127
128    [field: NonSerialized]
129    public event CollectionItemsChangedEventHandler<TItem> CollectionReset;
130    protected virtual void OnCollectionReset(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
131      if (CollectionReset != null)
132        CollectionReset(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
133    }
134
135    [field: NonSerialized]
136    public event PropertyChangedEventHandler PropertyChanged;
137    protected virtual void OnPropertyChanged(string propertyName) {
138      if (PropertyChanged != null)
139        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
140    }
141
142    private void collection_ItemsAdded(object sender, CollectionItemsChangedEventArgs<TItem> e) {
143      OnItemsAdded(e.Items);
144    }
145    private void collection_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<TItem> e) {
146      OnItemsRemoved(e.Items);
147    }
148    private void collection_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<TItem> e) {
149      OnItemsReplaced(e.Items, e.OldItems);
150    }
151    private void collection_CollectionReset(object sender, CollectionItemsChangedEventArgs<TItem> e) {
152      OnCollectionReset(e.Items, e.OldItems);
153    }
154    private void collection_PropertyChanged(object sender, PropertyChangedEventArgs e) {
155      if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Count"))
156        OnPropertyChanged(e.PropertyName);
157    }
158    #endregion
159  }
160}
Note: See TracBrowser for help on using the repository browser.