Skip to content

Commit 7e4ca80

Browse files
committed
HV-2127 Use ImmutableCollections (List.of/Set.of/Map.of) instead of Collections.unmodifiableXxx
Signed-off-by: marko-bekhta <[email protected]>
1 parent ea7baae commit 7e4ca80

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

engine/src/main/java/org/hibernate/validator/internal/constraintvalidators/hv/UniqueElementsValidator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static java.util.stream.Collectors.toList;
88

99
import java.util.Collection;
10+
import java.util.Collections;
1011
import java.util.List;
1112
import java.util.Set;
1213
import java.util.stream.Collectors;
@@ -53,7 +54,10 @@ public boolean isValid(Collection collection, ConstraintValidatorContext constra
5354
if ( constraintValidatorContext instanceof HibernateConstraintValidatorContext ) {
5455
constraintValidatorContext.unwrap( HibernateConstraintValidatorContext.class )
5556
.addMessageParameter( "duplicates", duplicates.stream().map( String::valueOf ).collect( Collectors.joining( ", " ) ) )
56-
.withDynamicPayload( CollectionHelper.toImmutableList( duplicates ) );
57+
// We cannot leverage the CollectionHelper.toImmutableList here as it does not allow `null` values.
58+
// User collections may have `null`s in it and those could as well be duplicates
59+
// so let's rely on the Collections.unmodifiableList here which accepts `null` values as long as the underlying collection allows it:
60+
.withDynamicPayload( Collections.unmodifiableList( duplicates ) );
5761
}
5862

5963
return false;

engine/src/main/java/org/hibernate/validator/internal/util/CollectionHelper.java

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
import java.util.ArrayList;
88
import java.util.Arrays;
99
import java.util.Collection;
10-
import java.util.Collections;
1110
import java.util.HashMap;
1211
import java.util.HashSet;
1312
import java.util.Iterator;
1413
import java.util.LinkedHashSet;
1514
import java.util.List;
1615
import java.util.Map;
17-
import java.util.Map.Entry;
1816
import java.util.Set;
1917
import java.util.concurrent.ConcurrentHashMap;
2018

@@ -95,38 +93,47 @@ public static <T> Set<T> asSet(T... ts) {
9593
return new HashSet<>( Arrays.asList( ts ) );
9694
}
9795

96+
/**
97+
* Converts the provided list to an {@code java.util.ImmutableCollections}.
98+
* <p>
99+
* <b>NOTE:</b> the resulting list does not allow {@code null} values.
100+
* Attempt to convert a list with {@code null}s will result in an exception!
101+
*
102+
* @param list the list to convert.
103+
* @return the converted list.
104+
* @param <T> the type of the list elements.
105+
*/
98106
public static <T> List<T> toImmutableList(List<? extends T> list) {
99-
switch ( list.size() ) {
100-
case 0:
101-
return Collections.emptyList();
102-
case 1:
103-
return Collections.singletonList( list.get( 0 ) );
104-
default:
105-
return Collections.unmodifiableList( list );
106-
}
107+
return List.copyOf( list );
107108
}
108109

110+
/**
111+
* Converts the provided set to an {@code java.util.ImmutableCollections}.
112+
* <p>
113+
* <b>NOTE:</b> the resulting set does not allow {@code null} values.
114+
* Attempt to convert a set with {@code null}s will result in an exception!
115+
*
116+
* @param set the set to convert.
117+
* @return the converted set.
118+
* @param <T> the type of the set elements.
119+
*/
109120
public static <T> Set<T> toImmutableSet(Set<? extends T> set) {
110-
switch ( set.size() ) {
111-
case 0:
112-
return Collections.emptySet();
113-
case 1:
114-
return Collections.singleton( set.iterator().next() );
115-
default:
116-
return Collections.unmodifiableSet( set );
117-
}
121+
return Set.copyOf( set );
118122
}
119123

124+
/**
125+
* Converts the provided map to an {@code java.util.ImmutableCollections}.
126+
* <p>
127+
* <b>NOTE:</b> the resulting map does not allow {@code null} keys and values .
128+
* Attempt to convert a map with {@code null}s (either as key or a value) will result in an exception!
129+
*
130+
* @param map the map to convert.
131+
* @param <K> the type of the map keys.
132+
* @param <V> the type of the map values.
133+
* @return the converted map.
134+
*/
120135
public static <K, V> Map<K, V> toImmutableMap(Map<K, V> map) {
121-
switch ( map.size() ) {
122-
case 0:
123-
return Collections.emptyMap();
124-
case 1:
125-
Entry<K, V> entry = map.entrySet().iterator().next();
126-
return Collections.singletonMap( entry.getKey(), entry.getValue() );
127-
default:
128-
return Collections.unmodifiableMap( map );
129-
}
136+
return Map.copyOf( map );
130137
}
131138

132139
/**

0 commit comments

Comments
 (0)