Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/ReadOnlyObservableArray.cs @ 2950

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