You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rw...@apache.org on 2003/07/08 20:04:50 UTC

cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections/primitives TestIntCollections.java

rwaldhoff    2003/07/08 11:04:49

  Modified:    collections/src/java/org/apache/commons/collections/primitives
                        IntCollections.java
               collections/src/test/org/apache/commons/collections/primitives
                        TestIntCollections.java
  Log:
  add IntCollections.singleton* and tests
  
  Revision  Changes    Path
  1.2       +39 -9     jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/IntCollections.java
  
  Index: IntCollections.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/primitives/IntCollections.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- IntCollections.java	20 May 2003 17:05:28 -0000	1.1
  +++ IntCollections.java	8 Jul 2003 18:04:49 -0000	1.2
  @@ -72,7 +72,37 @@
    * 
    * @author Rodney Waldhoff 
    */
  -public class IntCollections {
  +public final class IntCollections {
  +
  +    /**
  +     * Returns an unmodifiable IntList containing only the specified element.
  +     * @param value the single value
  +     * @return an unmodifiable IntList containing only the specified element.
  +     */    
  +    public static IntList singletonIntList(int value) {
  +        // TODO: a specialized implementation of IntList may be more performant
  +        IntList list = new ArrayIntList(1);
  +        list.add(value);
  +        return UnmodifiableIntList.wrap(list);
  +    }
  +
  +    /**
  +     * Returns an unmodifiable IntIterator containing only the specified element.
  +     * @param value the single value
  +     * @return an unmodifiable IntIterator containing only the specified element.
  +     */    
  +    public static IntIterator singletonIntIterator(int value) {
  +        return singletonIntList(value).iterator();
  +    }
  +
  +    /**
  +     * Returns an unmodifiable IntListIterator containing only the specified element.
  +     * @param value the single value
  +     * @return an unmodifiable IntListIterator containing only the specified element.
  +     */    
  +    public static IntListIterator singletonIntListIterator(int value) {
  +        return singletonIntList(value).listIterator();
  +    }
   
       /**
        * Returns an unmodifiable version of the given non-null IntList.
  @@ -81,7 +111,7 @@
        * @throws NullPointerException if the given IntList is null
        * @see org.apache.commons.collections.primitives.decorators.UnmodifiableIntList#wrap
        */    
  -    public static final IntList unmodifiableIntList(IntList list) throws NullPointerException {
  +    public static IntList unmodifiableIntList(IntList list) throws NullPointerException {
           if(null == list) {
               throw new NullPointerException();
           }
  @@ -95,7 +125,7 @@
        * @throws NullPointerException if the given IntIterator is null
        * @see org.apache.commons.collections.primitives.decorators.UnmodifiableIntIterator#wrap
        */    
  -    public static final IntIterator unmodifiableIntIterator(IntIterator iter) {
  +    public static IntIterator unmodifiableIntIterator(IntIterator iter) {
           if(null == iter) {
               throw new NullPointerException();
           }
  @@ -109,7 +139,7 @@
        * @throws NullPointerException if the given IntListIterator is null
        * @see org.apache.commons.collections.primitives.decorators.UnmodifiableIntListIterator#wrap
        */    
  -    public static final IntListIterator unmodifiableIntListIterator(IntListIterator iter) {
  +    public static IntListIterator unmodifiableIntListIterator(IntListIterator iter) {
           if(null == iter) {
               throw new NullPointerException();
           }
  @@ -121,7 +151,7 @@
        * @return an unmodifiable, empty IntList.
        * @see #EMPTY_INT_LIST
        */    
  -    public static final IntList getEmptyIntList() {
  +    public static IntList getEmptyIntList() {
           return EMPTY_INT_LIST;
       }
       
  @@ -130,7 +160,7 @@
        * @return an unmodifiable, empty IntIterator.
        * @see #EMPTY_INT_ITERATOR
        */    
  -    public static final IntIterator getEmptyIntIterator() {
  +    public static IntIterator getEmptyIntIterator() {
           return EMPTY_INT_ITERATOR;
       }
       
  @@ -139,7 +169,7 @@
        * @return an unmodifiable, empty IntListIterator.
        * @see #EMPTY_INT_LIST_ITERATOR
        */    
  -    public static final IntListIterator getEmptyIntListIterator() {
  +    public static IntListIterator getEmptyIntListIterator() {
           return EMPTY_INT_LIST_ITERATOR;
       }    
   
  
  
  
  1.2       +43 -2     jakarta-commons/collections/src/test/org/apache/commons/collections/primitives/TestIntCollections.java
  
  Index: TestIntCollections.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/primitives/TestIntCollections.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestIntCollections.java	20 May 2003 17:05:28 -0000	1.1
  +++ TestIntCollections.java	8 Jul 2003 18:04:49 -0000	1.2
  @@ -80,6 +80,47 @@
   
       //---------------------------------------------------------------- Tests
   
  +    public void testSingletonIntListIterator() {
  +        IntListIterator iter = IntCollections.singletonIntListIterator(17);
  +        assertTrue(!iter.hasPrevious());        
  +        assertTrue(iter.hasNext());        
  +        assertEquals(17,iter.next());        
  +        assertTrue(iter.hasPrevious());        
  +        assertTrue(!iter.hasNext());        
  +        assertEquals(17,iter.previous());        
  +        try {
  +            iter.set(18);
  +            fail("Expected UnsupportedOperationException");
  +        } catch(UnsupportedOperationException e) {
  +            // expected
  +        }
  +    }
  +
  +    public void testSingletonIntIterator() {
  +        IntIterator iter = IntCollections.singletonIntIterator(17);
  +        assertTrue(iter.hasNext());        
  +        assertEquals(17,iter.next());        
  +        assertTrue(!iter.hasNext());        
  +        try {
  +            iter.remove();
  +            fail("Expected UnsupportedOperationException");
  +        } catch(UnsupportedOperationException e) {
  +            // expected
  +        }
  +    }
  +
  +    public void testSingletonIntList() {
  +        IntList list = IntCollections.singletonIntList(17);
  +        assertEquals(1,list.size());
  +        assertEquals(17,list.get(0));        
  +        try {
  +            list.add(18);
  +            fail("Expected UnsupportedOperationException");
  +        } catch(UnsupportedOperationException e) {
  +            // expected
  +        }
  +    }
  +
       public void testUnmodifiableIntListNull() {
           try {
               IntCollections.unmodifiableIntList(null);
  
  
  

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