Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2575 for trunk/sources


Ignore:
Timestamp:
12/29/09 02:33:57 (15 years ago)
Author:
swagner
Message:

Implemented IDisposable and unified extension capabilities for all observable collections (#819)

Location:
trunk/sources/HeuristicLab.Collections/3.3
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Collections/3.3/IObservableCollection.cs

    r2574 r2575  
    2626
    2727namespace HeuristicLab.Collections {
    28   public interface IObservableCollection<T> : ICollection<T> {
     28  public interface IObservableCollection<T> : ICollection<T>, IDisposable {
    2929    event CollectionItemsChangedEventHandler<T> ItemsAdded;
    3030    event CollectionItemsChangedEventHandler<T> ItemsRemoved;
  • trunk/sources/HeuristicLab.Collections/3.3/IObservableKeyedCollection.cs

    r2574 r2575  
    2626
    2727namespace HeuristicLab.Collections {
    28   public interface IObservableKeyedCollection<TKey, TItem> : IObservableCollection<TItem>, IDisposable {
     28  public interface IObservableKeyedCollection<TKey, TItem> : IObservableCollection<TItem> {
    2929    event CollectionItemsChangedEventHandler<TItem> ItemsReplaced;
    3030  }
  • trunk/sources/HeuristicLab.Collections/3.3/ObservableCollection.cs

    r2574 r2575  
    5656    public ObservableCollection(IEnumerable<T> collection) {
    5757      list = new List<T>(collection);
     58      OnItemsAdded(collection);
     59    }
     60    #endregion
     61
     62    #region Destructors
     63    ~ObservableCollection() {
     64      Dispose(false);
     65    }
     66    protected virtual void Dispose(bool disposing) {
     67      if (disposing) {
     68        Clear();
     69      }
     70    }
     71    public void Dispose() {
     72      Dispose(true);
     73      GC.SuppressFinalize(this);
    5874    }
    5975    #endregion
  • trunk/sources/HeuristicLab.Collections/3.3/ObservableDictionary.cs

    r2574 r2575  
    8080    public ObservableDictionary(IDictionary<TKey, TValue> dictionary) {
    8181      dict = new Dictionary<TKey, TValue>(dictionary);
     82      OnItemsAdded(dictionary);
    8283    }
    8384    public ObservableDictionary(int capacity, IEqualityComparer<TKey> comparer) {
     
    8687    public ObservableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) {
    8788      dict = new Dictionary<TKey, TValue>(dictionary, comparer);
     89      OnItemsAdded(dictionary);
     90    }
     91    #endregion
     92
     93    #region Destructors
     94    ~ObservableDictionary() {
     95      Dispose(false);
     96    }
     97    protected virtual void Dispose(bool disposing) {
     98      if (disposing) {
     99        Clear();
     100      }
     101    }
     102    public void Dispose() {
     103      Dispose(true);
     104      GC.SuppressFinalize(this);
    88105    }
    89106    #endregion
  • trunk/sources/HeuristicLab.Collections/3.3/ObservableKeyedCollectionBase.cs

    r2574 r2575  
    6666      dict = new Dictionary<TKey, TItem>();
    6767      foreach (TItem item in collection)
    68         AddItem(item);
     68        dict.Add(GetKeyForItem(item), item);
     69      OnItemsAdded(collection);
    6970    }
    7071    protected ObservableKeyedCollectionBase(int capacity, IEqualityComparer<TKey> comparer) {
     
    7576      dict = new Dictionary<TKey, TItem>(comparer);
    7677      foreach (TItem item in collection)
    77         AddItem(item);
     78        dict.Add(GetKeyForItem(item), item);
     79      OnItemsAdded(collection);
    7880    }
    7981    #endregion
     
    150152
    151153    #region Manipulation
    152     protected virtual void AddItem(TItem item) {
     154    public void Add(TItem item) {
    153155      dict.Add(GetKeyForItem(item), item);
    154     }
    155     public void Add(TItem item) {
    156       AddItem(item);
    157156      OnItemsAdded(new TItem[] { item });
    158157    }
     
    160159      if (collection == null) throw new ArgumentNullException();
    161160      foreach (TItem item in collection)
    162         AddItem(item);
     161        dict.Add(GetKeyForItem(item), item);
    163162      OnItemsAdded(collection);
    164163    }
    165164
    166     protected virtual bool RemoveItem(TItem item) {
    167       return dict.Remove(GetKeyForItem(item));
    168     }
    169165    public bool Remove(TKey key) {
    170166      TItem item;
    171167      if (TryGetValue(key, out item)) {
    172         RemoveItem(item);
     168        dict.Remove(key);
    173169        OnItemsRemoved(new TItem[] { item });
    174170        return true;
     
    177173    }
    178174    public bool Remove(TItem item) {
    179       if (RemoveItem(item)) {
     175      if (dict.Remove(GetKeyForItem(item))) {
    180176        OnItemsRemoved(new TItem[] { item });
    181177        return true;
     
    187183      List<TItem> items = new List<TItem>();
    188184      foreach (TItem item in collection) {
    189         if (RemoveItem(item))
     185        if (dict.Remove(GetKeyForItem(item)))
    190186          items.Add(item);
    191187      }
     
    199195    }
    200196
    201     protected virtual void ClearItems() {
    202       dict.Clear();
    203     }
    204197    public void Clear() {
    205198      TItem[] items = dict.Values.ToArray();
    206       ClearItems();
     199      dict.Clear();
    207200      OnCollectionReset(new TItem[0], items);
    208201    }
  • trunk/sources/HeuristicLab.Collections/3.3/ObservableList.cs

    r2574 r2575  
    7070    public ObservableList(IEnumerable<T> collection) {
    7171      list = new List<T>(collection);
     72      OnItemsAdded(GetIndexedItems());
     73    }
     74    #endregion
     75
     76    #region Destructors
     77    ~ObservableList() {
     78      Dispose(false);
     79    }
     80    protected virtual void Dispose(bool disposing) {
     81      if (disposing) {
     82        Clear();
     83      }
     84    }
     85    public void Dispose() {
     86      Dispose(true);
     87      GC.SuppressFinalize(this);
    7288    }
    7389    #endregion
Note: See TracChangeset for help on using the changeset viewer.