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