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 BoundedCollection.java CollectionUtils.java BoundedFifoBuffer.java
Date Sun, 24 Nov 2002 16:23:21 GMT
scolebourne    2002/11/24 08:23:21

  Modified:    collections STATUS.html
               collections/src/test/org/apache/commons/collections
                        TestCollectionUtils.java
                        TestBoundedFifoBuffer2.java
               collections/src/java/org/apache/commons/collections
                        CollectionUtils.java BoundedFifoBuffer.java
  Added:       collections/src/java/org/apache/commons/collections
                        BoundedCollection.java
  Log:
  Add BoundedCollection, from Herve Quiroz
  
  Revision  Changes    Path
  1.21      +3 -1      jakarta-commons/collections/STATUS.html
  
  Index: STATUS.html
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/STATUS.html,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- STATUS.html	21 Nov 2002 23:09:57 -0000	1.20
  +++ STATUS.html	24 Nov 2002 16:23:20 -0000	1.21
  @@ -40,6 +40,8 @@
       keys of the map and the property values are the values of the map.</li>
   <li><strong>BinaryHeap</strong> - Binary heap implementation 
       of PriorityQueue and Buffer.</li>
  +<li><strong>BoundedCollection</strong> - an interface used by collections
that can 
  +have a variable number of elements up to a fixed bound.</li>
   <li><strong>BoundedFifoBuffer</strong> - a very efficient implementation
of 
       Buffer that does not alter the size of the buffer at runtime.</li>
   <li><strong>Buffer</strong> - a collection that allows objects to be
