| 1 | |
|
| 2 | |
|
| 3 | |
|
| 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 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 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 | |
|
| 38 | |
|
| 39 | |
public IdentityDictionary() { |
| 40 | 971 | super( OBJECT_IDENTITIES ); |
| 41 | 971 | } |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public IdentityDictionary( |
| 50 | |
AbstractDictionary<? extends K, ? extends V> associations ) { |
| 51 | |
|
| 52 | 4 | super( associations, OBJECT_IDENTITIES ); |
| 53 | 3 | } |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 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 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public static <T, U> IdentityDictionary<T, U> emptyIdentityDictionary() { |
| 77 | 971 | return new IdentityDictionary<T, U>(); |
| 78 | |
} |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 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 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 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 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 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 | |
|
| 207 | |
|
| 208 | |
@Override |
| 209 | |
protected Class<? extends AbstractDictionary> getImplementationClass() { |
| 210 | 817 | return IdentityDictionary.class; |
| 211 | |
} |
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
@Override |
| 217 | |
protected <T, U> IdentityDictionary<T, U> newEmptyDictionary() { |
| 218 | 40 | return emptyIdentityDictionary(); |
| 219 | |
} |
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
@Override |
| 225 | |
protected <R> IdentityBag<R> newEmptyExtensibleResultCollection() { |
| 226 | 4 | return emptyIdentityBag(); |
| 227 | |
} |
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
@Override |
| 233 | |
protected <T> IdentitySet<T> newEmptySet() { |
| 234 | 35 | return emptyIdentitySet(); |
| 235 | |
} |
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 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 | |
} |