Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/28/09 21:38:15 (15 years ago)
Author:
swagner
Message:

Restructured interfaces and base classes (#819)

Location:
trunk/sources/HeuristicLab.Collections/3.3
Files:
4 added
4 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Collections/3.3/HeuristicLab.Collections-3.3.csproj

    r2573 r2574  
    4949  </ItemGroup>
    5050  <ItemGroup>
     51    <Compile Include="IObservableKeyedCollection.cs" />
     52    <Compile Include="IObservableList.cs" />
     53    <Compile Include="IObservableCollection.cs" />
     54    <Compile Include="IObservableDictionary.cs" />
    5155    <Compile Include="ObservableKeyedCollectionBase.cs" />
    52     <Compile Include="IndexedCollectionChangedEventsBase.cs" />
    5356    <Compile Include="ObservableCollection.cs" />
    54     <Compile Include="IIndexedCollectionChangedEvents.cs" />
    5557    <Compile Include="IndexedItem.cs" />
    56     <Compile Include="CollectionChangedEventsBase.cs" />
    5758    <Compile Include="ObservableList.cs" />
    5859    <Compile Include="CollectionItemsChangedEventArgs.cs" />
    5960    <Compile Include="CollectionItemsChangedEventHandler.cs" />
    60     <Compile Include="ICollectionChangedEvents.cs" />
    6161    <Compile Include="HeuristicLabCollectionsPlugin.cs" />
    6262    <Compile Include="ObservableDictionary.cs" />
  • trunk/sources/HeuristicLab.Collections/3.3/ObservableCollection.cs

    r2573 r2574  
    3030namespace HeuristicLab.Collections {
    3131  [Serializable]
    32   public class ObservableCollection<T> : CollectionChangedEventsBase<T>, ICollection<T> {
     32  public class ObservableCollection<T> : IObservableCollection<T> {
    3333    [Storable]
    3434    private List<T> list;
     
    158158    }
    159159    #endregion
     160
     161    #region Events
     162    [field: NonSerialized]
     163    public event CollectionItemsChangedEventHandler<T> ItemsAdded;
     164    protected virtual void OnItemsAdded(IEnumerable<T> items) {
     165      if (ItemsAdded != null)
     166        ItemsAdded(this, new CollectionItemsChangedEventArgs<T>(items));
     167    }
     168
     169    [field: NonSerialized]
     170    public event CollectionItemsChangedEventHandler<T> ItemsRemoved;
     171    protected virtual void OnItemsRemoved(IEnumerable<T> items) {
     172      if (ItemsRemoved != null)
     173        ItemsRemoved(this, new CollectionItemsChangedEventArgs<T>(items));
     174    }
     175
     176    [field: NonSerialized]
     177    public event CollectionItemsChangedEventHandler<T> CollectionReset;
     178    protected virtual void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
     179      if (CollectionReset != null)
     180        CollectionReset(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
     181    }
     182    #endregion
    160183  }
    161184}
  • trunk/sources/HeuristicLab.Collections/3.3/ObservableDictionary.cs

    r2573 r2574  
    3030namespace HeuristicLab.Collections {
    3131  [Serializable]
    32   public class ObservableDictionary<TKey, TValue> : CollectionChangedEventsBase<KeyValuePair<TKey, TValue>>, IDictionary<TKey, TValue> {
     32  public class ObservableDictionary<TKey, TValue> : IObservableDictionary<TKey, TValue> {
    3333    [Storable]
    3434    private Dictionary<TKey, TValue> dict;
     
    156156    }
    157157    #endregion
     158
     159    #region Events
     160    [field: NonSerialized]
     161    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsAdded;
     162    protected virtual void OnItemsAdded(IEnumerable<KeyValuePair<TKey, TValue>> items) {
     163      if (ItemsAdded != null)
     164        ItemsAdded(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
     165    }
     166
     167    [field: NonSerialized]
     168    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsRemoved;
     169    protected virtual void OnItemsRemoved(IEnumerable<KeyValuePair<TKey, TValue>> items) {
     170      if (ItemsRemoved != null)
     171        ItemsRemoved(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
     172    }
     173
     174    [field: NonSerialized]
     175    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsReplaced;
     176    protected virtual void OnItemsReplaced(IEnumerable<KeyValuePair<TKey, TValue>> items, IEnumerable<KeyValuePair<TKey, TValue>> oldItems) {
     177      if (ItemsReplaced != null)
     178        ItemsReplaced(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
     179    }
     180
     181    [field: NonSerialized]
     182    public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> CollectionReset;
     183    protected virtual void OnCollectionReset(IEnumerable<KeyValuePair<TKey, TValue>> items, IEnumerable<KeyValuePair<TKey, TValue>> oldItems) {
     184      if (CollectionReset != null)
     185        CollectionReset(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
     186    }
     187    #endregion
    158188  }
    159189}
  • trunk/sources/HeuristicLab.Collections/3.3/ObservableKeyedCollectionBase.cs

    r2573 r2574  
    3030namespace HeuristicLab.Collections {
    3131  [Serializable]
    32   public abstract class ObservableKeyedCollectionBase<TKey, TItem> : CollectionChangedEventsBase<TItem>, ICollection<TItem> {
     32  public abstract class ObservableKeyedCollectionBase<TKey, TItem> : IObservableKeyedCollection<TKey, TItem> {
    3333    [Storable]
    3434    private Dictionary<TKey, TItem> dict;
     
    7676      foreach (TItem item in collection)
    7777        AddItem(item);
     78    }
     79    #endregion
     80
     81    #region Destructors
     82    ~ObservableKeyedCollectionBase() {
     83      Dispose(false);
     84    }
     85    protected virtual void Dispose(bool disposing) {
     86      if (disposing) {
     87        Clear();
     88      }
     89    }
     90    public void Dispose() {
     91      Dispose(true);
     92      GC.SuppressFinalize(this);
    7893    }
    7994    #endregion
     
    232247    }
    233248    #endregion
     249
     250    #region Events
     251    [field: NonSerialized]
     252    public event CollectionItemsChangedEventHandler<TItem> ItemsAdded;
     253    protected virtual void OnItemsAdded(IEnumerable<TItem> items) {
     254      if (ItemsAdded != null)
     255        ItemsAdded(this, new CollectionItemsChangedEventArgs<TItem>(items));
     256    }
     257
     258    [field: NonSerialized]
     259    public event CollectionItemsChangedEventHandler<TItem> ItemsRemoved;
     260    protected virtual void OnItemsRemoved(IEnumerable<TItem> items) {
     261      if (ItemsRemoved != null)
     262        ItemsRemoved(this, new CollectionItemsChangedEventArgs<TItem>(items));
     263    }
     264
     265    [field: NonSerialized]
     266    public event CollectionItemsChangedEventHandler<TItem> ItemsReplaced;
     267    protected virtual void OnItemsReplaced(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
     268      if (ItemsReplaced != null)
     269        ItemsReplaced(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
     270    }
     271
     272    [field: NonSerialized]
     273    public event CollectionItemsChangedEventHandler<TItem> CollectionReset;
     274    protected virtual void OnCollectionReset(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
     275      if (CollectionReset != null)
     276        CollectionReset(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
     277    }
     278    #endregion
    234279  }
    235280}
  • trunk/sources/HeuristicLab.Collections/3.3/ObservableList.cs

    r2573 r2574  
    3030namespace HeuristicLab.Collections {
    3131  [Serializable]
    32   public class ObservableList<T> : IndexedCollectionChangedEventsBase<IndexedItem<T>>, IList<T> {
     32  public class ObservableList<T> : IObservableList<T> {
    3333    [Storable]
    3434    private List<T> list;
     
    4545      get { return ((ICollection<T>)list).IsReadOnly; }
    4646    }
     47    bool ICollection<IndexedItem<T>>.IsReadOnly {
     48      get { return ((ICollection<T>)list).IsReadOnly; }
     49    }
    4750
    4851    public T this[int index] {
     
    7881      return list.Contains(item);
    7982    }
     83    public bool Contains(IndexedItem<T> item) {
     84      return list[item.Index].Equals(item.Value);
     85    }
    8086
    8187    public int IndexOf(T item) {
     
    148154      list.Add(item);
    149155      OnItemsAdded(new IndexedItem<T>[] { new IndexedItem<T>(list.Count - 1, item) });
     156    }
     157    public void Add(IndexedItem<T> item) {
     158      list.Insert(item.Index, item.Value);
     159      OnItemsAdded(new IndexedItem<T>[] { item });
    150160    }
    151161    public void AddRange(IEnumerable<T> collection) {
     
    183193      return false;
    184194    }
     195    public bool Remove(IndexedItem<T> item) {
     196      if (list[item.Index].Equals(item.Value)) {
     197        list.RemoveAt(item.Index);
     198        OnItemsRemoved(new IndexedItem<T>[] { item });
     199        return true;
     200      }
     201      return false;
     202    }
    185203    public int RemoveAll(Predicate<T> match) {
    186204      if (match == null) throw new ArgumentNullException();
     
    254272      list.CopyTo(array, arrayIndex);
    255273    }
     274    void ICollection<IndexedItem<T>>.CopyTo(IndexedItem<T>[] array, int arrayIndex) {
     275      IndexedItem<T>[] items = GetIndexedItems();
     276      items.CopyTo(array, arrayIndex);
     277    }
    256278    public List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter) {
    257279      return list.ConvertAll<TOutput>(converter);
     
    275297      return ((IEnumerable<T>)list).GetEnumerator();
    276298    }
     299    IEnumerator<IndexedItem<T>> IEnumerable<IndexedItem<T>>.GetEnumerator() {
     300      int index = -1;
     301      foreach (T item in list) {
     302        index++;
     303        yield return new IndexedItem<T>(index, item);
     304      }
     305    }
    277306    IEnumerator IEnumerable.GetEnumerator() {
    278307      return ((IEnumerable)list).GetEnumerator();
     
    283312    public void TrimExcess() {
    284313      list.TrimExcess();
     314    }
     315    #endregion
     316
     317    #region Events
     318    [field: NonSerialized]
     319    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsAdded;
     320    protected virtual void OnItemsAdded(IEnumerable<IndexedItem<T>> items) {
     321      if (ItemsAdded != null)
     322        ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
     323    }
     324
     325    [field: NonSerialized]
     326    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsRemoved;
     327    protected virtual void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
     328      if (ItemsRemoved != null)
     329        ItemsRemoved(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
     330    }
     331
     332    [field: NonSerialized]
     333    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsReplaced;
     334    protected virtual void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
     335      if (ItemsReplaced != null)
     336        ItemsReplaced(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
     337    }
     338
     339    [field: NonSerialized]
     340    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsMoved;
     341    protected virtual void OnItemsMoved(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
     342      if (ItemsMoved != null)
     343        ItemsMoved(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
     344    }
     345
     346    [field: NonSerialized]
     347    public event CollectionItemsChangedEventHandler<IndexedItem<T>> CollectionReset;
     348    protected virtual void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
     349      if (CollectionReset != null)
     350        CollectionReset(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
    285351    }
    286352    #endregion
Note: See TracChangeset for help on using the changeset viewer.