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