Skip to content

Commit e67df59

Browse files
committed
miscellaneous code cleanups
1 parent 7c599ad commit e67df59

File tree

4 files changed

+210
-312
lines changed

4 files changed

+210
-312
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.hibernate.internal.CoreLogging;
4444
import org.hibernate.internal.CoreMessageLogger;
4545
import org.hibernate.internal.util.StringHelper;
46-
import org.hibernate.internal.util.collections.CollectionHelper;
4746
import org.hibernate.mapping.GeneratorCreator;
4847
import org.hibernate.mapping.KeyValue;
4948
import org.hibernate.mapping.PersistentClass;
@@ -74,6 +73,7 @@
7473
import static org.hibernate.id.IdentifierGenerator.GENERATOR_NAME;
7574
import static org.hibernate.internal.util.NullnessUtil.castNonNull;
7675
import static org.hibernate.internal.util.StringHelper.qualify;
76+
import static org.hibernate.internal.util.collections.CollectionHelper.combineUntyped;
7777
import static org.hibernate.resource.beans.internal.Helper.allowExtensionsInCdi;
7878

7979
/**
@@ -883,7 +883,7 @@ static void createIdGeneratorsFromGeneratorAnnotations(
883883
Locale.ROOT,
884884
"Identifier attribute '%s' has too many generator annotations: %s",
885885
getPath( propertyHolder, inferredData ),
886-
CollectionHelper.combineUntyped( idGeneratorAnnotations, generatorAnnotations )
886+
combineUntyped( idGeneratorAnnotations, generatorAnnotations )
887887
) );
888888
}
889889
if ( !idGeneratorAnnotations.isEmpty() ) {

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

Lines changed: 65 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package org.hibernate.internal.util.collections;
66

77
import java.util.ArrayList;
8-
import java.util.Arrays;
98
import java.util.Collection;
109
import java.util.Collections;
1110
import java.util.HashMap;
@@ -20,6 +19,13 @@
2019
import java.util.function.BiConsumer;
2120
import 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

Comments
 (0)