Skip to content

Commit c94aa7d

Browse files
committed
come cleanups in CollectionType; use wildcard capture
1 parent 2cf69b3 commit c94aa7d

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

hibernate-core/src/main/java/org/hibernate/type/CollectionType.java

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.sql.SQLException;
1111
import java.util.ArrayList;
1212
import java.util.Collection;
13+
import java.util.HashMap;
1314
import java.util.Iterator;
1415
import java.util.List;
1516
import java.util.Map;
@@ -491,14 +492,14 @@ public Object replaceElements(
491492

492493
// copy elements into newly empty target collection
493494
final Type elemType = getElementType( session.getFactory() );
494-
for ( Object o : (Collection) original ) {
495-
result.add( elemType.replace( o, null, session, owner, copyCache ) );
495+
for ( Object element : (Collection) original ) {
496+
result.add( elemType.replace( element, null, session, owner, copyCache ) );
496497
}
497498

498499
// if the original is a PersistentCollection, and that original
499500
// was not flagged as dirty, then reset the target's dirty flag
500501
// here after the copy operation.
501-
// </p>
502+
//
502503
// One thing to be careful of here is a "bare" original collection
503504
// in which case we should never ever ever reset the dirty flag
504505
// on the target because we simply do not know...
@@ -563,30 +564,35 @@ private static Serializable createArraySnapshot(
563564
return array;
564565
}
565566

566-
private static Serializable createMapSnapshot(
567-
Map<?, ?> map,
567+
private static <K,V> Serializable createMapSnapshot(
568+
Map<K, V> map,
568569
PersistentCollection<?> result,
569570
Type elemType,
570571
Object owner,
571572
Map<Object, Object> copyCache,
572573
SharedSessionContractImplementor session) {
573-
final Map<?,?> resultSnapshot = (Map<?,?>) result.getStoredSnapshot();
574-
final Map<Object, Object> targetMap;
575-
if ( map instanceof SortedMap<?,?> sortedMap ) {
576-
//noinspection unchecked, rawtypes
577-
targetMap = new TreeMap( sortedMap.comparator() );
574+
final Map<K, V> targetMap;
575+
final Serializable snapshot;
576+
if ( map instanceof SortedMap<K,V> sortedMap ) {
577+
final TreeMap<K, V> treeMap = new TreeMap<>( sortedMap.comparator() );
578+
targetMap = treeMap;
579+
snapshot = treeMap;
578580
}
579581
else {
580-
targetMap = mapOfSize( map.size() );
581-
}
582-
for ( Map.Entry<?,?> entry : map.entrySet() ) {
583-
final Object key = entry.getKey();
584-
final Object value = entry.getValue();
582+
final HashMap<K, V> hashMap = mapOfSize( map.size() );
583+
targetMap = hashMap;
584+
snapshot = hashMap;
585+
}
586+
final Map<?, ?> resultSnapshot = (Map<?,?>) result.getStoredSnapshot();
587+
for ( Map.Entry<K,V> entry : map.entrySet() ) {
588+
final K key = entry.getKey();
589+
final V value = entry.getValue();
585590
final Object resultSnapshotValue = resultSnapshot == null ? null : resultSnapshot.get( key );
586591
final Object newValue = elemType.replace( value, resultSnapshotValue, session, owner, copyCache );
587-
targetMap.put( key == value ? newValue : key, newValue );
592+
//noinspection unchecked
593+
targetMap.put( key == value ? (K) newValue : key, (V) newValue );
588594
}
589-
return (Serializable) targetMap;
595+
return snapshot;
590596
}
591597

592598
private static ArrayList<Object> createListSnapshot(
@@ -650,8 +656,8 @@ private Object replaceOriginal(
650656
Object owner,
651657
Map<Object, Object> copyCache) {
652658

653-
//for arrays, replaceElements() may return a different reference, since
654-
//the array length might not match
659+
// for arrays, replaceElements() may return a different reference, since
660+
// the array length might not match
655661
final Object result =
656662
replaceElements( original,
657663
instantiateResultIfNecessary( original, target ),
@@ -663,9 +669,9 @@ private Object replaceOriginal(
663669
&& !collection.isDirty();
664670
if ( target instanceof PersistentCollection<?> oldCollection
665671
&& oldCollection.isDirectlyAccessible() ) {
666-
// When a replace/merge is requested and the underlying collection is directly accessible,
667-
// use a new persistent collection, to avoid potential issues
668-
// like the underlying collection being unmodifiable and hence failing the element replacement
672+
// When a replacement or merge is requested and the underlying collection is directly accessible,
673+
// use a new persistent collection, to avoid potential issues like the underlying collection being
674+
// unmodifiable and hence failing the element replacement
669675
final CollectionPersister collectionPersister = getPersister( session );
670676
final Object key = oldCollection.getKey();
671677
final PersistentCollection<?> newCollection = instantiate( session, collectionPersister, key );
@@ -704,8 +710,8 @@ private Object replaceUninitializedOriginal(
704710
Object target,
705711
SharedSessionContractImplementor session,
706712
Map<Object, Object> copyCache) {
707-
final PersistentCollection<?> persistentCollection = (PersistentCollection<?>) original;
708-
if ( persistentCollection.hasQueuedOperations() ) {
713+
final PersistentCollection<?> collection = (PersistentCollection<?>) original;
714+
if ( collection.hasQueuedOperations() ) {
709715
if ( original == target ) {
710716
// A managed entity with an uninitialized collection is being merged,
711717
// We need to replace any detached entities in the queued operations
@@ -716,8 +722,7 @@ private Object replaceUninitializedOriginal(
716722
else {
717723
// original is a detached copy of the collection;
718724
// it contains queued operations, which will be ignored
719-
LOG.ignoreQueuedOperationsOnMerge(
720-
collectionInfoString( getRole(), persistentCollection.getKey() ) );
725+
LOG.ignoreQueuedOperationsOnMerge( collectionInfoString( getRole(), collection.getKey() ) );
721726
}
722727
}
723728
return target;
@@ -827,8 +832,8 @@ else if ( overridingEager != null ? overridingEager : !persister.isLazy() ) {
827832
persistenceContext.addCollectionHolder( collection );
828833
}
829834
if ( LOG.isTraceEnabled() ) {
830-
LOG.tracef( "Created collection wrapper: %s",
831-
collectionInfoString( persister, collection, key, session ) );
835+
LOG.trace( "Created collection wrapper: "
836+
+ collectionInfoString( persister, collection, key, session ) );
832837
}
833838
return collection;
834839
}

0 commit comments

Comments
 (0)