MWF: Remove #if NET_2_0 and #if ONLY_1_1 conditions (part 11).
[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 #if NET_4_0
27 using System.Runtime.CompilerServices;
28
29 namespace System.Collections.Specialized
30 {
31         [TypeForwardedFrom (Consts.WindowsBase_3_0)]
32         public class NotifyCollectionChangedEventArgs : EventArgs
33         {
34                 private NotifyCollectionChangedAction action;
35                 private IList oldItems, newItems;
36                 private int oldIndex = -1, newIndex = -1;
37
38                 #region Constructors
39
40                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action)
41                 {
42                         this.action = action;
43
44                         if (action != NotifyCollectionChangedAction.Reset)
45                                 throw new ArgumentException ("This constructor can only be used with the Reset action.", "action");
46                 }
47
48                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems)
49                         : this (action, changedItems, -1)
50                 {
51                 }
52
53                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem)
54                         : this (action, changedItem, -1)
55                 {
56                 }
57
58                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList newItems, IList oldItems)
59                         : this (action, newItems, oldItems, -1)
60                 {
61                 }
62
63                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems, int startingIndex)
64                 {
65                         this.action = action;
66
67                         if (action == NotifyCollectionChangedAction.Add || action == NotifyCollectionChangedAction.Remove) {
68                                 if (changedItems == null)
69                                         throw new ArgumentNullException ("changedItems");
70
71                                 if (startingIndex < -1)
72                                         throw new ArgumentException ("The value of startingIndex must be -1 or greater.", "startingIndex");
73
74                                 if (action == NotifyCollectionChangedAction.Add)
75                                         InitializeAdd (changedItems, startingIndex);
76                                 else
77                                         InitializeRemove (changedItems, startingIndex);
78                         } else if (action == NotifyCollectionChangedAction.Reset) {
79                                 if (changedItems != null)
80                                         throw new ArgumentException ("This constructor can only be used with the Reset action if changedItems is null", "changedItems");
81
82                                 if (startingIndex != -1)
83                                         throw new ArgumentException ("This constructor can only be used with the Reset action if startingIndex is -1", "startingIndex");
84                         } else {
85                                 throw new ArgumentException ("This constructor can only be used with the Reset, Add, or Remove actions.", "action");
86                         }
87                 }
88
89                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem, int index)
90                 {
91                         IList changedItems = new object [] { changedItem };
92                         this.action = action;
93
94                         if (action == NotifyCollectionChangedAction.Add)
95                                 InitializeAdd (changedItems, index);
96                         else if (action == NotifyCollectionChangedAction.Remove)
97                                 InitializeRemove (changedItems, index);
98                         else if (action == NotifyCollectionChangedAction.Reset) {
99                                 if (changedItem != null)
100                                         throw new ArgumentException ("This constructor can only be used with the Reset action if changedItem is null", "changedItem");
101
102                                 if (index != -1)
103                                         throw new ArgumentException ("This constructor can only be used with the Reset action if index is -1", "index");
104                         } else {
105                                 throw new ArgumentException ("This constructor can only be used with the Reset, Add, or Remove actions.", "action");
106                         }
107                 }
108
109                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object newItem, object oldItem)
110                         : this (action, newItem, oldItem, -1)
111                 {
112                 }
113
114                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList newItems, IList oldItems, int index)
115                 {
116                         this.action = action;
117
118                         if (action != NotifyCollectionChangedAction.Replace)
119                                 throw new ArgumentException ("This constructor can only be used with the Replace action.", "action");
120
121                         if (newItems == null)
122                                 throw new ArgumentNullException ("newItems");
123
124                         if (oldItems == null)
125                                 throw new ArgumentNullException ("oldItems");
126
127                         this.oldItems = oldItems;
128                         this.newItems = newItems;
129
130                         oldIndex = index;
131                         newIndex = index;
132                 }
133
134                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, IList changedItems, int index, int oldIndex)
135                 {
136                         this.action = action;
137
138                         if (action != NotifyCollectionChangedAction.Move)
139                                 throw new ArgumentException ("This constructor can only be used with the Move action.", "action");
140
141                         if (index < -1)
142                                 throw new ArgumentException ("The value of index must be -1 or greater.", "index");
143
144                         InitializeMove (changedItems, index, oldIndex);
145                 }
146
147                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object changedItem, int index, int oldIndex)
148                         : this (action, new object [] { changedItem }, index, oldIndex)
149                 {
150                 }
151
152                 public NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction action, object newItem, object oldItem, int index)
153                 {
154                         this.action = action;
155
156                         if (action != NotifyCollectionChangedAction.Replace)
157                                 throw new ArgumentException ("This constructor can only be used with the Replace action.", "action");
158
159                         InitializeReplace (new object [] { newItem }, new object [] { oldItem }, index);
160                 }
161
162                 #endregion
163
164                 #region Accessor Properties
165
166                 public NotifyCollectionChangedAction Action {
167                         get { return action; }
168                 }
169
170                 public IList NewItems {
171                         get { return newItems; }
172                 }
173
174                 public int NewStartingIndex {
175                         get { return newIndex; }
176                 }
177
178                 public IList OldItems {
179                         get { return oldItems; }
180                 }
181
182                 public int OldStartingIndex {
183                         get { return oldIndex; }
184                 }
185
186                 #endregion
187
188                 #region Initialize Methods
189
190                 private void InitializeAdd(IList items, int index)
191                 {
192                         this.newItems = ArrayList.ReadOnly (items);
193                         this.newIndex = index;
194                 }
195
196                 private void InitializeRemove(IList items, int index)
197                 {
198                         this.oldItems = ArrayList.ReadOnly (items);
199                         this.oldIndex = index;
200                 }
201
202                 private void InitializeMove(IList changedItems, int newItemIndex, int oldItemIndex)
203                 {
204                         InitializeAdd (changedItems, newItemIndex);
205                         InitializeRemove (changedItems, oldItemIndex);
206                 }
207
208                 private void InitializeReplace(IList addedItems, IList removedItems, int index)
209                 {
210                         InitializeAdd (addedItems, index);
211                         InitializeRemove (removedItems, index);
212                 }
213
214                 #endregion
215         }
216 }
217 #endif