13
13
import java .util .function .BiConsumer ;
14
14
import java .util .function .Consumer ;
15
15
16
+ import static java .lang .System .identityHashCode ;
17
+ import static org .hibernate .internal .util .collections .CollectionHelper .setOfSize ;
18
+
16
19
/**
17
- * A {@code Map} where keys are compared by object identity,
18
- * rather than {@code equals()}.
20
+ * A {@link Map} where keys are compared by object identity,
21
+ * rather than using {@link # equals(Object )}.
19
22
*/
20
23
public final class IdentityMap <K ,V > implements Map <K ,V > {
21
24
@@ -45,11 +48,10 @@ private IdentityMap(LinkedHashMap<IdentityKey<K>,V> underlyingMap) {
45
48
46
49
/**
47
50
* Return the map entries (as instances of {@code Map.Entry} in a collection that
48
- * is safe from concurrent modification). ie. we may safely add new instances to
49
- * the underlying {@code Map} during iteration of the {@code entries()}.
51
+ * is safe from concurrent modification). That is, we may safely add new instances
52
+ * to the underlying {@code Map} during iteration of the {@code entries()}.
50
53
*
51
54
* @param map The map of entries
52
- * @return Collection
53
55
*/
54
56
public static <K ,V > Entry <K ,V >[] concurrentEntries (Map <K ,V > map ) {
55
57
return ( (IdentityMap <K ,V >) map ).entryArray ();
@@ -61,7 +63,7 @@ public static <K,V> void onEachKey(Map<K,V> map, Consumer<K> consumer) {
61
63
}
62
64
63
65
/**
64
- * Override Map {@link #forEach(BiConsumer)} to provide a more efficient implementation
66
+ * Overrides {@link Map #forEach(BiConsumer)} with a more efficient implementation.
65
67
* @param action the operation to apply to each element
66
68
*/
67
69
@ Override
@@ -84,9 +86,8 @@ public boolean isEmpty() {
84
86
}
85
87
86
88
@ Override
87
- @ SuppressWarnings ("unchecked" )
88
89
public boolean containsKey (Object key ) {
89
- return map .containsKey ( new IdentityKey ( key ) );
90
+ return map .containsKey ( new IdentityKey <> ( key ) );
90
91
}
91
92
92
93
@ Override
@@ -96,9 +97,8 @@ public boolean containsValue(Object val) {
96
97
}
97
98
98
99
@ Override
99
- @ SuppressWarnings ( {"unchecked" })
100
100
public V get (Object key ) {
101
- return map .get ( new IdentityKey ( key ) );
101
+ return map .get ( new IdentityKey <> ( key ) );
102
102
}
103
103
104
104
@ Override
@@ -108,15 +108,14 @@ public V put(K key, V value) {
108
108
}
109
109
110
110
@ Override
111
- @ SuppressWarnings ( {"unchecked" })
112
111
public V remove (Object key ) {
113
112
this .entryArray = null ;
114
- return map .remove ( new IdentityKey ( key ) );
113
+ return map .remove ( new IdentityKey <> ( key ) );
115
114
}
116
115
117
116
@ Override
118
117
public void putAll (Map <? extends K , ? extends V > otherMap ) {
119
- for ( Entry <? extends K , ? extends V > entry : otherMap .entrySet () ) {
118
+ for ( var entry : otherMap .entrySet () ) {
120
119
put ( entry .getKey (), entry .getValue () );
121
120
}
122
121
}
@@ -141,21 +140,22 @@ public Collection<V> values() {
141
140
142
141
@ Override
143
142
public Set <Entry <K ,V >> entrySet () {
144
- Set <Entry <K ,V >> set = CollectionHelper . setOfSize ( map .size () );
145
- for ( Entry < IdentityKey < K >, V > entry : map .entrySet () ) {
143
+ final Set <Entry <K ,V >> set = setOfSize ( map .size () );
144
+ for ( var entry : map .entrySet () ) {
146
145
set .add ( new IdentityMapEntry <>( entry .getKey ().key , entry .getValue () ) );
147
146
}
148
147
return set ;
149
148
}
150
149
151
150
public Entry <K ,V >[] entryArray () {
152
151
if ( entryArray == null ) {
152
+ //noinspection unchecked
153
153
entryArray = new Entry [ map .size () ];
154
- final Iterator < Entry < IdentityKey < K >, V >> itr = map .entrySet ().iterator ();
154
+ final var iterator = map .entrySet ().iterator ();
155
155
int i = 0 ;
156
- while ( itr .hasNext () ) {
157
- final Entry < IdentityKey < K >, V > me = itr .next ();
158
- entryArray [i ++] = new IdentityMapEntry <>( me .getKey ().key , me .getValue () );
156
+ while ( iterator .hasNext () ) {
157
+ final var entry = iterator .next ();
158
+ entryArray [i ++] = new IdentityMapEntry <>( entry .getKey ().key , entry .getValue () );
159
159
}
160
160
}
161
161
return entryArray ;
@@ -166,45 +166,33 @@ public String toString() {
166
166
return map .toString ();
167
167
}
168
168
169
- private static final class KeyIterator <K > implements Iterator <K > {
170
- private final Iterator <IdentityKey <K >> identityKeyIterator ;
171
-
172
- private KeyIterator (Iterator <IdentityKey <K >> iterator ) {
173
- identityKeyIterator = iterator ;
174
- }
175
-
169
+ private record KeyIterator <K >(Iterator <IdentityKey <K >> identityKeyIterator )
170
+ implements Iterator <K > {
171
+ @ Override
176
172
public boolean hasNext () {
177
173
return identityKeyIterator .hasNext ();
178
174
}
179
-
175
+ @ Override
180
176
public K next () {
181
177
return identityKeyIterator .next ().key ;
182
178
}
183
-
179
+ @ Override
184
180
public void remove () {
185
181
throw new UnsupportedOperationException ();
186
182
}
187
-
188
183
}
189
184
190
- private static final class IdentityMapEntry <K ,V > implements Entry <K ,V > {
191
-
192
- private final K key ;
193
- private final V value ;
194
-
195
- IdentityMapEntry (final K key , final V value ) {
196
- this .key =key ;
197
- this .value =value ;
198
- }
199
-
185
+ private record IdentityMapEntry <K , V >(K key , V value )
186
+ implements Entry <K , V > {
187
+ @ Override
200
188
public K getKey () {
201
189
return key ;
202
190
}
203
-
191
+ @ Override
204
192
public V getValue () {
205
193
return value ;
206
194
}
207
-
195
+ @ Override
208
196
public V setValue (final V value ) {
209
197
throw new UnsupportedOperationException ();
210
198
}
@@ -213,30 +201,21 @@ public V setValue(final V value) {
213
201
/**
214
202
* We need to base the identity on {@link System#identityHashCode(Object)}
215
203
*/
216
- private static final class IdentityKey <K > implements Serializable {
217
-
218
- private final K key ;
219
-
220
- IdentityKey (K key ) {
221
- this .key = key ;
222
- }
223
-
224
- @ SuppressWarnings ( {"EqualsWhichDoesntCheckParameterClass" })
204
+ private record IdentityKey <K >(K key )
205
+ implements Serializable {
225
206
@ Override
226
207
public boolean equals (Object other ) {
227
- return other != null && key == ( (IdentityKey ) other ).key ;
208
+ return other instanceof IdentityKey <?> that
209
+ && this .key == that .key ;
228
210
}
229
-
230
211
@ Override
231
212
public int hashCode () {
232
- return System . identityHashCode ( key );
213
+ return identityHashCode ( key );
233
214
}
234
-
235
215
@ Override
236
216
public String toString () {
237
217
return key .toString ();
238
218
}
239
-
240
219
}
241
220
242
221
}
0 commit comments