#region License Information /* HeuristicLab * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Collections { [StorableClass] [Serializable] public class ReadOnlyObservableSet : IObservableSet { [Storable] protected IObservableSet set; #region Properties public int Count { get { return set.Count; } } bool ICollection.IsReadOnly { get { return true; } } #endregion #region Constructors protected ReadOnlyObservableSet() { } public ReadOnlyObservableSet(IObservableSet set) { if (set == null) throw new ArgumentNullException(); this.set = set; RegisterEvents(); } [StorableConstructor] protected ReadOnlyObservableSet(bool deserializing) { } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { RegisterEvents(); } #endregion #region Access public bool Contains(T item) { return set.Contains(item); } public bool IsProperSubsetOf(IEnumerable other) { return set.IsProperSubsetOf(other); } public bool IsProperSupersetOf(IEnumerable other) { return set.IsProperSupersetOf(other); } public bool IsSubsetOf(IEnumerable other) { return set.IsSubsetOf(other); } public bool IsSupersetOf(IEnumerable other) { return set.IsSupersetOf(other); } public bool Overlaps(IEnumerable other) { return set.Overlaps(other); } public bool SetEquals(IEnumerable other) { return set.SetEquals(other); } #endregion #region Manipulation bool IObservableSet.Add(T item) { throw new NotSupportedException(); } void ICollection.Add(T item) { throw new NotSupportedException(); } void IObservableSet.ExceptWith(IEnumerable other) { throw new NotSupportedException(); } void IObservableSet.IntersectWith(IEnumerable other) { throw new NotSupportedException(); } bool ICollection.Remove(T item) { throw new NotSupportedException(); } void IObservableSet.SymmetricExceptWith(IEnumerable other) { throw new NotSupportedException(); } void IObservableSet.UnionWith(IEnumerable other) { throw new NotSupportedException(); } void ICollection.Clear() { throw new NotSupportedException(); } #endregion #region Conversion public void CopyTo(T[] array, int arrayIndex) { set.CopyTo(array, arrayIndex); } #endregion #region Enumeration public IEnumerator GetEnumerator() { return set.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return set.GetEnumerator(); } #endregion #region Events protected void RegisterEvents() { set.ItemsAdded += new CollectionItemsChangedEventHandler(set_ItemsAdded); set.ItemsRemoved += new CollectionItemsChangedEventHandler(set_ItemsRemoved); set.CollectionReset += new CollectionItemsChangedEventHandler(set_CollectionReset); set.PropertyChanged += new PropertyChangedEventHandler(set_PropertyChanged); } [field: NonSerialized] public event CollectionItemsChangedEventHandler ItemsAdded; protected virtual void OnItemsAdded(IEnumerable items) { CollectionItemsChangedEventHandler handler = ItemsAdded; if (handler != null) handler(this, new CollectionItemsChangedEventArgs(items)); } [field: NonSerialized] public event CollectionItemsChangedEventHandler ItemsRemoved; protected virtual void OnItemsRemoved(IEnumerable items) { CollectionItemsChangedEventHandler handler = ItemsRemoved; if (handler != null) handler(this, new CollectionItemsChangedEventArgs(items)); } [field: NonSerialized] public event CollectionItemsChangedEventHandler CollectionReset; protected virtual void OnCollectionReset(IEnumerable items, IEnumerable oldItems) { CollectionItemsChangedEventHandler handler = CollectionReset; if (handler != null) handler(this, new CollectionItemsChangedEventArgs(items, oldItems)); } [field: NonSerialized] public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } private void set_ItemsAdded(object sender, CollectionItemsChangedEventArgs e) { OnItemsAdded(e.Items); } private void set_ItemsRemoved(object sender, CollectionItemsChangedEventArgs e) { OnItemsRemoved(e.Items); } private void set_CollectionReset(object sender, CollectionItemsChangedEventArgs e) { OnCollectionReset(e.Items, e.OldItems); } private void set_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName.Equals("Count")) OnPropertyChanged(e.PropertyName); } #endregion } }