Skip to content

Commit 582a3a4

Browse files
committed
minor cleanups to IdentityMap and LazySet
1 parent 4d4ab3c commit 582a3a4

File tree

2 files changed

+37
-57
lines changed

2 files changed

+37
-57
lines changed

hibernate-core/src/main/java/org/hibernate/internal/util/collections/IdentityMap.java

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
import java.util.function.BiConsumer;
1414
import java.util.function.Consumer;
1515

16+
import static java.lang.System.identityHashCode;
17+
import static org.hibernate.internal.util.collections.CollectionHelper.setOfSize;
18+
1619
/**
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)}.
1922
*/
2023
public final class IdentityMap<K,V> implements Map<K,V> {
2124

@@ -45,11 +48,10 @@ private IdentityMap(LinkedHashMap<IdentityKey<K>,V> underlyingMap) {
4548

4649
/**
4750
* 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()}.
5053
*
5154
* @param map The map of entries
52-
* @return Collection
5355
*/
5456
public static <K,V> Entry<K,V>[] concurrentEntries(Map<K,V> map) {
5557
return ( (IdentityMap<K,V>) map ).entryArray();
@@ -61,7 +63,7 @@ public static <K,V> void onEachKey(Map<K,V> map, Consumer<K> consumer) {
6163
}
6264

6365
/**
64-
* Override Map{@link #forEach(BiConsumer)} to provide a more efficient implementation
66+
* Overrides {@link Map#forEach(BiConsumer)} with a more efficient implementation.
6567
* @param action the operation to apply to each element
6668
*/
6769
@Override
@@ -84,9 +86,8 @@ public boolean isEmpty() {
8486
}
8587

8688
@Override
87-
@SuppressWarnings("unchecked")
8889
public boolean containsKey(Object key) {
89-
return map.containsKey( new IdentityKey( key ) );
90+
return map.containsKey( new IdentityKey<>( key ) );
9091
}
9192

9293
@Override
@@ -96,9 +97,8 @@ public boolean containsValue(Object val) {
9697
}
9798

9899
@Override
99-
@SuppressWarnings( {"unchecked"})
100100
public V get(Object key) {
101-
return map.get( new IdentityKey( key ) );
101+
return map.get( new IdentityKey<>( key ) );
102102
}
103103

104104
@Override
@@ -108,15 +108,14 @@ public V put(K key, V value) {
108108
}
109109

110110
@Override
111-
@SuppressWarnings( {"unchecked"})
112111
public V remove(Object key) {
113112
this.entryArray = null;
114-
return map.remove( new IdentityKey( key ) );
113+
return map.remove( new IdentityKey<>( key ) );
115114
}
116115

117116
@Override
118117
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() ) {
120119
put( entry.getKey(), entry.getValue() );
121120
}
122121
}
@@ -141,21 +140,22 @@ public Collection<V> values() {
141140

142141
@Override
143142
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() ) {
146145
set.add( new IdentityMapEntry<>( entry.getKey().key, entry.getValue() ) );
147146
}
148147
return set;
149148
}
150149

151150
public Entry<K,V>[] entryArray() {
152151
if ( entryArray == null ) {
152+
//noinspection unchecked
153153
entryArray = new Entry[ map.size() ];
154-
final Iterator<Entry<IdentityKey<K>, V>> itr = map.entrySet().iterator();
154+
final var iterator = map.entrySet().iterator();
155155
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() );
159159
}
160160
}
161161
return entryArray;
@@ -166,45 +166,33 @@ public String toString() {
166166
return map.toString();
167167
}
168168

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
176172
public boolean hasNext() {
177173
return identityKeyIterator.hasNext();
178174
}
179-
175+
@Override
180176
public K next() {
181177
return identityKeyIterator.next().key;
182178
}
183-
179+
@Override
184180
public void remove() {
185181
throw new UnsupportedOperationException();
186182
}
187-
188183
}
189184

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
200188
public K getKey() {
201189
return key;
202190
}
203-
191+
@Override
204192
public V getValue() {
205193
return value;
206194
}
207-
195+
@Override
208196
public V setValue(final V value) {
209197
throw new UnsupportedOperationException();
210198
}
@@ -213,30 +201,21 @@ public V setValue(final V value) {
213201
/**
214202
* We need to base the identity on {@link System#identityHashCode(Object)}
215203
*/
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 {
225206
@Override
226207
public boolean equals(Object other) {
227-
return other != null && key == ( (IdentityKey) other ).key;
208+
return other instanceof IdentityKey<?> that
209+
&& this.key == that.key;
228210
}
229-
230211
@Override
231212
public int hashCode() {
232-
return System.identityHashCode( key );
213+
return identityHashCode( key );
233214
}
234-
235215
@Override
236216
public String toString() {
237217
return key.toString();
238218
}
239-
240219
}
241220

242221
}

hibernate-core/src/main/java/org/hibernate/internal/util/collections/LazySet.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
*/
55
package org.hibernate.internal.util.collections;
66

7-
import java.util.Collections;
87
import java.util.HashSet;
98
import java.util.Set;
109
import java.util.function.Consumer;
1110
import java.util.function.Supplier;
1211

12+
import static java.util.Collections.emptySet;
13+
1314
/**
1415
* @author Steve Ebersole
1516
*/
@@ -40,6 +41,6 @@ public void forEach(Consumer<T> action) {
4041
}
4142

4243
public Set<T> getUnderlyingSet() {
43-
return set == null ? Collections.emptySet() : set;
44+
return set == null ? emptySet() : set;
4445
}
4546
}

0 commit comments

Comments
 (0)