[bcl] Remove NET_4_0 defines from class libs.
[mono.git] / mcs / class / System / System.Collections.Specialized / NotifyCollectionChangedEventArgs.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Chris Toshok (toshok@ximian.com)
24 //      Brian O'Keefe (zer0keefie@gmail.com)
25 //
26 using System.Runtime.CompilerServices;
27
28 namespace System.Collections.Specialized
29 {
30 #if !MOBILE
31         [TypeForwardedFrom (Consts.WindowsBase_3_0)]
32 #endif
33         public class NotifyCollectionChangedEventArgs : EventArgs
34         {
35                 private NotifyCollectionChangedAction action;
36                 private IList oldItems, newItems;
37                 private int oldIndex = -1, newIndex = -1;
38
39                 #region Constructors
40
41                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action)
42                 {
43                         this.action = action;
44
45                         if (action != NotifyCollectionChangedAction.Reset)
46                                 throw new ArgumentException ("This constructor can only be used with the Reset action.", "action");
47                 }
48
49                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems)
50                         : this (action, changedItems, -1)
51                 {
52                 }
53
54                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem)
55                         : this (action, changedItem, -1)
56                 {
57                 }
58
59                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList newItems, IList oldItems)
60                         : this (action, newItems, oldItems, -1)
61                 {
62                 }
63
64                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems, int startingIndex)
65                 {
66                         this.action = action;
67
68                         if (action == NotifyCollectionChangedAction.Add || action == NotifyCollectionChangedAction.Remove) {
69                                 if (changedItems == null)
70                                         throw new ArgumentNullException ("changedItems");
71
72                                 if (startingIndex < -1)
73                                         throw new ArgumentException ("The value of startingIndex must be -1 or greater.", "startingIndex");
74
75                                 if (action == NotifyCollectionChangedAction.Add)
76                                         InitializeAdd (changedItems, startingIndex);
77                                 else
78                                         InitializeRemove (changedItems, startingIndex);
79                         } else if (action == NotifyCollectionChangedAction.Reset) {
80                                 if (changedItems != null)
81                                         throw new ArgumentException ("This constructor can only be used with the Reset action if changedItems is null", "changedItems");
82
83                                 if (startingIndex != -1)
84                                         throw new ArgumentException ("This constructor can only be used with the Reset action if startingIndex is -1", "startingIndex");
85                         } else {
86                                 throw new ArgumentException ("This constructor can only be used with the Reset, Add, or Remove actions.", "action");
87                         }
88                 }
89
90                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem, int index)
91                 {
92                         IList changedItems = new object [] { changedItem };
93                         this.action = action;
94
95                         if (action == NotifyCollectionChangedAction.Add)
96                                 InitializeAdd (changedItems, index);
97                         else if (action == NotifyCollectionChangedAction.Remove)
98                                 InitializeRemove (changedItems, index);
99                         else if (action == NotifyCollectionChangedAction.Reset) {
100                                 if (changedItem != null)
101                                         throw new ArgumentException ("This constructor can only be used with the Reset action if changedItem is null", "changedItem");
102
103                                 if (index != -1)
104                                         throw new ArgumentException ("This constructor can only be used with the Reset action if index is -1", "index");
105                         } else {
106                                 throw new ArgumentException ("This constructor can only be used with the Reset, Add, or Remove actions.", "action");
107                         }
108                 }
109
110                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object newItem, object oldItem)
111                         : this (action, newItem, oldItem, -1)
112                 {
113                 }
114
115                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList newItems, IList oldItems, int startingIndex)
116                 {
117                         this.action = action;
118
119                         if (action != NotifyCollectionChangedAction.Replace)
120                                 throw new ArgumentException ("This constructor can only be used with the Replace action.", "action");
121
122                         if (newItems == null)
123                                 throw new ArgumentNullException ("newItems");
124
125                         if (oldItems == null)
126                                 throw new ArgumentNullException ("oldItems");
127
128                         this.oldItems = oldItems;
129                         this.newItems = newItems;
130
131                         oldIndex = startingIndex;
132                         newIndex = startingIndex;
133                 }
134
135                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems, int index, int oldIndex)
136                 {
137                         this.action = action;
138
139                         if (action != NotifyCollectionChangedAction.Move)
140                                 throw new ArgumentException ("This constructor can only be used with the Move action.", "action");
141
142                         if (index < -1)
143                                 throw new ArgumentException ("The value of index must be -1 or greater.", "index");
144
145                         InitializeMove (changedItems, index, oldIndex);
146                 }
147
148                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem, int index, int oldIndex)
149                         : this (action, new object [] { changedItem }, index, oldIndex)
150                 {
151                 }
152
153                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object newItem, object oldItem, int index)
154                 {
155                         this.action = action;
156
157                         if (action != NotifyCollectionChangedAction.Replace)
158                                 throw new ArgumentException ("This constructor can only be used with the Replace action.", "action");
159
160                         InitializeReplace (new object [] { newItem }, new object [] { oldItem }, index);
161                 }
162
163                 #endregion
164
165                 #region Accessor Properties
166
167                 public NotifyCollectionChangedAction Action {
168                         get { return action; }
169                 }
170
171                 public IList NewItems {
172                         get { return newItems; }
173                 }
174
175                 public int NewStartingIndex {
176                         get { return newIndex; }
177                 }
178
179                 public IList OldItems {
180                         get { return oldItems; }
181                 }
182
183                 public int OldStartingIndex {
184                         get { return oldIndex; }
185                 }
186
187                 #endregion
188
189                 #region Initialize Methods
190
191                 private void InitializeAdd(IList items, int index)
192                 {
193                         this.newItems = ArrayList.ReadOnly (items);
194                         this.newIndex = index;
195                 }
196
197                 private void InitializeRemove(IList items, int index)
198                 {
199                         this.oldItems = ArrayList.ReadOnly (items);
200                         this.oldIndex = index;
201                 }
202
203                 private void InitializeMove(IList changedItems, int newItemIndex, int oldItemIndex)
204                 {
205                         InitializeAdd (changedItems, newItemIndex);
206                         InitializeRemove (changedItems, oldItemIndex);
207                 }
208
209                 private void InitializeReplace(IList addedItems, IList removedItems, int index)
210                 {
211                         InitializeAdd (addedItems, index);
212                         InitializeRemove (removedItems, index);
213                 }
214
215                 #endregion
216         }
217 }