Coverage Report - jaggregate.Dictionary
 
Classes in this File Line Coverage Branch Coverage Complexity
Dictionary
100%
30/30
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 java.io.IOException;
 9  
 import java.io.ObjectInputStream;
 10  
 import java.io.ObjectOutputStream;
 11  
 import java.io.Serializable;
 12  
 
 13  
 import static jaggregate.Bag.*;
 14  
 import static jaggregate.Set.emptySet;
 15  
 import static jaggregate.internal.EquivalenceTester.*;
 16  
 
 17  
 /**
 18  
  * Represents an unordered collection whose <dfn>elements</dfn> can be accessed using an
 19  
  * explicitly assigned external <dfn>key</dfn>.  <dfn>Key equivalence</dfn> is defined by
 20  
  * {@link Object#equals(Object) equals}.
 21  
  *
 22  
  * @param <K> a restriction on the types of the keys that may be contained in the
 23  
  * dictionary
 24  
  * @param <V> a restriction on the types of the values that may be contained in the
 25  
  * dictionary
 26  
  *
 27  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 28  
  * @version $Id: Dictionary.java,v 1.5 2008/05/09 03:12:56 pholser Exp $
 29  
  */
 30  86
 public class Dictionary<K, V> extends AbstractDictionaryImpl<K, V>
 31  
     implements Serializable {
 32  
 
 33  
     private static final long serialVersionUID = -1L;
 34  
 
 35  
     /**
 36  
      * Creates an empty dictionary.
 37  
      */
 38  
     public Dictionary() {
 39  1221
         super( OBJECT_ATTRIBUTES );
 40  1221
     }
 41  
 
 42  
     /**
 43  
      * Creates a dictionary that contains the given associations.
 44  
      *
 45  
      * @param associations the associations to add to the new dictionary
 46  
      * @throws NullPointerException if {@code associations} is {@code null}
 47  
      */
 48  
     public Dictionary( AbstractDictionary<? extends K, ? extends V> associations ) {
 49  7
         super( associations, OBJECT_ATTRIBUTES );
 50  6
     }
 51  
 
 52  
     /**
 53  
      * Creates a dictionary that contains the given associations.
 54  
      *
 55  
      * @param associations the associations to add to the new dictionary
 56  
      * @throws NullPointerException if {@code associations} is {@code null}, or if
 57  
      * any of the elements of {@code associations} is {@code null}
 58  
      */
 59  
     public Dictionary( Pair<? extends K, ? extends V>... associations ) {
 60  55
         super( OBJECT_ATTRIBUTES );
 61  
 
 62  190
         for ( Pair<? extends K, ? extends V> each : associations )
 63  137
             putAt( each.key(), each.value() );
 64  52
     }
 65  
 
 66  
     /**
 67  
      * Creates an empty dictionary.
 68  
      *
 69  
      * @param <T> the type of keys allowed in the new dictionary
 70  
      * @param <U> the type of values allowed in the new dictionary
 71  
      * @return a new empty dictionary
 72  
      */
 73  
     public static <T, U> Dictionary<T, U> emptyDictionary() {
 74  1221
         return new Dictionary<T, U>();
 75  
     }
 76  
 
 77  
     /**
 78  
      * Creates a dictionary that contains the given associations.
 79  
      *
 80  
      * @param <T> the type of keys allowed in the new dictionary
 81  
      * @param <U> the type of values allowed in the new dictionary
 82  
      * @param associations the associations to add to the new dictionary
 83  
      * @return the new dictionary
 84  
      * @throws NullPointerException if {@code associations} is {@code null}
 85  
      */
 86  
     public static <T, U> Dictionary<T, U> dictionaryFrom(
 87  
         AbstractDictionary<? extends T, ? extends U> associations ) {
 88  
 
 89  6
         return new Dictionary<T, U>( associations );
 90  
     }
 91  
 
 92  
     /**
 93  
      * Creates a dictionary that contains the given associations.
 94  
      *
 95  
      * @param <T> the type of keys allowed in the new dictionary
 96  
      * @param <U> the type of values allowed in the new dictionary
 97  
      * @param associations the associations to add to the new dictionary
 98  
      * @return the new dictionary
 99  
      * @throws NullPointerException if {@code associations} is {@code null}, or if
 100  
      * any of the elements of {@code associations} is {@code null}
 101  
      */
 102  
     public static <T, U> Dictionary<T, U> dictionaryFrom(
 103  
         Pair<? extends T, ? extends U>[] associations ) {
 104  
 
 105  55
         return new Dictionary<T, U>( associations );
 106  
     }
 107  
 
 108  
     /**
 109  
      * Creates a dictionary that contains the given associations.
 110  
      *
 111  
      * @param <T> the type of keys allowed in the new dictionary
 112  
      * @param <U> the type of values allowed in the new dictionary
 113  
      * @param firstAssociation an association to add to the new dictionary
 114  
      * @param restOfAssociations other associations to add to the new dictionary
 115  
      * @return the new dictionary
 116  
      * @throws NullPointerException if {@code firstAssociation} is {@code null},
 117  
      * {@code associations} is {@code null}, or if any of the elements of
 118  
      * {@code associations} is {@code null}
 119  
      */
 120  
     public static <T, U> Dictionary<T, U> dictionaryWith(
 121  
         Pair<? extends T, ? extends U> firstAssociation,
 122  
         Pair<? extends T, ? extends U>... restOfAssociations ) {
 123  
 
 124  23
         Dictionary<T, U> dictionary = dictionaryFrom( restOfAssociations );
 125  22
         dictionary.putAt( firstAssociation.key(), firstAssociation.value() );
 126  21
         return dictionary;
 127  
     }
 128  
 
 129  
     /**
 130  
      * {@inheritDoc}
 131  
      */
 132  
     @Override
 133  
     public <R> Bag<R> collect(
 134  
         UnaryFunctor<? super Pair<K, V>, ? extends R> transformer ) {
 135  
 
 136  3
         return (Bag<R>) super.collect( transformer );
 137  
     }
 138  
 
 139  
     /**
 140  
      * {@inheritDoc}
 141  
      */
 142  
     @Override
 143  
     public <R> Dictionary<K, R> collectValues(
 144  
         UnaryFunctor<? super V, ? extends R> transformer ) {
 145  
 
 146  5
         return (Dictionary<K, R>) super.collectValues( transformer );
 147  
     }
 148  
 
 149  
     /**
 150  
      * {@inheritDoc}
 151  
      */
 152  
     @Override
 153  
     public Dictionary<K, V> reject( UnaryCondition<? super Pair<K, V>> discriminator ) {
 154  6
         return (Dictionary<K, V>) super.reject( discriminator );
 155  
     }
 156  
 
 157  
     /**
 158  
      * {@inheritDoc}
 159  
      */
 160  
     @Override
 161  
     public Dictionary<K, V> rejectValues( UnaryCondition<? super V> discriminator ) {
 162  5
         return (Dictionary<K, V>) super.rejectValues( discriminator );
 163  
     }
 164  
 
 165  
     /**
 166  
      * {@inheritDoc}
 167  
      */
 168  
     @Override
 169  
     public Dictionary<K, V> select( UnaryCondition<? super Pair<K, V>> discriminator ) {
 170  6
         return (Dictionary<K, V>) super.select( discriminator );
 171  
     }
 172  
 
 173  
     /**
 174  
      * {@inheritDoc}
 175  
      */
 176  
     @Override
 177  
     public Dictionary<K, V> selectValues( UnaryCondition<? super V> discriminator ) {
 178  5
         return (Dictionary<K, V>) super.selectValues( discriminator );
 179  
     }
 180  
 
 181  
     /**
 182  
      * {@inheritDoc}
 183  
      */
 184  
     @Override
 185  
     protected Class<? extends AbstractDictionary> getImplementationClass() {
 186  836
         return Dictionary.class;
 187  
     }
 188  
 
 189  
     /**
 190  
      * {@inheritDoc}
 191  
      */
 192  
     @Override
 193  
     protected <T, U> Dictionary<T, U> newEmptyDictionary() {
 194  22
         return emptyDictionary();
 195  
     }
 196  
 
 197  
     /**
 198  
      * {@inheritDoc}
 199  
      */
 200  
     @Override
 201  
     protected <R> Bag<R> newEmptyExtensibleResultCollection() {
 202  6
         return emptyBag();
 203  
     }
 204  
 
 205  
     /**
 206  
      * {@inheritDoc}
 207  
      */
 208  
     @Override
 209  
     protected <T> Set<T> newEmptySet() {
 210  44
         return emptySet();
 211  
     }
 212  
 
 213  
     /**
 214  
      * {@inheritDoc}
 215  
      */
 216  
     @Override
 217  
     protected String asString( Object keyOrValue ) {
 218  12
         return String.valueOf( keyOrValue );
 219  
     }
 220  
 
 221  
     private void writeObject( ObjectOutputStream outputStream )
 222  
         throws IOException {
 223  
 
 224  3
         serializeTo( outputStream );
 225  3
     }
 226  
 
 227  
     private void readObject( ObjectInputStream inputStream )
 228  
         throws IOException, ClassNotFoundException {
 229  
 
 230  3
         deserializeFrom( inputStream, OBJECT_ATTRIBUTES );
 231  3
     }
 232  
 }