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