Coverage Report - jaggregate.IdentityBag
 
Classes in this File Line Coverage Branch Coverage Complexity
IdentityBag
100%
30/30
N/A
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 java.io.IOException;
 9  
 import java.io.ObjectInputStream;
 10  
 import java.io.ObjectOutputStream;
 11  
 import java.io.Serializable;
 12  
 
 13  
 import static jaggregate.IdentityDictionary.*;
 14  
 import static jaggregate.IdentitySet.*;
 15  
 
 16  
 /**
 17  
  * A bag for which <dfn>equivalence</dfn> is defined by {@code ==}.
 18  
  *
 19  
  * @param <E> a restriction on the types of elements that may be included in the bag
 20  
  *
 21  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 22  
  * @version $Id: IdentityBag.java,v 1.4 2008/05/07 06:00:48 pholser Exp $
 23  
  */
 24  63
 public class IdentityBag<E> extends AbstractBag<E> implements Serializable {
 25  
     private static final long serialVersionUID = -1L;
 26  
 
 27  
     /**
 28  
      * Creates an empty bag.
 29  
      */
 30  135
     public IdentityBag() {
 31  
         // empty on purpose
 32  135
     }
 33  
 
 34  
     /**
 35  
      * Creates a bag containing the elements in the given collection.
 36  
      *
 37  
      * @param elements elements to add to the new bag
 38  
      * @throws NullPointerException if {@code elements} is {@code null}
 39  
      */
 40  
     public IdentityBag( Collection<? extends E> elements ) {
 41  13
         super( elements );
 42  12
     }
 43  
 
 44  
     /**
 45  
      * Creates a bag containing the elements in the given array.
 46  
      *
 47  
      * @param elements elements to add to the new bag
 48  
      * @throws NullPointerException if {@code elements} is {@code null}
 49  
      */
 50  
     public IdentityBag( E... elements ) {
 51  113
         super( elements );
 52  112
     }
 53  
 
 54  
     /**
 55  
      * Creates a bag containing the elements in the given iterable object.
 56  
      *
 57  
      * @param elements elements to add to the new bag
 58  
      * @throws NullPointerException if {@code elements} is {@code null}
 59  
      */
 60  
     public IdentityBag( Iterable<? extends E> elements ) {
 61  2
         super( elements );
 62  1
     }
 63  
 
 64  
     /**
 65  
      * Creates an empty bag.
 66  
      *
 67  
      * @param <T> the type of elements allowed in the new bag
 68  
      * @return a new empty bag
 69  
      */
 70  
     public static <T> IdentityBag<T> emptyIdentityBag() {
 71  135
         return new IdentityBag<T>();
 72  
     }
 73  
 
 74  
     /**
 75  
      * Creates a bag containing the elements in the given collection.
 76  
      *
 77  
      * @param <T> the type of elements allowed in the new bag
 78  
      * @param elements elements to add to the new bag
 79  
      * @return the new bag
 80  
      * @throws NullPointerException if {@code elements} is {@code null}
 81  
      */
 82  
     public static <T> IdentityBag<T> identityBagFrom( Collection<? extends T> elements ) {
 83  5
         return new IdentityBag<T>( elements );
 84  
     }
 85  
 
 86  
     /**
 87  
      * Creates a bag containing the elements in the given array.
 88  
      *
 89  
      * @param <T> the type of elements allowed in the new bag
 90  
      * @param elements elements to add to the new bag
 91  
      * @return the new bag
 92  
      * @throws NullPointerException if {@code elements} is {@code null}
 93  
      */
 94  
     public static <T> IdentityBag<T> identityBagFrom( T[] elements ) {
 95  112
         return new IdentityBag<T>( elements );
 96  
     }
 97  
 
 98  
     /**
 99  
      * Creates a bag containing the given elements.
 100  
      *
 101  
      * @param <T> the type of elements allowed in the new bag
 102  
      * @param newElement first new element to add
 103  
      * @param restOfNewElements remainder of the elements to add
 104  
      * @return the new bag
 105  
      * @throws NullPointerException if {@code restOfNewElements} is {@code null}
 106  
      * @throws IllegalArgumentException if any of the new elements is found to violate
 107  
      * restrictions on the characteristics of valid elements
 108  
      * @see #addAll(Collection)
 109  
      */
 110  
     public static <T> IdentityBag<T> identityBagWith( T newElement,
 111  
         T... restOfNewElements ) {
 112  
 
 113  79
         IdentityBag<T> newBag = identityBagFrom( restOfNewElements );
 114  79
         newBag.add( newElement );
 115  
 
 116  79
         return newBag;
 117  
     }
 118  
 
 119  
     /**
 120  
      * Creates a bag containing the elements given by an iterable object.
 121  
      *
 122  
      * @param <T> the type of elements allowed in the new bag
 123  
      * @param elements elements to add to the new bag
 124  
      * @return the new bag
 125  
      * @throws NullPointerException if {@code elements} is {@code null}
 126  
      */
 127  
     public static <T> IdentityBag<T> identityBagFrom( Iterable<? extends T> elements ) {
 128  2
         return new IdentityBag<T>( elements );
 129  
     }
 130  
 
 131  
     /**
 132  
      * {@inheritDoc}
 133  
      * <p/>
 134  
      * Answers self.
 135  
      */
 136  
     @Override
 137  
     public IdentityBag<E> toIdentityBag() {
 138  1
         return this;
 139  
     }
 140  
 
 141  
     /**
 142  
      * {@inheritDoc}
 143  
      *
 144  
      * @return a bag of results
 145  
      */
 146  
     @Override
 147  
     public <R> IdentityBag<R> collect(
 148  
         UnaryFunctor<? super E, ? extends R> transformer ) {
 149  
 
 150  3
         return (IdentityBag<R>) super.collect( transformer );
 151  
     }
 152  
 
 153  
     /**
 154  
      * {@inheritDoc}
 155  
      *
 156  
      * @return a bag of rejects
 157  
      */
 158  
     @Override
 159  
     public IdentityBag<E> reject( UnaryCondition<? super E> discriminator ) {
 160  2
         return (IdentityBag<E>) super.reject( discriminator );
 161  
     }
 162  
 
 163  
     /**
 164  
      * {@inheritDoc}
 165  
      *
 166  
      * @return a bag of selections
 167  
      */
 168  
     @Override
 169  
     public IdentityBag<E> select( UnaryCondition<? super E> discriminator ) {
 170  12
         return (IdentityBag<E>) super.select( discriminator );
 171  
     }
 172  
 
 173  
     /**
 174  
      * {@inheritDoc}
 175  
      */
 176  
     @Override
 177  
     protected Class<? extends AbstractBag> getImplementationClass() {
 178  276
         return IdentityBag.class;
 179  
     }
 180  
 
 181  
     /**
 182  
      * {@inheritDoc}
 183  
      */
 184  
     @Override
 185  
     protected AbstractDictionary<E, Integer> newEmptyStorage() {
 186  264
         return emptyIdentityDictionary();
 187  
     }
 188  
 
 189  
     /**
 190  
      * {@inheritDoc}
 191  
      */
 192  
     @Override
 193  
     protected IdentityBag<E> newEmptyExtensibleCollection() {
 194  12
         return newEmptyExtensibleResultCollection();
 195  
     }
 196  
 
 197  
     /**
 198  
      * {@inheritDoc}
 199  
      */
 200  
     @Override
 201  
     protected <R> IdentityBag<R> newEmptyExtensibleResultCollection() {
 202  14
         return emptyIdentityBag();
 203  
     }
 204  
 
 205  
     /**
 206  
      * {@inheritDoc}
 207  
      */
 208  
     @Override
 209  
     protected <T> IdentitySet<T> newEmptySet() {
 210  43
         return emptyIdentitySet();
 211  
     }
 212  
 
 213  
     /**
 214  
      * {@inheritDoc}
 215  
      */
 216  
     @Override
 217  
     protected String asString( E anElement ) {
 218  6
         return Objects.identityToString( anElement );
 219  
     }
 220  
 
 221  
     private void writeObject( ObjectOutputStream output ) throws IOException {
 222  1
         serializeTo( output );
 223  1
     }
 224  
 
 225  
     private void readObject( ObjectInputStream input )
 226  
         throws IOException, ClassNotFoundException {
 227  
 
 228  1
         deserializeFrom( input );
 229  1
     }
 230  
 }