Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2965_CancelablePersistence/HeuristicLab.Collections/3.3/ReadOnlyObservableKeyedList.cs @ 16605

Last change on this file since 16605 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25
26namespace HeuristicLab.Collections {
27  [StorableClass]
28  [Serializable]
29  public class ReadOnlyObservableKeyedList<TKey, TItem> : ReadOnlyObservableList<TItem>, IObservableKeyedList<TKey, TItem> {
30
31    protected IObservableKeyedList<TKey, TItem> KeyedList {
32      get { return (IObservableKeyedList<TKey, TItem>)base.list; }
33    }
34
35    bool ICollection<TItem>.IsReadOnly {
36      get { return true; }
37    }
38    public TItem this[TKey key] {
39      get { return KeyedList[key]; }
40    }
41
42    #region Constructors
43    protected ReadOnlyObservableKeyedList() { }
44    public ReadOnlyObservableKeyedList(IObservableKeyedList<TKey, TItem> keyedList)
45      : base(keyedList) {
46      RegisterEvents();
47    }
48    [StorableConstructor]
49    protected ReadOnlyObservableKeyedList(bool deserializing) { }
50    [StorableHook(HookType.AfterDeserialization)]
51    private void AfterDeserialization() {
52      RegisterEvents();
53    }
54    #endregion
55
56    public bool ContainsKey(TKey key) {
57      return KeyedList.ContainsKey(key);
58    }
59    public bool TryGetValue(TKey key, out TItem item) {
60      return KeyedList.TryGetValue(key, out item);
61    }
62
63    bool IObservableKeyedCollection<TKey, TItem>.Remove(TKey key) {
64      throw new NotSupportedException();
65    }
66
67    #region events
68    private void RegisterEvents() {
69      ((INotifyObservableKeyedCollectionItemsChanged<TKey, TItem>)KeyedList).ItemsReplaced += keyedList_ItemsReplaced;
70    }
71
72    [field: NonSerialized]
73    private event CollectionItemsChangedEventHandler<TItem> itemsReplaced;
74    event CollectionItemsChangedEventHandler<TItem> INotifyObservableKeyedCollectionItemsChanged<TKey, TItem>.ItemsReplaced {
75      add { itemsReplaced += value; }
76      remove { itemsReplaced -= value; }
77    }
78    private void OnItemsReplaced(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
79      var handler = itemsReplaced;
80      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
81    }
82
83    private void keyedList_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<TItem> e) {
84      OnItemsReplaced(e.Items, e.OldItems);
85    }
86
87    #endregion
88  }
89}
Note: See TracBrowser for help on using the repository browser.