commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/observed ObservedBuffer.java ModificationEventType.java ModificationHandler.java
Date Sun, 07 Sep 2003 16:50:59 GMT
scolebourne    2003/09/07 09:50:59

  Modified:    collections/src/test/org/apache/commons/collections/observed
                        TestAll.java ObservedTestHelper.java
               collections/src/java/org/apache/commons/collections/observed
                        ModificationEventType.java ModificationHandler.java
  Added:       collections/src/test/org/apache/commons/collections/observed
                        TestObservedBuffer.java
               collections/src/java/org/apache/commons/collections/observed
                        ObservedBuffer.java
  Log:
  Add ObservedBuffer
  
  Revision  Changes    Path
  1.2       +3 -2      jakarta-commons/collections/src/test/org/apache/commons/collections/observed/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/observed/TestAll.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestAll.java	3 Sep 2003 23:54:25 -0000	1.1
  +++ TestAll.java	7 Sep 2003 16:50:59 -0000	1.2
  @@ -84,6 +84,7 @@
           TestSuite suite = new TestSuite();
           
           suite.addTest(TestObservedBag.suite());
  +        suite.addTest(TestObservedBuffer.suite());
           suite.addTest(TestObservedCollection.suite());
           suite.addTest(TestObservedList.suite());
           suite.addTest(TestObservedSet.suite());
  
  
  
  1.6       +56 -2     jakarta-commons/collections/src/test/org/apache/commons/collections/observed/ObservedTestHelper.java
  
  Index: ObservedTestHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/observed/ObservedTestHelper.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ObservedTestHelper.java	7 Sep 2003 10:33:33 -0000	1.5
  +++ ObservedTestHelper.java	7 Sep 2003 16:50:59 -0000	1.6
  @@ -190,6 +190,15 @@
           doTestRemoveNCopies(factory);
       }
       
  +    public static void bulkTestObservedBuffer(ObservedFactory factory) {
  +        Assert.assertTrue(factory.createObservedCollection() instanceof ObservedBuffer);
  +        Assert.assertTrue(factory.createObservedCollection(LISTENER) instanceof ObservedBuffer);
  +        Assert.assertTrue(factory.createObservedCollection(new StandardModificationHandler())
instanceof ObservedBuffer);
  +        
  +        bulkTestObservedCollection(factory);
  +        doTestRemoveNext(factory);
  +    }
  +    
       //-----------------------------------------------------------------------
       public static void doTestFactoryPlain(ObservedFactory factory) {
           ObservedCollection coll = factory.createObservedCollection();
  @@ -847,6 +856,51 @@
           Assert.assertEquals(13, LISTENER.postEvent.getPreSize());
           Assert.assertEquals(10, LISTENER.postEvent.getPostSize());
           Assert.assertEquals(-3, LISTENER.postEvent.getSizeChange());
  +        Assert.assertEquals(true, LISTENER.postEvent.isSizeChanged());
  +
  +        Assert.assertEquals(false, LISTENER.postEvent.isRange());
  +        Assert.assertEquals(-1, LISTENER.postEvent.getRangeOffset());
  +        Assert.assertEquals(null, LISTENER.postEvent.getRange());
  +        Assert.assertEquals(false, LISTENER.postEvent.isTypeAdd());
  +        Assert.assertEquals(true, LISTENER.postEvent.isTypeReduce());
  +        Assert.assertEquals(false, LISTENER.postEvent.isTypeChange());
  +        Assert.assertEquals(false, LISTENER.postEvent.isTypeBulk());
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    public static void doTestRemoveNext(ObservedFactory factory) {
  +        ObservedBuffer coll = (ObservedBuffer) factory.createObservedCollection(LISTENER);
  +        
  +        coll.add(SIX);
  +        coll.add(SEVEN);
  +        LISTENER.preEvent = null;
  +        LISTENER.postEvent = null;
  +        Assert.assertEquals(2, coll.size());
  +        coll.remove();
  +        Assert.assertEquals(1, coll.size());
  +        // pre
  +        Assert.assertSame(coll, LISTENER.preEvent.getObservedCollection());
  +        Assert.assertSame(coll.getHandler(), LISTENER.preEvent.getHandler());
  +        Assert.assertEquals(ModificationEventType.REMOVE_NEXT, LISTENER.preEvent.getType());
  +        Assert.assertEquals(-1, LISTENER.preEvent.getChangeIndex());
  +        Assert.assertSame(null, LISTENER.preEvent.getChangeObject());
  +        Assert.assertEquals(0, LISTENER.preEvent.getChangeCollection().size());
  +        Assert.assertEquals(1, LISTENER.preEvent.getChangeRepeat());
  +        Assert.assertSame(null, LISTENER.preEvent.getPrevious());
  +        Assert.assertEquals(2, LISTENER.preEvent.getPreSize());
  +        // post
  +        Assert.assertSame(coll, LISTENER.postEvent.getObservedCollection());
  +        Assert.assertSame(coll.getHandler(), LISTENER.postEvent.getHandler());
  +        Assert.assertEquals(ModificationEventType.REMOVE_NEXT, LISTENER.postEvent.getType());
  +        Assert.assertEquals(-1, LISTENER.postEvent.getChangeIndex());
  +        Assert.assertSame(SEVEN, LISTENER.postEvent.getChangeObject());
  +        Assert.assertEquals(1, LISTENER.postEvent.getChangeCollection().size());
  +        Assert.assertSame(SEVEN, LISTENER.postEvent.getChangeCollection().iterator().next());
  +        Assert.assertEquals(1, LISTENER.postEvent.getChangeRepeat());
  +        Assert.assertSame(SEVEN, LISTENER.postEvent.getPrevious());
  +        Assert.assertEquals(2, LISTENER.postEvent.getPreSize());
  +        Assert.assertEquals(1, LISTENER.postEvent.getPostSize());
  +        Assert.assertEquals(-1, LISTENER.postEvent.getSizeChange());
           Assert.assertEquals(true, LISTENER.postEvent.isSizeChanged());
   
           Assert.assertEquals(false, LISTENER.postEvent.isRange());
  
  
  
  1.1                  jakarta-commons/collections/src/test/org/apache/commons/collections/observed/TestObservedBuffer.java
  
  Index: TestObservedBuffer.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/observed/TestObservedBuffer.java,v
1.1 2003/09/07 16:50:59 scolebourne Exp $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.collections.observed;
  
  import java.util.Arrays;
  import java.util.Collection;
  import java.util.List;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.collections.ArrayStack;
  import org.apache.commons.collections.TestCollection;
  
  /**
   * Extension of {@link TestCollection} for exercising the
   * {@link ObservedBuffer} implementation.
   *
   * @since Commons Collections 3.0
   * @version $Revision: 1.1 $ $Date: 2003/09/07 16:50:59 $
   * 
   * @author Stephen Colebourne
   */
  public class TestObservedBuffer extends TestCollection implements ObservedTestHelper.ObservedFactory
{
      
      public TestObservedBuffer(String testName) {
          super(testName);
      }
  
      public static Test suite() {
          return new TestSuite(TestObservedBuffer.class);
      }
  
      public static void main(String args[]) {
          String[] testCaseName = { TestObservedBuffer.class.getName()};
          junit.textui.TestRunner.main(testCaseName);
      }
  
      //-----------------------------------------------------------------------
      public Collection makeConfirmedCollection() {
          return new ArrayStack();
      }
  
      protected Collection makeConfirmedFullCollection() {
          ArrayStack stack = new ArrayStack();
          stack.addAll(Arrays.asList(getFullElements()));
          return stack;
      }
      
      public Collection makeCollection() {
          return ObservedBuffer.decorate(new ArrayStack(), ObservedTestHelper.LISTENER);
      }
  
      protected Collection makeFullCollection() {
          List stack = new ArrayStack();
          stack.addAll(Arrays.asList(getFullElements()));
          return ObservedBuffer.decorate(stack, ObservedTestHelper.LISTENER);
      }
      
      //-----------------------------------------------------------------------
      public void testObservedBuffer() {
          ObservedTestHelper.bulkTestObservedBuffer(this);
      }
  
      //-----------------------------------------------------------------------
      public ObservedCollection createObservedCollection() {
          return ObservedBuffer.decorate(new ArrayStack());
      }
  
      public ObservedCollection createObservedCollection(Object listener) {
          return ObservedBuffer.decorate(new ArrayStack(), listener);
      }
  
  }
  
  
  
  1.3       +16 -8     jakarta-commons/collections/src/java/org/apache/commons/collections/observed/ModificationEventType.java
  
  Index: ModificationEventType.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/observed/ModificationEventType.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ModificationEventType.java	6 Sep 2003 18:59:09 -0000	1.2
  +++ ModificationEventType.java	7 Sep 2003 16:50:59 -0000	1.3
  @@ -100,15 +100,17 @@
       public static final int REMOVE_INDEXED =0x00000200;
       /** The method remove(Object,int) */
       public static final int REMOVE_NCOPIES =0x00000400;
  +    /** The method remove() */
  +    public static final int REMOVE_NEXT    =0x00000800;
       /** The method iterator.remove() */
  -    public static final int REMOVE_ITERATED=0x00000800;
  +    public static final int REMOVE_ITERATED=0x00001000;
       
       /** The method removeAll(Collection) */
  -    public static final int REMOVE_ALL =    0x00001000;
  +    public static final int REMOVE_ALL =    0x00002000;
       /** The method retainAll(Collection) */
  -    public static final int RETAIN_ALL =    0x00002000;
  +    public static final int RETAIN_ALL =    0x00004000;
       /** The method clear() */
  -    public static final int CLEAR =         0x00004000;
  +    public static final int CLEAR =         0x00008000;
       
       /** The method set(int,Object) */
       public static final int SET_INDEXED =   0x00010000;
  @@ -120,7 +122,7 @@
       /** All methods that change without structure modification */
       public static final int GROUP_CHANGE = SET_INDEXED | SET_ITERATED;
       /** All remove methods */
  -    public static final int GROUP_REMOVE = REMOVE | REMOVE_NCOPIES | REMOVE_ITERATED |
REMOVE_INDEXED | REMOVE_ALL;
  +    public static final int GROUP_REMOVE = REMOVE | REMOVE_INDEXED | REMOVE_NCOPIES | REMOVE_ITERATED
| REMOVE_NEXT | REMOVE_ALL;
       /** All retain methods */
       public static final int GROUP_RETAIN = RETAIN_ALL;
       /** All clear methods */
  @@ -134,6 +136,8 @@
       public static final int GROUP_NCOPIES = ADD_NCOPIES | REMOVE_NCOPIES;
       /** All iterated methods */
       public static final int GROUP_ITERATED = ADD_ITERATED | REMOVE_ITERATED | SET_ITERATED;
  +    /** All 'next' methods */
  +    public static final int GROUP_NEXT = REMOVE_NEXT;
       /** All bulk methods (xxxAll, clear) */
       public static final int GROUP_BULK =  ADD_ALL | ADD_ALL_INDEXED | REMOVE_ALL | RETAIN_ALL
| CLEAR;
       /** All methods that modify the structure */
  @@ -145,8 +149,10 @@
       public static final int GROUP_FROM_SET = GROUP_FROM_COLLECTION;
       /** All methods sent by a List */
       public static final int GROUP_FROM_LIST = GROUP_FROM_COLLECTION | ADD_INDEXED | ADD_ALL_INDEXED
| REMOVE_INDEXED | SET_INDEXED;
  -    /** All methods sent by a List */
  +    /** All methods sent by a Bag */
       public static final int GROUP_FROM_BAG = GROUP_FROM_COLLECTION | ADD_NCOPIES | REMOVE_NCOPIES;
  +    /** All methods sent by a Buffer */
  +    public static final int GROUP_FROM_BUFFER = GROUP_FROM_COLLECTION | REMOVE_NEXT;
   
       /** No methods */
       public static final int GROUP_NONE = 0x00000000;
  @@ -188,6 +194,8 @@
               return "RemoveIndexed";
               case REMOVE_ITERATED:
               return "RemoveIterated";
  +            case REMOVE_NEXT:
  +            return "RemoveNext";
               case REMOVE_ALL:
               return "RemoveAll";
               case RETAIN_ALL:
  
  
  
  1.5       +26 -2     jakarta-commons/collections/src/java/org/apache/commons/collections/observed/ModificationHandler.java
  
  Index: ModificationHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/observed/ModificationHandler.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ModificationHandler.java	7 Sep 2003 10:33:32 -0000	1.4
  +++ ModificationHandler.java	7 Sep 2003 16:50:59 -0000	1.5
  @@ -584,6 +584,30 @@
   
       //-----------------------------------------------------------------------
       /**
  +     * Store data and send event before remove() is called on a Buffer.
  +     * <p>
  +     * This implementation forwards to {@link #preEvent(int, int, Object, int, Object,
Object, int)}.
  +     * 
  +     * @return true to process modification
  +     */
  +    protected boolean preRemoveNext() {
  +        return preEvent(ModificationEventType.REMOVE_NEXT, -1, null, 1, null, null, -1);
  +    }
  +
  +    /**
  +     * Send an event after remove() is called on a Buffer.
  +     * <p>
  +     * This implementation forwards to {@link #postEvent(boolean, int, int, Object, int,
Object, Object, int)}.
  +     * 
  +     * @param removedValue  the previous value at this index
  +     */
  +    protected void postRemoveNext(Object removedValue) {
  +        // assume collection changed
  +        postEvent(true, ModificationEventType.REMOVE_NEXT, -1, removedValue, 1, removedValue,
null, -1);
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    /**
        * Store data and send event before remove(obj) is called on an Iterator.
        * <p>
        * This implementation forwards to {@link #preEvent(int, int, Object, int, Object,
Object, int)}.
  
  
  
  1.1                  jakarta-commons/collections/src/java/org/apache/commons/collections/observed/ObservedBuffer.java
  
  Index: ObservedBuffer.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/observed/ObservedBuffer.java,v
1.1 2003/09/07 16:50:59 scolebourne Exp $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.collections.observed;
  
  import org.apache.commons.collections.Buffer;
  
  /**
   * Decorates a <code>Buffer</code> implementation to observe modifications.
   * <p>
   * Each modifying method call made on this <code>Buffer</code> is forwarded
to a
   * {@link ModificationHandler}.
   * The handler manages the event, notifying listeners and optionally vetoing changes.
   * The default handler is {@link StandardModificationHandler}.
   * See this class for details of configuration available.
   *
   * @since Commons Collections 3.0
   * @version $Revision: 1.1 $ $Date: 2003/09/07 16:50:59 $
   * 
   * @author Stephen Colebourne
   */
  public class ObservedBuffer extends ObservedCollection implements Buffer {
      
      // Factories
      //-----------------------------------------------------------------------
      /**
       * Factory method to create an observable buffer.
       * <p>
       * A {@link StandardModificationHandler} will be created.
       * This can be accessed by {@link #getHandler()} to add listeners.
       *
       * @param buffer  the buffer to decorate, must not be null
       * @return the observed Buffer
       * @throws IllegalArgumentException if the buffer is null
       */
      public static ObservedBuffer decorate(final Buffer buffer) {
          return new ObservedBuffer(buffer, null);
      }
  
      /**
       * Factory method to create an observable buffer using a listener or a handler.
       * <p>
       * A lot of functionality is available through this method.
       * If you don't need the extra functionality, simply implement the
       * {@link org.apache.commons.collections.observed.standard.StandardModificationListener}
       * interface and pass it in as the second parameter.
       * <p>
       * Internally, an <code>ObservedBuffer</code> relies on a {@link ModificationHandler}.
       * The handler receives all the events and processes them, typically by
       * calling listeners. Different handler implementations can be plugged in
       * to provide a flexible event system.
       * <p>
       * The handler implementation is determined by the listener parameter via
       * the registered factories. The listener may be a manually configured 
       * <code>ModificationHandler</code> instance.
       * <p>
       * The listener is defined as an Object for maximum flexibility.
       * It does not have to be a listener in the classic JavaBean sense.
       * It is entirely up to the factory and handler as to how the parameter
       * is interpretted. An IllegalArgumentException is thrown if no suitable
       * handler can be found for this listener.
       * <p>
       * A <code>null</code> listener will create a {@link StandardModificationHandler}.
       *
       * @param buffer  the buffer to decorate, must not be null
       * @param listener  buffer listener, may be null
       * @return the observed buffer
       * @throws IllegalArgumentException if the buffer is null
       * @throws IllegalArgumentException if there is no valid handler for the listener
       */
      public static ObservedBuffer decorate(
              final Buffer buffer,
              final Object listener) {
          
          if (buffer == null) {
              throw new IllegalArgumentException("Buffer must not be null");
          }
          return new ObservedBuffer(buffer, listener);
      }
  
      // Constructors
      //-----------------------------------------------------------------------
      /**
       * Constructor that wraps (not copies).
       * 
       * @param buffer  the buffer to decorate, must not be null
       * @param listener  the listener, may be null
       * @throws IllegalArgumentException if the buffer is null
       */
      protected ObservedBuffer(
              final Buffer buffer,
              final Object listener) {
          super(buffer, listener);
      }
      
      /**
       * Typecast the collection to a Buffer.
       * 
       * @return the wrapped collection as a Buffer
       */
      private Buffer getBuffer() {
          return (Buffer) getCollection();
      }
  
      // Buffer API
      //-----------------------------------------------------------------------
      public Object get() {
          return getBuffer().get();
      }
  
      //-----------------------------------------------------------------------
      public Object remove() {
          Object result = null;
          if (handler.preRemoveNext()) {
              result = getBuffer().remove();
              handler.postRemoveNext(result);
          }
          return result;
      }
  
  }
  
  
  

Mime
View raw message