55package org .hibernate .internal .util .collections ;
66
77import java .util .ArrayList ;
8- import java .util .Arrays ;
98import java .util .Collection ;
109import java .util .Collections ;
1110import java .util .HashMap ;
2019import java .util .function .BiConsumer ;
2120import java .util .function .Function ;
2221
22+ import static java .util .Arrays .asList ;
23+ import static java .util .Collections .emptyList ;
24+ import static java .util .Collections .emptyMap ;
25+ import static java .util .Collections .emptySet ;
26+ import static java .util .Collections .singletonList ;
27+ import static java .util .Collections .singletonMap ;
28+
2329/**
2430 * Various helper util methods for handling collections.
2531 *
@@ -130,7 +136,7 @@ public static <K> LinkedHashSet<K> linkedSet() {
130136 *
131137 * @return The proper size.
132138 */
133- public static int determineProperSizing (Map original ) {
139+ public static int determineProperSizing (Map <?,?> original ) {
134140 return determineProperSizing ( original .size () );
135141 }
136142
@@ -147,17 +153,12 @@ public static <K, V> HashMap<K, V> makeCopy(
147153 if ( original == null ) {
148154 return null ;
149155 }
150-
151- final HashMap <K , V > copy = new HashMap <>( determineProperSizing ( original ) );
152-
153- original .forEach (
154- (key , value ) -> copy .put (
155- keyTransformer .apply ( key ),
156- valueTransformer .apply ( value )
157- )
158- );
159-
160- return copy ;
156+ else {
157+ final HashMap <K , V > copy = new HashMap <>( determineProperSizing ( original ) );
158+ original .forEach ( (key , value ) -> copy .put ( keyTransformer .apply ( key ),
159+ valueTransformer .apply ( value ) ) );
160+ return copy ;
161+ }
161162 }
162163
163164 public static <K , V > Map <K , V > makeMap (
@@ -171,19 +172,15 @@ public static <K, V, E> Map<K, V> makeMap(
171172 Function <E ,K > keyProducer ,
172173 Function <E ,V > valueProducer ) {
173174 if ( isEmpty ( collection ) ) {
174- return Collections . emptyMap ();
175+ return emptyMap ();
175176 }
176-
177- final Map <K , V > map = new HashMap <>( determineProperSizing ( collection .size () ));
178-
179- for ( E element : collection ) {
180- map .put (
181- keyProducer .apply ( element ),
182- valueProducer .apply ( element )
183- );
177+ else {
178+ final Map <K , V > map = new HashMap <>( determineProperSizing ( collection .size () ) );
179+ for ( E element : collection ) {
180+ map .put ( keyProducer .apply ( element ), valueProducer .apply ( element ) );
181+ }
182+ return map ;
184183 }
185-
186- return map ;
187184 }
188185
189186 /**
@@ -194,7 +191,7 @@ public static <K, V, E> Map<K, V> makeMap(
194191 *
195192 * @return The proper size.
196193 */
197- public static int determineProperSizing (Set original ) {
194+ public static int determineProperSizing (Set <?> original ) {
198195 return determineProperSizing ( original .size () );
199196 }
200197
@@ -207,8 +204,7 @@ public static int determineProperSizing(Set original) {
207204 * @return The proper size.
208205 */
209206 public static int determineProperSizing (int numberOfElements ) {
210- int actual = ( (int ) ( numberOfElements / LOAD_FACTOR ) ) + 1 ;
211- return Math .max ( actual , MINIMUM_INITIAL_CAPACITY );
207+ return Math .max ( ( (int ) ( numberOfElements / LOAD_FACTOR ) ) + 1 , MINIMUM_INITIAL_CAPACITY );
212208 }
213209
214210 /**
@@ -255,26 +251,26 @@ public static <T> Set<T> makeCopy(Set<T> source) {
255251 if ( source == null ) {
256252 return null ;
257253 }
258-
259- final int size = source .size ();
260- final Set < T > copy = CollectionHelper . setOfSize ( size + 1 );
261- copy . addAll ( source ) ;
262- return copy ;
254+ else {
255+ final Set < T > copy = setOfSize ( source .size () + 1 );
256+ copy . addAll ( source );
257+ return copy ;
258+ }
263259 }
264260
265261 public static boolean isEmpty (Collection <?> collection ) {
266262 return collection == null || collection .isEmpty ();
267263 }
268264
269- public static boolean isEmpty (Map map ) {
265+ public static boolean isEmpty (Map <?,?> map ) {
270266 return map == null || map .isEmpty ();
271267 }
272268
273- public static boolean isNotEmpty (Collection collection ) {
269+ public static boolean isNotEmpty (Collection <?> collection ) {
274270 return !isEmpty ( collection );
275271 }
276272
277- public static boolean isNotEmpty (Map map ) {
273+ public static boolean isNotEmpty (Map <?,?> map ) {
278274 return !isEmpty ( map );
279275 }
280276
@@ -305,23 +301,20 @@ public static <T> Set<T> setOf(T... values) {
305301 }
306302
307303 public static <T > Set <T > setOf (Collection <T > values ) {
308- if ( isEmpty ( values ) ) {
309- return Collections .emptySet ();
310- }
311- return new HashSet <>( values );
304+ return isEmpty ( values ) ? emptySet () : new HashSet <>( values );
312305 }
313306
314307 public static Properties asProperties (Map <?,?> map ) {
315- if ( map instanceof Properties ) {
316- return ( ( Properties ) map ) ;
308+ if ( map instanceof Properties properties ) {
309+ return properties ;
317310 }
318-
319- final Properties properties = new Properties ();
320- if ( isNotEmpty ( map ) ) {
321- properties .putAll ( map );
311+ else {
312+ final Properties properties = new Properties ();
313+ if ( isNotEmpty ( map ) ) {
314+ properties .putAll ( map );
315+ }
316+ return properties ;
322317 }
323-
324- return properties ;
325318 }
326319
327320 /**
@@ -354,10 +347,10 @@ public static <T> Set<T> toSmallSet(Set<T> set) {
354347 public static <K , V > Map <K , V > toSmallMap (final Map <K , V > map ) {
355348 switch ( map .size () ) {
356349 case 0 :
357- return Collections . EMPTY_MAP ;
350+ return emptyMap () ;
358351 case 1 :
359352 Map .Entry <K , V > entry = map .entrySet ().iterator ().next ();
360- return Collections . singletonMap ( entry .getKey (), entry .getValue () );
353+ return singletonMap ( entry .getKey (), entry .getValue () );
361354 default :
362355 //TODO assert tests pass even if this is set to return an unmodifiable Map
363356 return map ;
@@ -374,9 +367,9 @@ public static <K, V> Map<K, V> toSmallMap(final Map<K, V> map) {
374367 public static <V > List <V > toSmallList (ArrayList <V > arrayList ) {
375368 switch ( arrayList .size () ) {
376369 case 0 :
377- return Collections . EMPTY_LIST ;
370+ return emptyList () ;
378371 case 1 :
379- return Collections . singletonList ( arrayList .get ( 0 ) );
372+ return singletonList ( arrayList .get ( 0 ) );
380373 default :
381374 arrayList .trimToSize ();
382375 return arrayList ;
@@ -410,48 +403,15 @@ public static <K,S> Map<K, S> asMap(Object[] elements) {
410403 public static Map <String ,String > toMap (String ... pairs ) {
411404 assert pairs .length % 2 == 0 ;
412405 if ( pairs .length == 2 ) {
413- return Collections .singletonMap ( pairs [0 ], pairs [1 ] );
414- }
415-
416- final Map <String ,String > result = new HashMap <>();
417- applyToMap ( result , pairs );
418- return result ;
419- }
420-
421- private static void applyToMap (Map <String ,String > map , String ... pairs ) {
422- assert pairs .length % 2 == 0 ;
423- for ( int i = 0 ; i < pairs .length ; i +=2 ) {
424- map .put ( pairs [i ], pairs [i +1 ] );
425- }
426- }
427-
428- public static Map <String ,?> toMap (Object ... pairs ) {
429- assert pairs .length % 2 == 0 ;
430- if ( pairs .length == 2 ) {
431- return Collections .singletonMap ( (String ) pairs [0 ], pairs [1 ] );
406+ return singletonMap ( pairs [0 ], pairs [1 ] );
432407 }
433-
434- final Map <String ,String > result = new HashMap <>();
435- applyToMap ( result , pairs );
436- return result ;
437- }
438-
439- public static Map <String ,Object > toSettingsMap (Object ... pairs ) {
440- assert pairs .length % 2 == 0 ;
441- if ( pairs .length == 2 ) {
442- return Collections .singletonMap ( (String ) pairs [0 ], pairs [1 ] );
443- }
444-
445- final Map <String ,Object > result = new HashMap <>();
446- applyToMap ( result , pairs );
447- return result ;
448- }
449-
450- @ SuppressWarnings ({ "unchecked" , "rawtypes" })
451- private static void applyToMap (Map <String ,?> map , Object ... pairs ) {
452- assert pairs .length % 2 == 0 ;
453- for ( int i = 0 ; i < pairs .length ; i +=2 ) {
454- ( (Map ) map ).put ( pairs [i ], pairs [i +1 ] );
408+ else {
409+ final Map <String ,String > result = new HashMap <>();
410+ assert pairs .length % 2 == 0 ;
411+ for ( int i = 0 ; i < pairs .length ; i +=2 ) {
412+ result .put ( pairs [i ], pairs [i +1 ] );
413+ }
414+ return result ;
455415 }
456416 }
457417
@@ -465,23 +425,6 @@ public static String[] asPairs(Map<String,String> map) {
465425 return pairs ;
466426 }
467427
468- public static Properties toProperties (Object ... pairs ) {
469- final Properties properties = new Properties ();
470- if ( pairs .length > 0 ) {
471- assert pairs .length % 2 == 0 ;
472- for ( int i = 0 ; i < pairs .length ; i +=2 ) {
473- properties .put ( pairs [i ], pairs [i +1 ] );
474- }
475- }
476- return properties ;
477- }
478-
479- public static void applyToProperties (Properties properties , Object ... pairs ) {
480- assert pairs .length % 2 == 0 ;
481- for ( int i = 0 ; i < pairs .length ; i +=2 ) {
482- properties .put ( pairs [i ], pairs [i +1 ] );
483- }
484- }
485428
486429 public static <O > List <O > combine (List <O > list1 , List <O > list2 ) {
487430 final ArrayList <O > combined = arrayList ( list1 .size () + list2 .size () );
@@ -498,6 +441,7 @@ public static List combineUntyped(List list1, List list2) {
498441 return combined ;
499442 }
500443
444+ @ SafeVarargs
501445 public static <O > List <O > combine (List <O >... lists ) {
502446 final ArrayList <O > combined = new ArrayList <>();
503447 for ( int i = 0 ; i < lists .length ; i ++ ) {
@@ -518,10 +462,11 @@ public static int size(Map<?,?> values) {
518462 return values == null ? 0 : values .size ();
519463 }
520464
465+ @ SafeVarargs
521466 public static <X > Set <X > toSet (X ... values ) {
522467 final HashSet <X > result = new HashSet <>();
523468 if ( isNotEmpty ( values ) ) {
524- result .addAll ( Arrays . asList ( values ) );
469+ result .addAll ( asList ( values ) );
525470 }
526471 return result ;
527472 }
@@ -532,16 +477,19 @@ public static <E> List<E> mutableJoin(Collection<E> first, Collection<E> second)
532477 if ( totalCount == 0 ) {
533478 return new ArrayList <>();
534479 }
535- final ArrayList <E > joined = new ArrayList <>( totalCount );
536- if ( first != null ) {
537- joined .addAll ( first );
538- }
539- if ( second != null ) {
540- joined .addAll ( second );
480+ else {
481+ final ArrayList <E > joined = new ArrayList <>( totalCount );
482+ if ( first != null ) {
483+ joined .addAll ( first );
484+ }
485+ if ( second != null ) {
486+ joined .addAll ( second );
487+ }
488+ return joined ;
541489 }
542- return joined ;
543490 }
544491
492+ @ SafeVarargs
545493 public static <E > List <E > mutableJoin (Collection <E > first , Collection <E >... others ) {
546494 // it can be empty, but not null
547495 assert first != null ;
0 commit comments