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