Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Pair |
|
| 0.0;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.Serializable; | |
9 | ||
10 | import static jaggregate.Objects.*; | |
11 | ||
12 | /** | |
13 | * A simple 2-tuple, typically used to represent the key-value pairs of an | |
14 | * {@link AbstractDictionary}. | |
15 | * | |
16 | * @param <K> the type of the pair's key | |
17 | * @param <V> the type of the pair's value | |
18 | * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> | |
19 | * @version $Id: Pair.java,v 1.2 2008/03/26 18:00:34 pholser Exp $ | |
20 | */ | |
21 | public class Pair<K, V> implements Serializable { | |
22 | private static final long serialVersionUID = -1L; | |
23 | ||
24 | protected V value; | |
25 | private final K key; | |
26 | ||
27 | /** | |
28 | * Creates a pair. | |
29 | * | |
30 | * @param key the new pair's key | |
31 | * @param value the new pair's value | |
32 | */ | |
33 | 13673 | public Pair( K key, V value ) { |
34 | 13673 | this.key = key; |
35 | 13673 | this.value = value; |
36 | 13673 | } |
37 | ||
38 | /** | |
39 | * Creates a new pair with the given key and value. Unlike the | |
40 | * {@linkplain Pair#Pair(Object,Object) constructor}, this generic factory method | |
41 | * allows type inference to work its magic and can save the caller from having to | |
42 | * specify the actual types of the pair's elements twice. For example: | |
43 | * <pre><code> | |
44 | * Pair<Integer, Integer> square = new Pair<Integer, Integer>( 3, 9 ); | |
45 | * </code></pre> | |
46 | * versus | |
47 | * <pre> | |
48 | * Pair<Integer, Integer> square = pairOf( 3, 9 ); | |
49 | * </pre> | |
50 | * | |
51 | * @param <T> the type of the key in the resultant pair | |
52 | * @param <U> the type of the value in the resultant pair | |
53 | * @param key a key | |
54 | * @param value a value | |
55 | * @return a pair containing {@code key} and {@code value} | |
56 | */ | |
57 | public static <T, U> Pair<T, U> pair( T key, U value ) { | |
58 | 3077 | return new Pair<T, U>( key, value ); |
59 | } | |
60 | ||
61 | /** | |
62 | * Gives the pair's key. | |
63 | * | |
64 | * @return the key | |
65 | */ | |
66 | public K key() { | |
67 | 21818 | return key; |
68 | } | |
69 | ||
70 | /** | |
71 | * Gives the pair's value. | |
72 | * | |
73 | * @return the value | |
74 | */ | |
75 | public V value() { | |
76 | 15958 | return value; |
77 | } | |
78 | ||
79 | /** | |
80 | * {@inheritDoc} | |
81 | */ | |
82 | @Override | |
83 | public boolean equals( Object that ) { | |
84 | 2474 | if ( this == that ) |
85 | 84 | return true; |
86 | ||
87 | 2390 | if ( !( that instanceof Pair<?, ?> ) ) |
88 | 0 | return false; |
89 | ||
90 | 2390 | Pair<?, ?> other = (Pair<?, ?>) that; |
91 | 2390 | return areEqual( key, other.key() ) && areEqual( value, other.value() ); |
92 | } | |
93 | ||
94 | /** | |
95 | * {@inheritDoc} | |
96 | */ | |
97 | @Override | |
98 | public int hashCode() { | |
99 | 1742 | return nullSafeHashCode( key ) ^ nullSafeHashCode( value ); |
100 | } | |
101 | ||
102 | /** | |
103 | * {@inheritDoc} | |
104 | */ | |
105 | @Override | |
106 | public String toString() { | |
107 | 3 | StringBuilder buffer = new StringBuilder( String.valueOf( key ) ); |
108 | 3 | buffer.append( '=' ).append( value ); |
109 | ||
110 | 3 | return buffer.toString(); |
111 | } | |
112 | } |