author | xuelei |
Mon, 25 Jun 2018 09:48:06 -0700 | |
branch | JDK-8145252-TLS13-branch |
changeset 56807 | b9e374d0534f |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
34527
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
1 |
/* |
43068
df01087b2c43
8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents:
34527
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
34527
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
4 |
* |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
10 |
* |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
15 |
* accompanied this code). |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
16 |
* |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
20 |
* |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
23 |
* questions. |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
24 |
*/ |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
25 |
|
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
26 |
package java.util; |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
27 |
|
43068
df01087b2c43
8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents:
34527
diff
changeset
|
28 |
import jdk.internal.vm.annotation.Stable; |
df01087b2c43
8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents:
34527
diff
changeset
|
29 |
|
34527
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
30 |
/** |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
31 |
* An immutable container for a key and a value, suitable for use |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
32 |
* in creating and populating {@code Map} instances. |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
33 |
* |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
34 |
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a> |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
35 |
* class; use of identity-sensitive operations (including reference equality |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
36 |
* ({@code ==}), identity hash code, or synchronization) on instances of |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
37 |
* {@code KeyValueHolder} may have unpredictable results and should be avoided. |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
38 |
* |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
39 |
* @apiNote |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
40 |
* This class is not public. Instances can be created using the |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
41 |
* {@link Map#entry Map.entry(k, v)} factory method, which is public. |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
42 |
* |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
43 |
* <p>This class differs from AbstractMap.SimpleImmutableEntry in the following ways: |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
44 |
* it is not serializable, it is final, and its key and value must be non-null. |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
45 |
* |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
46 |
* @param <K> the key type |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
47 |
* @param <V> the value type |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
48 |
* |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
49 |
* @see Map#ofEntries Map.ofEntries() |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
50 |
* @since 9 |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
51 |
*/ |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
52 |
final class KeyValueHolder<K,V> implements Map.Entry<K,V> { |
43068
df01087b2c43
8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents:
34527
diff
changeset
|
53 |
@Stable |
34527
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
54 |
final K key; |
43068
df01087b2c43
8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents:
34527
diff
changeset
|
55 |
@Stable |
34527
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
56 |
final V value; |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
57 |
|
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
58 |
KeyValueHolder(K k, V v) { |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
59 |
key = Objects.requireNonNull(k); |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
60 |
value = Objects.requireNonNull(v); |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
61 |
} |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
62 |
|
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
63 |
/** |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
64 |
* Gets the key from this holder. |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
65 |
* |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
66 |
* @return the key |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
67 |
*/ |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
68 |
@Override |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
69 |
public K getKey() { |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
70 |
return key; |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
71 |
} |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
72 |
|
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
73 |
/** |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
74 |
* Gets the value from this holder. |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
75 |
* |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
76 |
* @return the value |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
77 |
*/ |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
78 |
@Override |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
79 |
public V getValue() { |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
80 |
return value; |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
81 |
} |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
82 |
|
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
83 |
/** |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
84 |
* Throws {@link UnsupportedOperationException}. |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
85 |
* |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
86 |
* @param value ignored |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
87 |
* @return never returns normally |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
88 |
*/ |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
89 |
@Override |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
90 |
public V setValue(V value) { |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
91 |
throw new UnsupportedOperationException("not supported"); |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
92 |
} |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
93 |
|
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
94 |
/** |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
95 |
* Compares the specified object with this entry for equality. |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
96 |
* Returns {@code true} if the given object is also a map entry and |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
97 |
* the two entries' keys and values are equal. Note that key and |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
98 |
* value are non-null, so equals() can be called safely on them. |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
99 |
*/ |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
100 |
@Override |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
101 |
public boolean equals(Object o) { |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
102 |
if (!(o instanceof Map.Entry)) |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
103 |
return false; |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
104 |
Map.Entry<?,?> e = (Map.Entry<?,?>)o; |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
105 |
return key.equals(e.getKey()) && value.equals(e.getValue()); |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
106 |
} |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
107 |
|
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
108 |
/** |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
109 |
* Returns the hash code value for this map entry. The hash code |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
110 |
* is {@code key.hashCode() ^ value.hashCode()}. Note that key and |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
111 |
* value are non-null, so hashCode() can be called safely on them. |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
112 |
*/ |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
113 |
@Override |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
114 |
public int hashCode() { |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
115 |
return key.hashCode() ^ value.hashCode(); |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
116 |
} |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
117 |
|
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
118 |
/** |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
119 |
* Returns a String representation of this map entry. This |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
120 |
* implementation returns the string representation of this |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
121 |
* entry's key followed by the equals character ("{@code =}") |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
122 |
* followed by the string representation of this entry's value. |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
123 |
* |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
124 |
* @return a String representation of this map entry |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
125 |
*/ |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
126 |
@Override |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
127 |
public String toString() { |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
128 |
return key + "=" + value; |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
129 |
} |
e3caf3a43d09
8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff
changeset
|
130 |
} |