View Javadoc

1   /*
2    * Copyright (c) 1998, 2005 Gargoyle Software Inc. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    *
7    * 1. Redistributions of source code must retain the above copyright notice,
8    *    this list of conditions and the following disclaimer.
9    * 2. Redistributions in binary form must reproduce the above copyright notice,
10   *    this list of conditions and the following disclaimer in the documentation
11   *    and/or other materials provided with the distribution.
12   * 3. The end-user documentation included with the redistribution, if any, must
13   *    include the following acknowledgment:
14   *
15   *       "This product includes software developed by Gargoyle Software Inc.
16   *        (http://www.GargoyleSoftware.com/)."
17   *
18   *    Alternately, this acknowledgment may appear in the software itself, if
19   *    and wherever such third-party acknowledgments normally appear.
20   * 4. The name "Gargoyle Software" must not be used to endorse or promote
21   *    products derived from this software without prior written permission.
22   *    For written permission, please contact info@GargoyleSoftware.com.
23   * 5. Products derived from this software may not be called "GSBase", nor may
24   *    "GSBase" appear in their name, without prior written permission of
25   *    Gargoyle Software Inc.
26   *
27   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30   * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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             // Normal
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