Coverage Report - org.apache.commons.events.observable.ObservableBag
 
Classes in this File Line Coverage Branch Coverage Complexity
ObservableBag
0%
0/24
0%
0/8
1.556
 
 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;
 17  
 
 18  
 import java.util.Set;
 19  
 
 20  
 import org.apache.commons.collections.Bag;
 21  
 
 22  
 /**
 23  
  * Decorates a <code>Bag</code> implementation to observe modifications.
 24  
  * <p>
 25  
  * Each modifying method call made on this <code>Bag</code> is forwarded to a
 26  
  * {@link ModificationHandler}.
 27  
  * The handler manages the event, notifying listeners and optionally vetoing changes.
 28  
  * The default handler is
 29  
  * {@link org.apache.commons.events.observable.standard.StandardModificationHandler StandardModificationHandler}.
 30  
  * See this class for details of configuration available.
 31  
  * <p>
 32  
  * NOTE: The {@link #uniqueSet()} method returns a <code>Set</code> that is
 33  
  * NOT observed. This is because the set should be unmodifiable.
 34  
  *
 35  
  * @since Commons Events 1.0
 36  
  * @version $Revision: 155443 $ $Date: 2005-02-26 13:19:51 +0000 (Sat, 26 Feb 2005) $
 37  
  * 
 38  
  * @author Stephen Colebourne
 39  
  */
 40  
 public class ObservableBag extends ObservableCollection implements Bag {
 41  
     
 42  
     // Factories
 43  
     //-----------------------------------------------------------------------
 44  
     /**
 45  
      * Factory method to create an observable bag.
 46  
      * <p>
 47  
      * A {@link org.apache.commons.events.observable.standard.StandardModificationHandler} will be created.
 48  
      * This can be accessed by {@link #getHandler()} to add listeners.
 49  
      *
 50  
      * @param bag  the bag to decorate, must not be null
 51  
      * @return the observed Bag
 52  
      * @throws IllegalArgumentException if the bag is null
 53  
      */
 54  
     public static ObservableBag decorate(final Bag bag) {
 55  0
         return new ObservableBag(bag, null);
 56  
     }
 57  
 
 58  
     /**
 59  
      * Factory method to create an observable bag using a listener or a handler.
 60  
      * <p>
 61  
      * A lot of functionality is available through this method.
 62  
      * If you don't need the extra functionality, simply implement the
 63  
      * {@link org.apache.commons.events.observable.standard.StandardModificationListener}
 64  
      * interface and pass it in as the second parameter.
 65  
      * <p>
 66  
      * Internally, an <code>ObservableBag</code> relies on a {@link ModificationHandler}.
 67  
      * The handler receives all the events and processes them, typically by
 68  
      * calling listeners. Different handler implementations can be plugged in
 69  
      * to provide a flexible event system.
 70  
      * <p>
 71  
      * The handler implementation is determined by the listener parameter via
 72  
      * the registered factories. The listener may be a manually configured 
 73  
      * <code>ModificationHandler</code> instance.
 74  
      * <p>
 75  
      * The listener is defined as an Object for maximum flexibility.
 76  
      * It does not have to be a listener in the classic JavaBean sense.
 77  
      * It is entirely up to the factory and handler as to how the parameter
 78  
      * is interpretted. An IllegalArgumentException is thrown if no suitable
 79  
      * handler can be found for this listener.
 80  
      * <p>
 81  
      * A <code>null</code> listener will create a
 82  
      * {@link org.apache.commons.events.observable.standard.StandardModificationHandler}.
 83  
      *
 84  
      * @param bag  the bag to decorate, must not be null
 85  
      * @param listener  bag listener, may be null
 86  
      * @return the observed bag
 87  
      * @throws IllegalArgumentException if the bag is null
 88  
      * @throws IllegalArgumentException if there is no valid handler for the listener
 89  
      */
 90  
     public static ObservableBag decorate(
 91  
             final Bag bag,
 92  
             final Object listener) {
 93  
         
 94  0
         if (bag == null) {
 95  0
             throw new IllegalArgumentException("Bag must not be null");
 96  
         }
 97  0
         return new ObservableBag(bag, listener);
 98  
     }
 99  
 
 100  
     // Constructors
 101  
     //-----------------------------------------------------------------------
 102  
     /**
 103  
      * Constructor that wraps (not copies).
 104  
      * <p>
 105  
      * The handler implementation is determined by the listener parameter via
 106  
      * the registered factories. The listener may be a manually configured 
 107  
      * <code>ModificationHandler</code> instance.
 108  
      * 
 109  
      * @param bag  the bag to decorate, must not be null
 110  
      * @param listener  the listener, may be null
 111  
      * @throws IllegalArgumentException if the bag is null
 112  
      */
 113  
     protected ObservableBag(
 114  
             final Bag bag,
 115  
             final Object listener) {
 116  0
         super(bag, listener);
 117  0
     }
 118  
     
 119  
     /**
 120  
      * Typecast the collection to a Bag.
 121  
      * 
 122  
      * @return the wrapped collection as a Bag
 123  
      */
 124  
     private Bag getBag() {
 125  0
         return (Bag) getCollection();
 126  
     }
 127  
 
 128  
     // Bag API
 129  
     //-----------------------------------------------------------------------
 130  
     public int getCount(Object object) {
 131  0
         return getBag().getCount(object);
 132  
     }
 133  
 
 134  
     public Set uniqueSet() {
 135  0
         return getBag().uniqueSet();
 136  
     }
 137  
 
 138  
     //-----------------------------------------------------------------------
 139  
     public boolean add(Object object) {
 140  
         // override as Bag violates Collection contract
 141  0
         boolean result = false;
 142  0
         if (handler.preAdd(object)) {
 143  0
             result = collection.add(object);
 144  0
             handler.postAdd(object, true);  // true, as result is misleading
 145  
         }
 146  0
         return result;
 147  
     }
 148  
 
 149  
     public boolean add(Object object, int nCopies) {
 150  0
         boolean result = false;
 151  0
         if (handler.preAddNCopies(object, nCopies)) {
 152  0
             result = getBag().add(object, nCopies);
 153  0
             handler.postAddNCopies(object, nCopies, result);
 154  
         }
 155  0
         return result;
 156  
     }
 157  
 
 158  
     public boolean remove(Object object, int nCopies) {
 159  0
         boolean result = false;
 160  0
         if (handler.preRemoveNCopies(object, nCopies)) {
 161  0
             result = getBag().remove(object, nCopies);
 162  0
             handler.postRemoveNCopies(object, nCopies, result);
 163  
         }
 164  0
         return result;
 165  
     }
 166  
 
 167  
 }