From 76089ae151529184696c05ec6ed752d623771f79 Mon Sep 17 00:00:00 2001 From: Nathan Xu Date: Fri, 3 Jul 2020 11:25:35 -0400 Subject: [PATCH] enrich CollectionHelper and make more use of it in existing code --- .../internal/StandardOrderedMapSemantics.java | 3 +- .../internal/StandardOrderedSetSemantics.java | 3 +- .../org/hibernate/engine/spi/ActionQueue.java | 3 +- .../hibernate/engine/spi/BatchFetchQueue.java | 5 +- .../util/collections/CollectionHelper.java | 83 +++++++++++++++---- .../entity/JoinedSubclassEntityPersister.java | 7 +- .../org/hibernate/type/OrderedMapType.java | 6 +- .../org/hibernate/type/OrderedSetType.java | 6 +- .../main/java/org/hibernate/type/SetType.java | 4 +- 9 files changed, 83 insertions(+), 37 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/collection/internal/StandardOrderedMapSemantics.java b/hibernate-core/src/main/java/org/hibernate/collection/internal/StandardOrderedMapSemantics.java index 82486edc47..cd4623204e 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/internal/StandardOrderedMapSemantics.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/internal/StandardOrderedMapSemantics.java @@ -12,6 +12,7 @@ import java.util.Map; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.spi.SharedSessionContractImplementor; +import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.metamodel.CollectionClassification; import org.hibernate.persister.collection.CollectionPersister; @@ -36,7 +37,7 @@ public class StandardOrderedMapSemantics extends AbstractMapSemantics instantiateRaw( int anticipatedSize, CollectionPersister collectionDescriptor) { - return anticipatedSize < 1 ? new LinkedHashMap<>() : new LinkedHashMap<>( anticipatedSize ); + return anticipatedSize < 1 ? CollectionHelper.linkedMap() : CollectionHelper.linkedMapOfSize( anticipatedSize ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/collection/internal/StandardOrderedSetSemantics.java b/hibernate-core/src/main/java/org/hibernate/collection/internal/StandardOrderedSetSemantics.java index 55da6127d4..5543410fee 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/internal/StandardOrderedSetSemantics.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/internal/StandardOrderedSetSemantics.java @@ -12,6 +12,7 @@ import java.util.Set; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.spi.SharedSessionContractImplementor; +import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.metamodel.CollectionClassification; import org.hibernate.persister.collection.CollectionPersister; @@ -36,7 +37,7 @@ public class StandardOrderedSetSemantics extends AbstractSetSemantics instantiateRaw( int anticipatedSize, CollectionPersister collectionDescriptor) { - return anticipatedSize < 1 ? new LinkedHashSet<>() : new LinkedHashSet<>( anticipatedSize ); + return anticipatedSize < 1 ? CollectionHelper.linkedSet() : CollectionHelper.linkedSetOfSize( anticipatedSize ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/ActionQueue.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/ActionQueue.java index 9a4599436e..7dcc2e620f 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/ActionQueue.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/ActionQueue.java @@ -45,6 +45,7 @@ import org.hibernate.cache.CacheException; import org.hibernate.engine.internal.NonNullableTransientDependencies; import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreMessageLogger; +import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.metadata.ClassMetadata; import org.hibernate.proxy.HibernateProxy; import org.hibernate.proxy.LazyInitializer; @@ -105,7 +106,7 @@ public class ActionQueue { */ private static final LinkedHashMap,ListProvider> EXECUTABLE_LISTS_MAP; static { - EXECUTABLE_LISTS_MAP = new LinkedHashMap<>( 8 ); + EXECUTABLE_LISTS_MAP = CollectionHelper.linkedMapOfSize( 8 ); EXECUTABLE_LISTS_MAP.put( OrphanRemovalAction.class, diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/BatchFetchQueue.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/BatchFetchQueue.java index f36ba961ba..43d894eda2 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/BatchFetchQueue.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/BatchFetchQueue.java @@ -6,7 +6,6 @@ */ package org.hibernate.engine.spi; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; @@ -140,7 +139,7 @@ public class BatchFetchQueue { } final LinkedHashSet keysForEntity = batchLoadableEntityKeys.computeIfAbsent( key.getEntityName(), - k -> new LinkedHashSet<>( 8 ) + k -> CollectionHelper.linkedSetOfSize( 8 ) ); keysForEntity.add( key ); @@ -260,7 +259,7 @@ public class BatchFetchQueue { final LinkedHashMap map = batchLoadableCollections.computeIfAbsent( persister.getRole(), - k -> new LinkedHashMap<>( 16 ) + k -> CollectionHelper.linkedMapOfSize( 16 ) ); map.put( ce, collection ); diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/collections/CollectionHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/collections/CollectionHelper.java index 211567650b..1d6081740e 100755 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/collections/CollectionHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/collections/CollectionHelper.java @@ -11,7 +11,8 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.List; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.Map; import java.util.Properties; import java.util.Set; @@ -19,7 +20,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; /** - * Various help for handling collections. + * Various helper util methods for handling collections. * * @author Gavin King * @author Steve Ebersole @@ -29,22 +30,6 @@ public final class CollectionHelper { public static final int MINIMUM_INITIAL_CAPACITY = 16; public static final float LOAD_FACTOR = 0.75f; - /** - * @deprecated use {@link java.util.Collections#EMPTY_LIST} or {@link java.util.Collections#emptyList()} instead - */ - @Deprecated - public static final List EMPTY_LIST = Collections.EMPTY_LIST; - /** - * @deprecated use {@link java.util.Collections#EMPTY_LIST} or {@link java.util.Collections#emptyList()} instead - */ - @Deprecated - public static final Collection EMPTY_COLLECTION = Collections.EMPTY_LIST; - /** - * @deprecated use {@link java.util.Collections#EMPTY_MAP} or {@link java.util.Collections#emptyMap()} instead - */ - @Deprecated - public static final Map EMPTY_MAP = Collections.EMPTY_MAP; - private CollectionHelper() { } @@ -61,6 +46,37 @@ public final class CollectionHelper { return new HashMap<>( determineProperSizing( size ), LOAD_FACTOR ); } + /** + * Build a properly sized linked map, especially handling load size and load factor to prevent immediate resizing. + *

+ * Especially helpful for copy map contents. + * + * @param size The size to make the map. + * + * @return The sized linked map. + */ + public static LinkedHashMap linkedMapOfSize(int size) { + return new LinkedHashMap<>( determineProperSizing( size ), LOAD_FACTOR ); + } + + /** + * Build a map whose size is unknown. + * + * @return The map. + */ + public static HashMap map() { + return new HashMap<>(); + } + + /** + * Build a linked map whose size is unknown. + * + * @return The linked map. + */ + public static LinkedHashMap linkedMap() { + return new LinkedHashMap<>(); + } + /** * Build a properly sized set, especially handling load size and load factor to prevent immediate resizing. *

@@ -74,6 +90,37 @@ public final class CollectionHelper { return new HashSet<>( determineProperSizing( size ), LOAD_FACTOR ); } + /** + * Build a set whose size is unknown. + * + * @return The set. + */ + public static HashSet set() { + return new HashSet<>(); + } + + /** + * Build a properly sized linked set, especially handling load size and load factor to prevent immediate resizing. + *

+ * Especially helpful for copy set contents. + * + * @param size The size to make the set. + * + * @return The sized linked set. + */ + public static LinkedHashSet linkedSetOfSize(int size) { + return new LinkedHashSet<>( determineProperSizing( size ), LOAD_FACTOR ); + } + + /** + * Build a linked set whose size is unknown. + * + * @return The linked set. + */ + public static LinkedHashSet linkedSet() { + return new LinkedHashSet<>(); + } + /** * Given a map, determine the proper initial size for a new Map to hold the same number of values. * Specifically we want to account for load size and load factor to prevent immediate resizing. diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java index bc4ee61e64..7d96c2e0ac 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java @@ -531,10 +531,9 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister { if ( persistentClass.isPolymorphic() ) { subclassesByDiscriminatorValue.put( discriminatorValue, getEntityName() ); - final int initialCapacity = CollectionHelper.determineProperSizing( subclassSpan + 1 ); - discriminatorValuesByTableName = new LinkedHashMap<>( initialCapacity ); - discriminatorColumnNameByTableName = new LinkedHashMap<>( initialCapacity ); - subclassNameByTableName = new HashMap<>( initialCapacity ); + discriminatorValuesByTableName = CollectionHelper.linkedMapOfSize( subclassSpan + 1 ); + discriminatorColumnNameByTableName = CollectionHelper.linkedMapOfSize( subclassSpan + 1 ); + subclassNameByTableName = CollectionHelper.mapOfSize( subclassSpan + 1 ); // We need to convert the `discriminatorSQLString` (which is a String read from boot-mapping) into // the type indicated by `#discriminatorType` (String -> Integer, e.g.). try { diff --git a/hibernate-core/src/main/java/org/hibernate/type/OrderedMapType.java b/hibernate-core/src/main/java/org/hibernate/type/OrderedMapType.java index 6fd841a8ea..0a158e398a 100755 --- a/hibernate-core/src/main/java/org/hibernate/type/OrderedMapType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/OrderedMapType.java @@ -5,8 +5,8 @@ * See the lgpl.txt file in the root directory or . */ package org.hibernate.type; -import java.util.LinkedHashMap; +import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.type.spi.TypeConfiguration; /** @@ -21,8 +21,8 @@ public class OrderedMapType extends MapType { @Override public Object instantiate(int anticipatedSize) { return anticipatedSize > 0 - ? new LinkedHashMap( anticipatedSize ) - : new LinkedHashMap(); + ? CollectionHelper.linkedMap() + : CollectionHelper.linkedMapOfSize( anticipatedSize ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/type/OrderedSetType.java b/hibernate-core/src/main/java/org/hibernate/type/OrderedSetType.java index 0d6a441a2f..a3fdbc74af 100755 --- a/hibernate-core/src/main/java/org/hibernate/type/OrderedSetType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/OrderedSetType.java @@ -5,8 +5,8 @@ * See the lgpl.txt file in the root directory or . */ package org.hibernate.type; -import java.util.LinkedHashSet; +import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.type.spi.TypeConfiguration; /** @@ -21,8 +21,8 @@ public class OrderedSetType extends SetType { @Override public Object instantiate(int anticipatedSize) { return anticipatedSize > 0 - ? new LinkedHashSet( anticipatedSize ) - : new LinkedHashSet(); + ? CollectionHelper.linkedSetOfSize( anticipatedSize ) + : CollectionHelper.linkedSet(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/type/SetType.java b/hibernate-core/src/main/java/org/hibernate/type/SetType.java index 4938a20c73..13173a5af9 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/SetType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/SetType.java @@ -6,8 +6,6 @@ */ package org.hibernate.type; -import java.util.HashSet; - import org.hibernate.collection.internal.PersistentSet; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.spi.SharedSessionContractImplementor; @@ -39,7 +37,7 @@ public class SetType extends CollectionType { @Override public Object instantiate(int anticipatedSize) { return anticipatedSize <= 0 - ? new HashSet() + ? CollectionHelper.set() : CollectionHelper.setOfSize( anticipatedSize ); }