slight cleanup to old code

This commit is contained in:
Gavin King 2024-11-16 10:35:35 +01:00
parent e1f62098e2
commit b0b258de11
2 changed files with 36 additions and 30 deletions

View File

@ -11,9 +11,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
import org.hibernate.collection.spi.PersistentArrayHolder;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -21,6 +19,9 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.CollectionClassification;
import org.hibernate.persister.collection.CollectionPersister;
import static org.hibernate.Hibernate.isInitialized;
import static org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer.UNFETCHED_PROPERTY;
/**
* A type for persistent arrays.
* @author Gavin King
@ -48,7 +49,7 @@ public class ArrayType extends CollectionType {
@Override
public PersistentCollection<?> instantiate(SharedSessionContractImplementor session, CollectionPersister persister, Object key)
throws HibernateException {
throws HibernateException {
return new PersistentArrayHolder<>(session, persister);
}
@ -75,20 +76,21 @@ public class ArrayType extends CollectionType {
if ( value == null ) {
return "null";
}
int length = Array.getLength(value);
List<String> list = new ArrayList<>(length);
Type elemType = getElementType(factory);
for ( int i=0; i<length; i++ ) {
Object element = Array.get(value, i);
if ( element == LazyPropertyInitializer.UNFETCHED_PROPERTY
|| !Hibernate.isInitialized( element ) ) {
list.add( "<uninitialized>" );
}
else {
list.add( elemType.toLoggableString( element, factory ) );
else {
final int length = Array.getLength( value );
final Type elemType = getElementType( factory );
final List<String> list = new ArrayList<>( length );
for ( int i = 0; i < length; i++ ) {
list.add( loggableString( factory, Array.get( value, i ), elemType ) );
}
return list.toString();
}
return list.toString();
}
private static String loggableString(SessionFactoryImplementor factory, Object element, Type elemType) {
return element == UNFETCHED_PROPERTY || !isInitialized( element )
? "<uninitialized>"
: elemType.toLoggableString( element, factory );
}
@Override
@ -101,16 +103,16 @@ public class ArrayType extends CollectionType {
Object original,
Object target,
Object owner,
Map copyCache,
Map<Object, Object> copyCache,
SharedSessionContractImplementor session) throws HibernateException {
int length = Array.getLength(original);
final int length = Array.getLength(original);
if ( length!=Array.getLength(target) ) {
//note: this affects the return value!
target=instantiateResult(original);
}
Type elemType = getElementType( session.getFactory() );
final Type elemType = getElementType( session.getFactory() );
for ( int i=0; i<length; i++ ) {
Array.set( target, i, elemType.replace( Array.get(original, i), null, session, owner, copyCache ) );
}
@ -126,10 +128,10 @@ public class ArrayType extends CollectionType {
@Override
public Object indexOf(Object array, Object element) {
int length = Array.getLength(array);
final int length = Array.getLength(array);
for ( int i=0; i<length; i++ ) {
//TODO: proxies!
if ( Array.get(array, i)==element ) {
if ( Array.get(array, i) == element ) {
return i;
}
}

View File

@ -57,22 +57,25 @@ public class MapType extends CollectionType {
: new HashMap<>( anticipatedSize + (int) ( anticipatedSize * .75f ), .75f );
}
@Override @SuppressWarnings("rawtypes")
@Override @SuppressWarnings({"rawtypes", "unchecked"})
public Object replaceElements(
final Object original,
final Object target,
final Object owner,
final Map copyCache,
final Map<Object, Object> copyCache,
final SharedSessionContractImplementor session) throws HibernateException {
CollectionPersister cp = session.getFactory().getRuntimeMetamodels().getMappingMetamodel().getCollectionDescriptor( getRole() );
final CollectionPersister persister =
session.getFactory().getRuntimeMetamodels().getMappingMetamodel()
.getCollectionDescriptor( getRole() );
Map result = (Map) target;
final Map source = (Map) original;
final Map result = (Map) target;
result.clear();
for ( Object o : ( (Map) original ).entrySet() ) {
Map.Entry me = (Map.Entry) o;
Object key = cp.getIndexType().replace( me.getKey(), null, session, owner, copyCache );
Object value = cp.getElementType().replace( me.getValue(), null, session, owner, copyCache );
for ( Object entry : source.entrySet() ) {
final Map.Entry me = (Map.Entry) entry;
final Object key = persister.getIndexType().replace( me.getKey(), null, session, owner, copyCache );
final Object value = persister.getElementType().replace( me.getValue(), null, session, owner, copyCache );
result.put( key, value );
}
@ -82,8 +85,9 @@ public class MapType extends CollectionType {
@Override @SuppressWarnings("rawtypes")
public Object indexOf(Object collection, Object element) {
for ( Object o : ( (Map) collection ).entrySet() ) {
Map.Entry me = (Map.Entry) o;
final Map map = (Map) collection;
for ( Object entry : map.entrySet() ) {
final Map.Entry me = (Map.Entry) entry;
//TODO: proxies!
if ( me.getValue() == element ) {
return me.getKey();