10
10
import java .sql .SQLException ;
11
11
import java .util .ArrayList ;
12
12
import java .util .Collection ;
13
+ import java .util .HashMap ;
13
14
import java .util .Iterator ;
14
15
import java .util .List ;
15
16
import java .util .Map ;
@@ -491,14 +492,14 @@ public Object replaceElements(
491
492
492
493
// copy elements into newly empty target collection
493
494
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 ) );
496
497
}
497
498
498
499
// if the original is a PersistentCollection, and that original
499
500
// was not flagged as dirty, then reset the target's dirty flag
500
501
// here after the copy operation.
501
- // </p>
502
+ //
502
503
// One thing to be careful of here is a "bare" original collection
503
504
// in which case we should never ever ever reset the dirty flag
504
505
// on the target because we simply do not know...
@@ -563,30 +564,35 @@ private static Serializable createArraySnapshot(
563
564
return array ;
564
565
}
565
566
566
- private static Serializable createMapSnapshot (
567
- Map <?, ? > map ,
567
+ private static < K , V > Serializable createMapSnapshot (
568
+ Map <K , V > map ,
568
569
PersistentCollection <?> result ,
569
570
Type elemType ,
570
571
Object owner ,
571
572
Map <Object , Object > copyCache ,
572
573
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 ;
578
580
}
579
581
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 ();
585
590
final Object resultSnapshotValue = resultSnapshot == null ? null : resultSnapshot .get ( key );
586
591
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 );
588
594
}
589
- return ( Serializable ) targetMap ;
595
+ return snapshot ;
590
596
}
591
597
592
598
private static ArrayList <Object > createListSnapshot (
@@ -650,8 +656,8 @@ private Object replaceOriginal(
650
656
Object owner ,
651
657
Map <Object , Object > copyCache ) {
652
658
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
655
661
final Object result =
656
662
replaceElements ( original ,
657
663
instantiateResultIfNecessary ( original , target ),
@@ -663,9 +669,9 @@ private Object replaceOriginal(
663
669
&& !collection .isDirty ();
664
670
if ( target instanceof PersistentCollection <?> oldCollection
665
671
&& 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
669
675
final CollectionPersister collectionPersister = getPersister ( session );
670
676
final Object key = oldCollection .getKey ();
671
677
final PersistentCollection <?> newCollection = instantiate ( session , collectionPersister , key );
@@ -704,8 +710,8 @@ private Object replaceUninitializedOriginal(
704
710
Object target ,
705
711
SharedSessionContractImplementor session ,
706
712
Map <Object , Object > copyCache ) {
707
- final PersistentCollection <?> persistentCollection = (PersistentCollection <?>) original ;
708
- if ( persistentCollection .hasQueuedOperations () ) {
713
+ final PersistentCollection <?> collection = (PersistentCollection <?>) original ;
714
+ if ( collection .hasQueuedOperations () ) {
709
715
if ( original == target ) {
710
716
// A managed entity with an uninitialized collection is being merged,
711
717
// We need to replace any detached entities in the queued operations
@@ -716,8 +722,7 @@ private Object replaceUninitializedOriginal(
716
722
else {
717
723
// original is a detached copy of the collection;
718
724
// it contains queued operations, which will be ignored
719
- LOG .ignoreQueuedOperationsOnMerge (
720
- collectionInfoString ( getRole (), persistentCollection .getKey () ) );
725
+ LOG .ignoreQueuedOperationsOnMerge ( collectionInfoString ( getRole (), collection .getKey () ) );
721
726
}
722
727
}
723
728
return target ;
@@ -827,8 +832,8 @@ else if ( overridingEager != null ? overridingEager : !persister.isLazy() ) {
827
832
persistenceContext .addCollectionHolder ( collection );
828
833
}
829
834
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 ) );
832
837
}
833
838
return collection ;
834
839
}
0 commit comments