removed 
  
  
  
  1.8       +56 -4     jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollectionUtils.java
  
  Index: TestCollectionUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollectionUtils.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TestCollectionUtils.java	1 Nov 2002 19:54:27 -0000	1.7
  +++ TestCollectionUtils.java	24 Nov 2002 16:23:21 -0000	1.8
  @@ -498,4 +498,56 @@
           };
       }
   
  +    public void testIsFull() {
  +        Set set = new HashSet();
  +        set.add("1");
  +        set.add("2");
  +        set.add("3");
  +        try {
  +            CollectionUtils.isFull(null);
  +            fail();
  +        } catch (NullPointerException ex) {}
  +        assertEquals(false, CollectionUtils.isFull(set));
  +        
  +        BoundedFifoBuffer buf = new BoundedFifoBuffer(set);
  +        assertEquals(true, CollectionUtils.isFull(buf));
  +        buf.remove("2");
  +        assertEquals(false, CollectionUtils.isFull(buf));
  +        buf.add("2");
  +        assertEquals(true, CollectionUtils.isFull(buf));
  +        
  +        Buffer buf2 = BufferUtils.synchronizedBuffer(buf);
  +        assertEquals(true, CollectionUtils.isFull(buf2));
  +        buf2.remove("2");
  +        assertEquals(false, CollectionUtils.isFull(buf2));
  +        buf2.add("2");
  +        assertEquals(true, CollectionUtils.isFull(buf2));
  +    }
  +
  +    public void testMaxSize() {
  +        Set set = new HashSet();
  +        set.add("1");
  +        set.add("2");
  +        set.add("3");
  +        try {
  +            CollectionUtils.maxSize(null);
  +            fail();
  +        } catch (NullPointerException ex) {}
  +        assertEquals(-1, CollectionUtils.maxSize(set));
  +        
  +        BoundedFifoBuffer buf = new BoundedFifoBuffer(set);
  +        assertEquals(3, CollectionUtils.maxSize(buf));
  +        buf.remove("2");
  +        assertEquals(3, CollectionUtils.maxSize(buf));
  +        buf.add("2");
  +        assertEquals(3, CollectionUtils.maxSize(buf));
  +        
  +        Buffer buf2 = BufferUtils.synchronizedBuffer(buf);
  +        assertEquals(3, CollectionUtils.maxSize(buf2));
  +        buf2.remove("2");
  +        assertEquals(3, CollectionUtils.maxSize(buf2));
  +        buf2.add("2");
  +        assertEquals(3, CollectionUtils.maxSize(buf2));
  +    }
  +
   }
  
  
  
  1.4       +27 -3     jakarta-commons/collections/src/test/org/apache/commons/collections/TestBoundedFifoBuffer2.java
  
  Index: TestBoundedFifoBuffer2.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestBoundedFifoBuffer2.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestBoundedFifoBuffer2.java	12 Oct 2002 22:36:21 -0000	1.3
  +++ TestBoundedFifoBuffer2.java	24 Nov 2002 16:23:21 -0000	1.4
  @@ -134,5 +134,29 @@
           verify();
       }
   
  +    /**
  +     * Tests is full
  +     */
  +    public void testIsFull() {
  +        resetFull();
  +        assertEquals(true, ((BoundedCollection) collection).isFull());
  +        ((BoundedFifoBuffer) collection).remove();
  +        assertEquals(false, ((BoundedCollection) collection).isFull());
  +        ((BoundedFifoBuffer) collection).add("jj");
  +        assertEquals(true, ((BoundedCollection) collection).isFull());
  +    }
  +
  +    /**
  +     * Tests max size
  +     */
  +    public void testMaxSize() {
  +        resetFull();
  +        assertEquals(getFullElements().length, ((BoundedCollection) collection).maxSize());
  +        ((BoundedFifoBuffer) collection).remove();
  +        assertEquals(getFullElements().length, ((BoundedCollection) collection).maxSize());
  +        ((BoundedFifoBuffer) collection).add("jj");
  +        assertEquals(getFullElements().length, ((BoundedCollection) collection).maxSize());
  +    }
  +
   }
   
  
  
  
  1.20      +86 -9     jakarta-commons/collections/src/java/org/apache/commons/collections/CollectionUtils.java
  
  Index: CollectionUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/CollectionUtils.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- CollectionUtils.java	1 Nov 2002 19:54:26 -0000	1.19
  +++ CollectionUtils.java	24 Nov 2002 16:23:21 -0000	1.20
  @@ -58,7 +58,6 @@
    * <http://www.apache.org/>.
    *
    */
  -
   package org.apache.commons.collections;
   
   import java.util.ArrayList;
  @@ -75,7 +74,6 @@
   
   import org.apache.commons.collections.iterators.ArrayIterator;
   import org.apache.commons.collections.iterators.EnumerationIterator;
  -
   /**
    * A set of {@link Collection} related utility methods.
    *
  @@ -84,6 +82,7 @@
    * @author Paul Jack
    * @author Stephen Colebourne
    * @author Steve Downey
  + * @author <a href="herve.quiroz@esil.univ-mrs.fr">Herve Quiroz</a>
    * @version $Revision$ $Date$
    */
   public class CollectionUtils {
  @@ -706,13 +705,17 @@
       }
   
   
  -    /** Reverses the order of the given array */
  +    /** 
  +     * Reverses the order of the given array 
  +     * 
  +     * @param array  the array to reverse
  +     */
       public static void reverseArray(Object[] array) {
           int i = 0;
           int j = array.length - 1;
           Object tmp;
  -        
  -        while(j>i) {
  +
  +        while (j > i) {
               tmp = array[j];
               array[j] = array[i];
               array[i] = tmp;
  @@ -730,6 +733,80 @@
               // ignored
           }
           return 0;
  +    }
  +
  +    /**
  +     * Returns true if no more elements can be added to the Collection.
  +     * <p>
  +     * This method uses the {@link BoundedCollection} class to determine the
  +     * full status. If the collection does not implement this interface then
  +     * false is returned.
  +     * <p>
  +     * This method handles the synchronized, blocking, unmodifiable 
  +     * and predicated decorators.
  +     *
  +     * @return  true if the Collection is full
  +     * @throws NullPointerException if the collection is null
  +     */
  +    public static boolean isFull(Collection coll) {
  +        if (coll == null) {
  +            throw new NullPointerException("The collection must not be null");
  +        }
  +        Collection unwrappedCollection = coll;
  +        
  +        // handle decorators
  +        while (true) {
  +            if (unwrappedCollection instanceof CollectionUtils.CollectionWrapper) {
  +                unwrappedCollection = ((CollectionUtils.CollectionWrapper) unwrappedCollection).collection;
  +            } else if (unwrappedCollection instanceof CollectionUtils.SynchronizedCollection)
{
  +                unwrappedCollection = ((CollectionUtils.SynchronizedCollection) unwrappedCollection).collection;
  +            } else {
  +                break;
  +            }
  +        }
  +        
  +        // is it full
  +        if (unwrappedCollection instanceof BoundedCollection) {
  +            return ((BoundedCollection) unwrappedCollection).isFull();
  +        }
  +        return false;
  +    }
  +
  +    /**
  +     * Get the maximum number of elements that the Collection can contain.
  +     * <p>
  +     * This method uses the {@link BoundedCollection} class to determine the
  +     * maximum size. If the collection does not implement this interface then
  +     * -1 is returned.
  +     * <p>
  +     * This method handles the synchronized, blocking, unmodifiable 
  +     * and predicated decorators.
  +     *
  +     * @return the maximum size of the Collection, -1 if no maximum size
  +     * @throws NullPointerException if the collection is null
  +     */
  +    public static int maxSize(Collection coll) {
  +        if (coll == null) {
  +            throw new NullPointerException("The collection must not be null");
  +        }
  +        Collection unwrappedCollection = coll;
  +        
  +        // handle decorators
  +        while (true) {
  +            if (unwrappedCollection instanceof CollectionUtils.CollectionWrapper) {
  +                unwrappedCollection = ((CollectionUtils.CollectionWrapper) unwrappedCollection).collection;
  +            } else if (unwrappedCollection instanceof CollectionUtils.SynchronizedCollection)
{
  +                unwrappedCollection = ((CollectionUtils.SynchronizedCollection) unwrappedCollection).collection;
  +            } else {
  +                break;
  +            }
  +        }
  +        
  +        // get max size
  +        if (unwrappedCollection instanceof BoundedCollection) {
  +            return ((BoundedCollection) unwrappedCollection).maxSize();
  +        }
  +        return -1;
       }
   
       /**
  
  
  
  1.6       +25 -6     jakarta-commons/collections/src/java/org/apache/commons/collections/BoundedFifoBuffer.java
  
  Index: BoundedFifoBuffer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/BoundedFifoBuffer.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- BoundedFifoBuffer.java	13 Oct 2002 12:59:04 -0000	1.5
  +++ BoundedFifoBuffer.java	24 Nov 2002 16:23:21 -0000	1.6
  @@ -85,14 +85,15 @@
    * <p>
    * This buffer prevents null objects from being added.
    *
  + * @since 2.1
    * @author Avalon
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
    * @author Paul Jack
    * @author Stephen Colebourne
  - * @since 2.1
  + * @author <a href="herve.quiroz@esil.univ-mrs.fr">Herve Quiroz</a>
    * @version $Id$
    */
  -public class BoundedFifoBuffer extends AbstractCollection implements Buffer {
  +public class BoundedFifoBuffer extends AbstractCollection implements Buffer, BoundedCollection
{
       private final Object[] m_elements;
       private int m_start = 0;
       private int m_end = 0;
  @@ -160,6 +161,24 @@
           return size() == 0;
       }
   
  +    /**
  +     * Returns true if this collection is full and no new elements can be added.
  +     *
  +     * @return <code>true</code> if the collection is full
  +     */
  +    public boolean isFull() {
  +        return size() == m_elements.length;
  +    }
  +    
  +    /**
  +     * Gets the maximum size of the collection (the bound).
  +     *
  +     * @return the maximum number of elements the collection can hold
  +     */
  +    public int maxSize() {
  +        return m_elements.length;
  +    }
  +    
       /**
        * Clears this buffer.
        */
  
  
  
  1.1                  jakarta-commons/collections/src/java/org/apache/commons/collections/BoundedCollection.java
  
  Index: BoundedCollection.java
  ===================================================================
  /*
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements 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 Group.
   *
   * 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;
  
  import java.util.Collection;
  /**
   * A BoundedCollection is a collection that is bounded in size.
   * <p>
   * The size of the collection can vary, but it can never exceed a preset 
   * maximum number of elements. This interface allows the querying of details
   * associated with the maximum number of elements.
   *
   * @since 2.2
   * @author <a href="herve.quiroz@esil.univ-mrs.fr">Herve Quiroz</a>
   * @author Stephen Colebourne
   * @version $Id: BoundedCollection.java,v 1.1 2002/11/24 16:23:21 scolebourne Exp $
   */
  public interface BoundedCollection extends Collection {
  
      /**
       * Returns true if this collection is full and no new elements can be added.
       *
       * @return <code>true</code> if the collection is full
       */
      boolean isFull();
      
      /**
       * Gets the maximum size of the collection (the bound).
       *
       * @return the maximum number of elements the collection can hold
       */
      int maxSize();
      
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message