1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package com.gargoylesoftware.base.collections;
39
40 import com.gargoylesoftware.base.util.DetailedIllegalArgumentException;
41 import com.gargoylesoftware.base.util.DetailedNullPointerException;
42 import java.util.Collections;
43 import java.util.EventObject;
44 import java.util.List;
45
46 /***
47 * An event that is fired when a NotificationList changes.
48 *
49 * @version $Revision: 1.5 $
50 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
51 */
52 public class NotificationListEvent extends EventObject {
53 private static final long serialVersionUID = -1402888290373923581L;
54 /***
55 * Action indicating that an item was inserted into the list.
56 */
57 public static final int INSERT = 1;
58 /***
59 * Action indicating that an item was removed from the list.
60 */
61 public static final int REMOVE = 2;
62 /***
63 * Action indicating that an item in the list was change.
64 */
65 public static final int CHANGE = 3;
66
67 private final int startIndex_;
68 private final int endIndex_;
69 private final int action_;
70 private final List oldValues_;
71 private final List newValues_;
72
73 /***
74 * Create a new event.
75 * @param source The NotificationList
76 * @param action The action that occured. This will be one of INSERT, REMOVE or CHANGE.
77 * @param startIndex The index of the first item that is affected.
78 * @param endIndex The index of the last item that is affected.
79 * @param oldValues The original values.
80 * @param newValues The new values.
81 */
82 public NotificationListEvent( final NotificationList source,
83 final int action,
84 final int startIndex,
85 final int endIndex,
86 final List oldValues,
87 final List newValues) {
88 super(source);
89 startIndex_ = startIndex;
90 endIndex_ = endIndex;
91 if( oldValues.size() == 0 ) {
92 oldValues_ = Collections.EMPTY_LIST;
93 }
94 else {
95 oldValues_ = Collections.unmodifiableList(oldValues);
96 }
97
98 if( newValues.size() == 0 ) {
99 newValues_ = Collections.EMPTY_LIST;
100 }
101 else {
102 newValues_ = Collections.unmodifiableList(newValues);
103 }
104
105 action_ = action;
106
107 switch( action ) {
108 case INSERT:
109 case REMOVE:
110 case CHANGE:
111
112 break;
113
114 default:
115 throw new DetailedIllegalArgumentException("action", new Integer(action), "Unexpected value");
116 }
117
118 if( startIndex < 0 ) {
119 throw new DetailedIllegalArgumentException("startIndex", new Integer(startIndex), "less than zero");
120 }
121
122 if( endIndex < startIndex ) {
123 throw new IllegalArgumentException("endIndex<startIndex: "
124 + endIndex
125 + "<"
126 + startIndex);
127 }
128
129 assertNotNull("oldValues", oldValues);
130 assertNotNull("newValues", newValues);
131 }
132
133 /***
134 * Return the start index.
135 * @return The start index.
136 */
137 public int getStartIndex() {
138 return startIndex_;
139 }
140
141 /***
142 * Return the end index.
143 * @return The end index.
144 */
145 public int getEndIndex() {
146 return endIndex_;
147 }
148
149 /***
150 * Return the action which will be one of INSERT, REMOVE, CHANGE.
151 * @return The action.
152 */
153 public int getAction() {
154 return action_;
155 }
156
157 /***
158 * Return the new values
159 * @return The new values.
160 */
161 public List getNewValues() {
162 return newValues_;
163 }
164
165 /***
166 * Return the old values.
167 * @return The old values.
168 */
169 public List getOldValues() {
170 return oldValues_;
171 }
172
173
174 /***
175 * Throw an exception if the specified object is null
176 * @param fieldName The name of the paremeter we are checking
177 * @param object The value of the parameter we are checking
178 */
179 protected final void assertNotNull( final String fieldName, final Object object ) {
180 if( object == null ) {
181 throw new DetailedNullPointerException(fieldName);
182 }
183 }
184 }
185
186
187