ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/KeyValueHolder.java
Revision: 1.2
Committed: Sat Mar 26 19:25:33 2016 UTC (8 years, 1 month ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +0 -0 lines
State: FILE REMOVED
Log Message:
drop local copies of Map.java and KeyValueHolder.java

File Contents

# User Rev Content
1 jsr166 1.1 /*
2     * Copyright (c) 2015, 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     /**
29     * An immutable container for a key and a value, suitable for use
30     * in creating and populating {@code Map} instances.
31     *
32     * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
33     * class; use of identity-sensitive operations (including reference equality
34     * ({@code ==}), identity hash code, or synchronization) on instances of
35     * {@code KeyValueHolder} may have unpredictable results and should be avoided.
36     *
37     * @apiNote
38     * This class is not public. Instances can be created using the
39     * {@link Map#entry Map.entry(k, v)} factory method, which is public.
40     *
41     * <p>This class differs from AbstractMap.SimpleImmutableEntry in the following ways:
42     * it is not serializable, it is final, and its key and value must be non-null.
43     *
44     * @param <K> the key type
45     * @param <V> the value type
46     *
47     * @see Map#ofEntries Map.ofEntries()
48     * @since 9
49     */
50     final class KeyValueHolder<K,V> implements Map.Entry<K,V> {
51     final K key;
52     final V value;
53    
54     KeyValueHolder(K k, V v) {
55     key = Objects.requireNonNull(k);
56     value = Objects.requireNonNull(v);
57     }
58    
59     /**
60     * Gets the key from this holder.
61     *
62     * @return the key
63     */
64     @Override
65     public K getKey() {
66     return key;
67     }
68    
69     /**
70     * Gets the value from this holder.
71     *
72     * @return the value
73     */
74     @Override
75     public V getValue() {
76     return value;
77     }
78    
79     /**
80     * Throws {@link UnsupportedOperationException}.
81     *
82     * @param value ignored
83     * @return never returns normally
84     */
85     @Override
86     public V setValue(V value) {
87     throw new UnsupportedOperationException("not supported");
88     }
89    
90     /**
91     * Compares the specified object with this entry for equality.
92     * Returns {@code true} if the given object is also a map entry and
93     * the two entries' keys and values are equal. Note that key and
94     * value are non-null, so equals() can be called safely on them.
95     */
96     @Override
97     public boolean equals(Object o) {
98     if (!(o instanceof Map.Entry))
99     return false;
100     Map.Entry<?,?> e = (Map.Entry<?,?>)o;
101     return key.equals(e.getKey()) && value.equals(e.getValue());
102     }
103    
104     /**
105     * Returns the hash code value for this map entry. The hash code
106     * is {@code key.hashCode() ^ value.hashCode()}. Note that key and
107     * value are non-null, so hashCode() can be called safely on them.
108     */
109     @Override
110     public int hashCode() {
111     return key.hashCode() ^ value.hashCode();
112     }
113    
114     /**
115     * Returns a String representation of this map entry. This
116     * implementation returns the string representation of this
117     * entry's key followed by the equals character ("{@code =}")
118     * followed by the string representation of this entry's value.
119     *
120     * @return a String representation of this map entry
121     */
122     @Override
123     public String toString() {
124     return key + "=" + value;
125     }
126     }