Skip to content

Commit 6902a72

Browse files
committed
use switch expressions in CollectionHelper
1 parent 2a40df6 commit 6902a72

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

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

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,13 @@ public static Properties asProperties(Map<?,?> map) {
326326
* @return will never return null, but might return an immutable collection.
327327
*/
328328
public static <T> Set<T> toSmallSet(Set<T> set) {
329-
switch ( set.size() ) {
330-
case 0:
331-
return emptySet();
332-
case 1:
333-
return singleton( set.iterator().next() );
334-
default:
335-
//TODO assert tests pass even if this is set to return an unmodifiable Set
336-
return set;
337-
}
329+
return switch ( set.size() ) {
330+
case 0 -> emptySet();
331+
case 1 -> singleton( set.iterator().next() );
332+
//TODO assert tests pass even if this is set to return an unmodifiable Set
333+
default -> set;
334+
335+
};
338336
}
339337

340338
/**
@@ -345,16 +343,16 @@ public static <T> Set<T> toSmallSet(Set<T> set) {
345343
* The goal is to save memory.
346344
*/
347345
public static <K, V> Map<K, V> toSmallMap(final Map<K, V> map) {
348-
switch ( map.size() ) {
349-
case 0:
350-
return emptyMap();
351-
case 1:
352-
Map.Entry<K, V> entry = map.entrySet().iterator().next();
353-
return singletonMap( entry.getKey(), entry.getValue() );
354-
default:
355-
//TODO assert tests pass even if this is set to return an unmodifiable Map
356-
return map;
357-
}
346+
return switch ( map.size() ) {
347+
case 0 -> emptyMap();
348+
case 1 -> {
349+
var entry = map.entrySet().iterator().next();
350+
yield singletonMap( entry.getKey(), entry.getValue() );
351+
}
352+
//TODO assert tests pass even if this is set to return an unmodifiable Map
353+
default -> map;
354+
355+
};
358356
}
359357

360358
/**
@@ -365,15 +363,14 @@ public static <K, V> Map<K, V> toSmallMap(final Map<K, V> map) {
365363
* The goal is to save memory.
366364
*/
367365
public static <V> List<V> toSmallList(ArrayList<V> arrayList) {
368-
switch ( arrayList.size() ) {
369-
case 0:
370-
return emptyList();
371-
case 1:
372-
return singletonList( arrayList.get( 0 ) );
373-
default:
366+
return switch ( arrayList.size() ) {
367+
case 0 -> emptyList();
368+
case 1 -> singletonList( arrayList.get( 0 ) );
369+
default -> {
374370
arrayList.trimToSize();
375-
return arrayList;
376-
}
371+
yield arrayList;
372+
}
373+
};
377374
}
378375

379376
public static <O> List<O> combine(List<O> list1, List<O> list2) {

0 commit comments

Comments
 (0)