HHH-8681 NonNullableTransientDependencies should initialize identityMap lazily

This commit is contained in:
Sanne Grinovero 2013-11-13 13:55:05 +00:00 committed by Steve Ebersole
parent b2479bf00d
commit be44a1984e
1 changed files with 26 additions and 10 deletions

View File

@ -27,6 +27,7 @@ import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;
import java.util.Collections;
import org.hibernate.engine.spi.SessionImplementor;
@ -35,12 +36,15 @@ import org.hibernate.engine.spi.SessionImplementor;
*
* @author Gail Badner
*/
public class NonNullableTransientDependencies {
public final class NonNullableTransientDependencies {
// Multiple property paths can refer to the same transient entity, so use Set<String>
// for the map value.
private final Map<Object,Set<String>> propertyPathsByTransientEntity = new IdentityHashMap<Object,Set<String>>();
private Map<Object,Set<String>> propertyPathsByTransientEntity; // lazily initialized
void add(String propertyName, Object transientEntity) {
if ( propertyPathsByTransientEntity == null ) {
propertyPathsByTransientEntity = new IdentityHashMap<Object, Set<String>>();
}
Set<String> propertyPaths = propertyPathsByTransientEntity.get( transientEntity );
if ( propertyPaths == null ) {
propertyPaths = new HashSet<String>();
@ -50,7 +54,12 @@ public class NonNullableTransientDependencies {
}
public Iterable<Object> getNonNullableTransientEntities() {
return propertyPathsByTransientEntity.keySet();
if ( propertyPathsByTransientEntity == null ) {
return Collections.emptyList();
}
else {
return propertyPathsByTransientEntity.keySet();
}
}
/**
@ -60,8 +69,13 @@ public class NonNullableTransientDependencies {
*
* @return The property paths
*/
public Iterable<String> getNonNullableTransientPropertyPaths(Object entity) {
return propertyPathsByTransientEntity.get( entity );
public Iterable<String> getNonNullableTransientPropertyPaths(final Object entity) {
if ( propertyPathsByTransientEntity == null ) {
return Collections.emptyList();
}
else {
return propertyPathsByTransientEntity.get( entity );
}
}
/**
@ -70,7 +84,7 @@ public class NonNullableTransientDependencies {
* @return {@code true} indicates there are no path tracked here currently
*/
public boolean isEmpty() {
return propertyPathsByTransientEntity.isEmpty();
return propertyPathsByTransientEntity == null || propertyPathsByTransientEntity.isEmpty();
}
/**
@ -81,7 +95,7 @@ public class NonNullableTransientDependencies {
* @throws IllegalStateException If the entity had tracked paths
*/
public void resolveNonNullableTransientEntity(Object entity) {
if ( propertyPathsByTransientEntity.remove( entity ) == null ) {
if ( propertyPathsByTransientEntity != null && propertyPathsByTransientEntity.remove( entity ) == null ) {
throw new IllegalStateException( "Attempt to resolve a non-nullable, transient entity that is not a dependency." );
}
}
@ -95,9 +109,11 @@ public class NonNullableTransientDependencies {
*/
public String toLoggableString(SessionImplementor session) {
final StringBuilder sb = new StringBuilder( getClass().getSimpleName() ).append( '[' );
for ( Map.Entry<Object,Set<String>> entry : propertyPathsByTransientEntity.entrySet() ) {
sb.append( "transientEntityName=" ).append( session.bestGuessEntityName( entry.getKey() ) );
sb.append( " requiredBy=" ).append( entry.getValue() );
if ( propertyPathsByTransientEntity != null ) {
for ( Map.Entry<Object,Set<String>> entry : propertyPathsByTransientEntity.entrySet() ) {
sb.append( "transientEntityName=" ).append( session.bestGuessEntityName( entry.getKey() ) );
sb.append( " requiredBy=" ).append( entry.getValue() );
}
}
sb.append( ']' );
return sb.toString();