Coverage Report - jaggregate.AbstractExtensibleCollection
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractExtensibleCollection
98%
51/52
100%
4/4
0
AbstractExtensibleCollection$1
100%
3/3
N/A
0
AbstractExtensibleCollection$2
100%
3/3
N/A
0
AbstractExtensibleCollection$3
100%
5/5
100%
1/1
0
AbstractExtensibleCollection$4
100%
2/2
100%
1/1
0
AbstractExtensibleCollection$5
100%
5/5
100%
1/1
0
 
 1  
 /*
 2  
  Copyright 2004-2008 Paul R. Holser, Jr.  All rights reserved.
 3  
  Licensed under the Academic Free License version 3.0
 4  
  */
 5  
 
 6  
 package jaggregate;
 7  
 
 8  
 import static jaggregate.internal.ArgumentChecks.*;
 9  
 
 10  
 /**
 11  
  * Implementation of the <dfn>extensible collection</dfn> concept that should be common
 12  
  * for most concrete implementations.
 13  
  *
 14  
  * @param <E> a restriction on the types of elements that may be included in the
 15  
  * collection
 16  
  *
 17  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 18  
  * @version $Id: AbstractExtensibleCollection.java,v 1.7 2008/10/03 19:01:23 pholser Exp $
 19  
  */
 20  0
 public abstract class AbstractExtensibleCollection<E> extends AbstractCollection<E>
 21  
     implements ExtensibleCollection<E> {
 22  
 
 23  
     /**
 24  
      * Creates an empty collection.
 25  
      */
 26  11607
     protected AbstractExtensibleCollection() {
 27  
         // For subclasses.
 28  11607
     }
 29  
 
 30  
     /**
 31  
      * {@inheritDoc}
 32  
      */
 33  
     @Override
 34  
     public <R> ExtensibleCollection<R> collect(
 35  
         UnaryFunctor<? super E, ? extends R> transformer ) {
 36  
 
 37  38
         return (ExtensibleCollection<R>) super.collect( transformer );
 38  
     }
 39  
 
 40  
     /**
 41  
      * {@inheritDoc}
 42  
      */
 43  
     @Override
 44  
     public ExtensibleCollection<E> reject( UnaryCondition<? super E> discriminator ) {
 45  57
         return (ExtensibleCollection<E>) super.reject( discriminator );
 46  
     }
 47  
 
 48  
     /**
 49  
      * {@inheritDoc}
 50  
      */
 51  
     @Override
 52  
     public ExtensibleCollection<E> select( UnaryCondition<? super E> discriminator ) {
 53  92
         return (ExtensibleCollection<E>) super.select( discriminator );
 54  
     }
 55  
 
 56  
     /**
 57  
      * {@inheritDoc}
 58  
      */
 59  
     public void addAll( Collection<? extends E> newElements ) {
 60  873
         newElements.forEachDo( new UnaryFunctor<E, Void>() {
 61  3455
             public Void evaluate( E argument ) {
 62  2582
                 add( argument );
 63  2580
                 return null;
 64  
             }
 65  
         } );
 66  847
     }
 67  
 
 68  
     /**
 69  
      * {@inheritDoc}
 70  
      */
 71  
     public void addAll( E[] newElements ) {
 72  13521
         for ( E each : newElements )
 73  9883
             add( each );
 74  3615
     }
 75  
 
 76  
     /**
 77  
      * {@inheritDoc}
 78  
      */
 79  
     public void addAll( E newElement, E... restOfNewElements ) {
 80  242
         add( newElement );
 81  242
         addAll( restOfNewElements );
 82  242
     }
 83  
 
 84  
     /**
 85  
      * {@inheritDoc}
 86  
      */
 87  
     public void addAll( Iterable<? extends E> newElements ) {
 88  722
         for ( E each : newElements )
 89  2416
             add( each );
 90  695
     }
 91  
 
 92  
     /**
 93  
      * {@inheritDoc}
 94  
      */
 95  
     public boolean removeAll( Collection<? extends E> oldElements ) {
 96  11
         final boolean[] removalsOccurred = new boolean[] { false };
 97  
 
 98  11
         oldElements.forEachDo( new UnaryFunctor<E, Void>() {
 99  25
             public Void evaluate( E argument ) {
 100  14
                 removalsOccurred[ 0 ] |= remove( argument );
 101  14
                 return null;
 102  
             }
 103  
         } );
 104  
 
 105  4
         return removalsOccurred[ 0 ];
 106  
     }
 107  
 
 108  
     /**
 109  
      * {@inheritDoc}
 110  
      */
 111  
     public boolean removeAll( E[] oldElements ) {
 112  108
         boolean removalsOccurred = false;
 113  
 
 114  245
         for ( E each : oldElements )
 115  137
             removalsOccurred |= remove( each );
 116  
 
 117  101
         return removalsOccurred;
 118  
     }
 119  
 
 120  
     /**
 121  
      * {@inheritDoc}
 122  
      */
 123  
     public boolean removeAll( E oldElement, E... restOfOldElements ) {
 124  101
         boolean removalsOccurred = remove( oldElement );
 125  101
         removalsOccurred |= removeAll( restOfOldElements );
 126  
 
 127  101
         return removalsOccurred;
 128  
     }
 129  
 
 130  
     /**
 131  
      * {@inheritDoc}
 132  
      */
 133  
     public boolean removeAll( Iterable<? extends E> oldElements ) {
 134  106
         boolean removalsOccurred = false;
 135  
 
 136  106
         for ( E each : oldElements )
 137  232
             removalsOccurred |= remove( each );
 138  
 
 139  99
         return removalsOccurred;
 140  
     }
 141  
 
 142  
     /**
 143  
      * {@inheritDoc}
 144  
      */
 145  
     public boolean removeIf( final UnaryCondition<? super E> discriminator ) {
 146  187
         ensureNotNull( discriminator, DISCRIMINATOR );
 147  
 
 148  181
         final boolean[] removalOccurred = new boolean[] { false };
 149  
 
 150  181
         forEachDo( new UnaryFunctor<E, Void>() {
 151  1156
             public Void evaluate( E argument ) {
 152  975
                 if ( discriminator.matches( argument ) ) {
 153  422
                     removalOccurred[ 0 ] = true;
 154  422
                     remove( argument );
 155  
                 }
 156  
 
 157  975
                 return null;
 158  
             }
 159  
         } );
 160  
 
 161  181
         return removalOccurred[ 0 ];
 162  
     }
 163  
 
 164  
     private boolean retainAllOf( final Collection<Object> keepers ) {
 165  156
         return removeIf( new UnaryCondition<E>() {
 166  156
             public boolean matches( E target ) {
 167  718
                 return !keepers.includes( target );
 168  
             }
 169  
         } );
 170  
     }
 171  
 
 172  
     /**
 173  
      * {@inheritDoc}
 174  
      */
 175  
     public boolean retainAll( Collection<? extends E> keepers ) {
 176  12
         AbstractExtensibleCollection<Object> setOfKeepers = newEmptySet();
 177  12
         setOfKeepers.addAll( keepers );
 178  
 
 179  6
         return retainAllOf( setOfKeepers );
 180  
     }
 181  
 
 182  
     /**
 183  
      * {@inheritDoc}
 184  
      */
 185  
     public boolean retainAll( E[] keepers ) {
 186  11
         AbstractExtensibleCollection<Object> setOfKeepers = newEmptySet();
 187  11
         setOfKeepers.addAll( keepers );
 188  
 
 189  4
         return retainAllOf( setOfKeepers );
 190  
     }
 191  
 
 192  
     /**
 193  
      * {@inheritDoc}
 194  
      */
 195  
     public boolean retainAll( E keeper, E... restOfKeepers ) {
 196  75
         AbstractExtensibleCollection<Object> setOfKeepers = newEmptySet();
 197  75
         setOfKeepers.addAll( keeper, restOfKeepers );
 198  
 
 199  75
         return retainAllOf( setOfKeepers );
 200  
     }
 201  
 
 202  
     /**
 203  
      * {@inheritDoc}
 204  
      */
 205  
     public boolean retainAll( Iterable<? extends E> keepers ) {
 206  78
         AbstractExtensibleCollection<Object> setOfKeepers = newEmptySet();
 207  78
         setOfKeepers.addAll( keepers );
 208  
 
 209  71
         return retainAllOf( setOfKeepers );
 210  
     }
 211  
 
 212  
     /**
 213  
      * {@inheritDoc}
 214  
      */
 215  
     public boolean retainIf( final UnaryCondition<? super E> discriminator ) {
 216  52
         ensureNotNull( discriminator, DISCRIMINATOR );
 217  
 
 218  46
         final boolean[] removalOccurred = new boolean[] { false };
 219  
 
 220  46
         forEachDo( new UnaryFunctor<E, Void>() {
 221  431
             public Void evaluate( E argument ) {
 222  385
                 if ( !discriminator.matches( argument ) ) {
 223  207
                     removalOccurred[ 0 ] = true;
 224  207
                     remove( argument );
 225  
                 }
 226  
 
 227  385
                 return null;
 228  
             }
 229  
         } );
 230  
 
 231  46
         return removalOccurred[ 0 ];
 232  
     }
 233  
 
 234  
     /**
 235  
      * Answers a new, empty set implementation for use internally.
 236  
      *
 237  
      * @param <T> a restriction on the types of elements that can be contained in the set
 238  
      * @return a new empty set
 239  
      */
 240  
     protected abstract <T> AbstractSet<T> newEmptySet();
 241  
 }