Coverage Report - org.apache.commons.events.observable.standard.StandardModificationEvent
 
Classes in this File Line Coverage Branch Coverage Complexity
StandardModificationEvent
0%
0/60
0%
0/36
1.938
 
 1  
 /*
 2  
  * Copyright 2003-2004 The Apache Software Foundation
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.commons.events.observable.standard;
 17  
 
 18  
 import java.util.Collection;
 19  
 import java.util.Collections;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 import java.util.Set;
 23  
 
 24  
 import org.apache.commons.collections.Bag;
 25  
 import org.apache.commons.events.observable.ModificationEvent;
 26  
 import org.apache.commons.events.observable.ModificationEventType;
 27  
 import org.apache.commons.events.observable.ModificationHandler;
 28  
 import org.apache.commons.events.observable.ObservableCollection;
 29  
 
 30  
 /**
 31  
  * Event class that encapsulates the event information for a
 32  
  * standard collection event. Two subclasses are provided, one for
 33  
  * pre and one for post events.
 34  
  * <p>
 35  
  * The information stored in this event is all that is available as
 36  
  * parameters or return values.
 37  
  * In addition, the <code>size</code> method is used on the collection.
 38  
  * All objects used are the real objects from the method calls, not clones.
 39  
  *
 40  
  * @since Commons Events 1.0
 41  
  * @version $Revision: 155443 $ $Date: 2005-02-26 13:19:51 +0000 (Sat, 26 Feb 2005) $
 42  
  * 
 43  
  * @author Stephen Colebourne
 44  
  */
 45  
 public class StandardModificationEvent extends ModificationEvent {
 46  
 
 47  
     /** The size before the event */
 48  
     protected final int preSize;
 49  
     /** The index of the change */
 50  
     protected final int index;
 51  
     /** The object of the change */
 52  
     protected final Object object;
 53  
     /** The number of changes */
 54  
     protected final int repeat;
 55  
     /** The result of the method call */
 56  
     protected final Object previous;
 57  
     /** The view that the event came from, null if none */
 58  
     protected final ObservableCollection view;
 59  
     /** The offset index within the main collection of the view, -1 if none */
 60  
     protected final int viewOffset;
 61  
 
 62  
     // Constructor
 63  
     //-----------------------------------------------------------------------
 64  
     /**
 65  
      * Constructor.
 66  
      * 
 67  
      * @param obsCollection  the event source
 68  
      * @param handler  the handler
 69  
      * @param type  the event type
 70  
      * @param preSize  the size before the change
 71  
      * @param index  the index that changed
 72  
      * @param object  the value that changed
 73  
      * @param repeat  the number of repeats
 74  
      * @param previous  the previous value being removed/replaced
 75  
      * @param view  the view collection, null if event from main collection
 76  
      * @param viewOffset  the offset within the main collection of the view, -1 if unknown
 77  
      */
 78  
     public StandardModificationEvent(
 79  
         final ObservableCollection obsCollection,
 80  
         final ModificationHandler handler,
 81  
         final int type,
 82  
         final int preSize,
 83  
         final int index,
 84  
         final Object object,
 85  
         final int repeat,
 86  
         final Object previous,
 87  
         final ObservableCollection view,
 88  
         final int viewOffset) {
 89  
 
 90  0
         super(obsCollection, handler, type);
 91  0
         this.preSize = preSize;
 92  0
         this.index = index;
 93  0
         this.object = object;
 94  0
         this.repeat = repeat;
 95  0
         this.previous = previous;
 96  0
         this.view = view;
 97  0
         this.viewOffset = viewOffset;
 98  0
     }
 99  
 
 100  
     // Change info
 101  
     //-----------------------------------------------------------------------
 102  
     /**
 103  
      * Gets the index of the change.
 104  
      * <p>
 105  
      * This is <code>-1</code> when not applicable. Typically only used
 106  
      * for {@link java.util.List} events.
 107  
      * 
 108  
      * @return the change index
 109  
      */
 110  
     public int getChangeIndex() {
 111  0
         return index;
 112  
     }
 113  
 
 114  
     /**
 115  
      * Gets the object that was added/removed/set.
 116  
      * <p>
 117  
      * This is <code>null</code> when not applicable, such as for clear().
 118  
      * 
 119  
      * @return the changing object
 120  
      */
 121  
     public Object getChangeObject() {
 122  0
         return object;
 123  
     }
 124  
 
 125  
     /**
 126  
      * Gets the collection of changed objects.
 127  
      * <p>
 128  
      * For clear, it is an empty list.
 129  
      * For bulk operations, it is the collection.
 130  
      * For non-bulk operations, it is a size one list.
 131  
      * 
 132  
      * @return the changing collection, never null
 133  
      */
 134  
     public Collection getChangeCollection() {
 135  0
         if (object == null) {
 136  0
             return Collections.EMPTY_LIST;
 137  0
         } else if (isType(ModificationEventType.GROUP_BULK)) {
 138  0
             if (object instanceof Collection) {
 139  0
                 return (Collection) object;
 140  
             } else {
 141  0
                 throw new IllegalStateException(
 142  
                     "Bulk operations must involve a Collection, but was " + object.getClass().getName());
 143  
             }
 144  
         } else {
 145  0
             return Collections.singletonList(object);
 146  
         }
 147  
     }
 148  
 
 149  
     /**
 150  
      * Gets the number of times the object was added/removed.
 151  
      * <p>
 152  
      * This is normally <code>1</code>, but will be used for 
 153  
      * {@link org.apache.commons.collections.Bag Bag} events.
 154  
      * 
 155  
      * @return the repeat
 156  
      */
 157  
     public int getChangeRepeat() {
 158  0
         return repeat;
 159  
     }
 160  
 
 161  
     /**
 162  
      * Gets the previous value that is being replaced or removed.
 163  
      * <p>
 164  
      * This is only returned if the value definitely was previously in the
 165  
      * collection. Bulk operatons will not return this.
 166  
      * 
 167  
      * @return the previous value that was removed/replaced
 168  
      */
 169  
     public Object getPrevious() {
 170  0
         return previous;
 171  
     }
 172  
 
 173  
     // Size info
 174  
     //-----------------------------------------------------------------------
 175  
     /**
 176  
      * Gets the size before the change.
 177  
      * 
 178  
      * @return the size before the change
 179  
      */
 180  
     public int getPreSize() {
 181  0
         return preSize;
 182  
     }
 183  
 
 184  
     // View info
 185  
     //-----------------------------------------------------------------------
 186  
     /**
 187  
      * Gets the view, <code>null</code> if none.
 188  
      * <p>
 189  
      * A view is a subSet, headSet, tailSet, subList and so on.
 190  
      * 
 191  
      * @return the view
 192  
      */
 193  
     public ObservableCollection getView() {
 194  0
         return view;
 195  
     }
 196  
 
 197  
     /**
 198  
      * Checks whether the event originated from a view.
 199  
      * 
 200  
      * @return true if event came from a view
 201  
      */
 202  
     public boolean isView() {
 203  0
         return (view != null);
 204  
     }
 205  
 
 206  
     /**
 207  
      * Gets the view offset, <code>-1</code> if no view or unknown offset.
 208  
      * <p>
 209  
      * This refers to the index of the start of the view within the main collection.
 210  
      * 
 211  
      * @return the view offset
 212  
      */
 213  
     public int getViewOffset() {
 214  0
         return viewOffset;
 215  
     }
 216  
 
 217  
     // Event type
 218  
     //-----------------------------------------------------------------------
 219  
     /**
 220  
      * Checks to see if the event is an add event (add/addAll).
 221  
      * 
 222  
      * @return true if of the specified type
 223  
      */
 224  
     public boolean isTypeAdd() {
 225  0
         return (type & ModificationEventType.GROUP_ADD) > 0;
 226  
     }
 227  
 
 228  
     /**
 229  
      * Checks to see if the event is a remove event (remove/removeAll/retainAll/clear).
 230  
      * 
 231  
      * @return true if of the specified type
 232  
      */
 233  
     public boolean isTypeReduce() {
 234  0
         return (type & ModificationEventType.GROUP_REDUCE) > 0;
 235  
     }
 236  
 
 237  
     /**
 238  
      * Checks to see if the event is a change event (set).
 239  
      * 
 240  
      * @return true if of the specified type
 241  
      */
 242  
     public boolean isTypeChange() {
 243  0
         return (type & ModificationEventType.GROUP_CHANGE) > 0;
 244  
     }
 245  
 
 246  
     /**
 247  
      * Checks to see if the event is a bulk event (addAll/removeAll/retainAll/clear).
 248  
      * 
 249  
      * @return true if of the specified type
 250  
      */
 251  
     public boolean isTypeBulk() {
 252  0
         return (type & ModificationEventType.GROUP_BULK) > 0;
 253  
     }
 254  
 
 255  
     /**
 256  
      * Checks to see if the event is of the specified type.
 257  
      * <p>
 258  
      * This is any combination of constants from {@link ModificationEventType}.
 259  
      * 
 260  
      * @param eventType  an event type constant
 261  
      * @return true if of the specified type
 262  
      */
 263  
     public boolean isType(final int eventType) {
 264  0
         return (type & eventType) > 0;
 265  
     }
 266  
 
 267  
     // toString
 268  
     //-----------------------------------------------------------------------
 269  
     /**
 270  
      * Gets a debugging string version of the event.
 271  
      * 
 272  
      * @return a debugging string
 273  
      */
 274  
     public String toString() {
 275  0
         StringBuffer buf = new StringBuffer(64);
 276  0
         buf.append("ModificationEvent[type=");
 277  0
         buf.append(ModificationEventType.toString(type));
 278  0
         if (index >= 0) {
 279  0
             buf.append(",index=");
 280  0
             buf.append(index);
 281  
         }
 282  0
         if (type != ModificationEventType.CLEAR) {
 283  0
             buf.append(",object=");
 284  0
             if (object instanceof List) {
 285  0
                 buf.append("List:size:");
 286  0
                 buf.append(((List) object).size());
 287  0
             } else if (object instanceof Set) {
 288  0
                 buf.append("Set:size:");
 289  0
                 buf.append(((Set) object).size());
 290  0
             } else if (object instanceof Bag) {
 291  0
                 buf.append("Bag:size:");
 292  0
                 buf.append(((Bag) object).size());
 293  0
             } else if (object instanceof Collection) {
 294  0
                 buf.append("Collection:size:");
 295  0
                 buf.append(((Collection) object).size());
 296  0
             } else if (object instanceof Map) {
 297  0
                 buf.append("Map:size:");
 298  0
                 buf.append(((Map) object).size());
 299  0
             } else if (object instanceof Object[]) {
 300  0
                 buf.append("Array:size:");
 301  0
                 buf.append(((Object[]) object).length);
 302  0
             } else if (object == null) {
 303  0
                 buf.append("null");
 304  
             } else {
 305  0
                 buf.append(object.toString());
 306  
             }
 307  
         }
 308  0
         buf.append(']');
 309  0
         return buf.toString();
 310  
     }
 311  
 
 312  
 }