15
15
*/
16
16
package org .springframework .data .neo4j .core ;
17
17
18
- import java .util .Collection ;
19
- import java .util .Collections ;
20
- import java .util .List ;
21
- import java .util .Map ;
22
-
23
18
import org .apiguardian .api .API ;
24
19
import org .springframework .core .CollectionFactory ;
25
20
import org .springframework .data .mapping .PersistentPropertyAccessor ;
26
21
import org .springframework .data .neo4j .core .mapping .Neo4jPersistentProperty ;
27
22
23
+ import java .util .Collection ;
24
+ import java .util .Collections ;
25
+ import java .util .Map ;
26
+ import java .util .Optional ;
27
+
28
28
/**
29
29
* Internal helper class that takes care of tracking whether a related object or a collection of related objects was recreated
30
30
* due to changing immutable properties
@@ -45,19 +45,19 @@ enum Cardinality {
45
45
static RelationshipHandler forProperty (Neo4jPersistentProperty property , Object rawValue ) {
46
46
47
47
Cardinality cardinality ;
48
- Collection <Object > newRelationshipObjectCollection = null ;
49
- Map <Object , Object > newRelationshipObjectCollectionMap = null ;
48
+ Collection <Object > newRelationshipObjectCollection = Collections . emptyList () ;
49
+ Map <Object , Object > newRelationshipObjectCollectionMap = Collections . emptyMap () ;
50
50
51
51
// Order is important here, all map based associations are dynamic, but not all dynamic associations are one to many
52
52
if (property .isCollectionLike ()) {
53
53
cardinality = Cardinality .ONE_TO_MANY ;
54
- newRelationshipObjectCollection = CollectionFactory .createApproximateCollection ( rawValue , ((Collection <?>) rawValue ).size ());
54
+ newRelationshipObjectCollection = CollectionFactory .createCollection ( property . getType () , ((Collection <?>) rawValue ).size ());
55
55
} else if (property .isDynamicOneToManyAssociation ()) {
56
56
cardinality = Cardinality .DYNAMIC_ONE_TO_MANY ;
57
- newRelationshipObjectCollectionMap = CollectionFactory .createApproximateMap ( rawValue , ((Map <?, ?>) rawValue ).size ());
57
+ newRelationshipObjectCollectionMap = CollectionFactory .createMap ( property . getType () , ((Map <?, ?>) rawValue ).size ());
58
58
} else if (property .isDynamicAssociation ()) {
59
59
cardinality = Cardinality .DYNAMIC_ONE_TO_ONE ;
60
- newRelationshipObjectCollectionMap = CollectionFactory .createApproximateMap ( rawValue , ((Map <?, ?>) rawValue ).size ());
60
+ newRelationshipObjectCollectionMap = CollectionFactory .createMap ( property . getType () , ((Map <?, ?>) rawValue ).size ());
61
61
} else {
62
62
cardinality = Cardinality .ONE_TO_ONE ;
63
63
}
@@ -76,9 +76,9 @@ static RelationshipHandler forProperty(Neo4jPersistentProperty property, Object
76
76
private final Map <Object , Object > newRelatedObjectsByType ;
77
77
78
78
RelationshipHandler (Neo4jPersistentProperty property ,
79
- Object rawValue , Cardinality cardinality ,
80
- Collection <Object > newRelatedObjects ,
81
- Map <Object , Object > newRelatedObjectsByType ) {
79
+ Object rawValue , Cardinality cardinality ,
80
+ Collection <Object > newRelatedObjects ,
81
+ Map <Object , Object > newRelatedObjectsByType ) {
82
82
this .property = property ;
83
83
this .rawValue = rawValue ;
84
84
this .cardinality = cardinality ;
@@ -87,31 +87,33 @@ static RelationshipHandler forProperty(Neo4jPersistentProperty property, Object
87
87
}
88
88
89
89
void handle (Object relatedValueToStore , Object newRelatedObject , Object potentiallyRecreatedRelatedObject ) {
90
- if (potentiallyRecreatedRelatedObject == newRelatedObject ) {
91
- return ;
92
- } else if (cardinality == Cardinality .ONE_TO_ONE ) {
93
- this .newRelatedObjects = Collections .singletonList (potentiallyRecreatedRelatedObject );
94
- } else if (cardinality == Cardinality .ONE_TO_MANY ) {
95
- newRelatedObjects .add (potentiallyRecreatedRelatedObject );
96
- } else {
97
- Object key = ((Map .Entry <Object , Object >) relatedValueToStore ).getKey ();
98
- if (cardinality == Cardinality .DYNAMIC_ONE_TO_ONE ) {
99
- newRelatedObjectsByType .put (key , potentiallyRecreatedRelatedObject );
90
+
91
+ if (potentiallyRecreatedRelatedObject != newRelatedObject ) {
92
+ if (cardinality == Cardinality .ONE_TO_ONE ) {
93
+ this .newRelatedObjects = Collections .singletonList (potentiallyRecreatedRelatedObject );
94
+ } else if (cardinality == Cardinality .ONE_TO_MANY ) {
95
+ newRelatedObjects .add (potentiallyRecreatedRelatedObject );
100
96
} else {
101
- Collection <Object > newCollection = (Collection <Object >) newRelatedObjectsByType
102
- .computeIfAbsent (key , k -> CollectionFactory .createCollection (
103
- property .getTypeInformation ().getRequiredActualType ().getType (),
104
- ((Collection ) ((Map ) rawValue ).get (key )).size ()));
105
- newCollection .add (potentiallyRecreatedRelatedObject );
97
+ Object key = ((Map .Entry <Object , Object >) relatedValueToStore ).getKey ();
98
+ if (cardinality == Cardinality .DYNAMIC_ONE_TO_ONE ) {
99
+ newRelatedObjectsByType .put (key , potentiallyRecreatedRelatedObject );
100
+ } else {
101
+ Collection <Object > newCollection = (Collection <Object >) newRelatedObjectsByType
102
+ .computeIfAbsent (key , k -> CollectionFactory .createCollection (
103
+ property .getTypeInformation ().getRequiredActualType ().getType (),
104
+ ((Collection ) ((Map ) rawValue ).get (key )).size ()));
105
+ newCollection .add (potentiallyRecreatedRelatedObject );
106
+ }
106
107
}
107
108
}
108
109
}
109
110
110
111
void applyFinalResultToOwner (PersistentPropertyAccessor <?> parentPropertyAccessor ) {
112
+
111
113
Object finalRelation = null ;
112
114
switch (cardinality ) {
113
115
case ONE_TO_ONE :
114
- finalRelation = newRelatedObjects == null ? null : (( List ) newRelatedObjects ). get ( 0 );
116
+ finalRelation = Optional . ofNullable ( newRelatedObjects ). flatMap ( v -> v . stream (). findFirst ()). orElse ( null );
115
117
break ;
116
118
case ONE_TO_MANY :
117
119
if (!newRelatedObjects .isEmpty ()) {
0 commit comments