16
16
package org .springframework .data .neo4j .core .mapping ;
17
17
18
18
import java .lang .reflect .Modifier ;
19
+ import java .util .ArrayList ;
19
20
import java .util .Collection ;
20
21
import java .util .Collections ;
21
22
import java .util .HashMap ;
24
25
import java .util .Map ;
25
26
import java .util .Set ;
26
27
import java .util .function .BiFunction ;
27
- import java .util .function .Function ;
28
28
29
29
import org .springframework .data .mapping .context .AbstractMappingContext ;
30
30
import org .springframework .lang .Nullable ;
@@ -122,15 +122,12 @@ private NodeDescriptionAndLabels computeConcreteNodeDescription(NodeDescription<
122
122
Map <NodeDescription <?>, Integer > unmatchedLabelsCache = new HashMap <>();
123
123
List <String > mostMatchingStaticLabels = null ;
124
124
125
- // Remove is faster than "stream, filter, count".
126
- Function <NodeDescription <?>, Integer > unmatchedLabelsCount =
127
- (nodeDescription ) -> {
128
- Set <String > staticLabelsClone = new HashSet <>(labels );
129
- nodeDescription .getStaticLabels ().forEach (staticLabelsClone ::remove );
130
- return staticLabelsClone .size ();
131
- };
132
-
133
125
for (NodeDescription <?> nd : haystack ) {
126
+
127
+ if (Modifier .isAbstract (nd .getUnderlyingClass ().getModifiers ())) {
128
+ continue ;
129
+ }
130
+
134
131
List <String > staticLabels = nd .getStaticLabels ();
135
132
136
133
if (staticLabels .containsAll (labels )) {
@@ -139,17 +136,20 @@ private NodeDescriptionAndLabels computeConcreteNodeDescription(NodeDescription<
139
136
return new NodeDescriptionAndLabels (nd , surplusLabels );
140
137
}
141
138
142
- unmatchedLabelsCache .put (nd , unmatchedLabelsCount .apply (nd ));
143
- if (mostMatchingNodeDescription == null ) {
144
- mostMatchingNodeDescription = nd ;
145
- mostMatchingStaticLabels = staticLabels ;
146
- continue ;
139
+ int unmatchedLabelsCount = 0 ;
140
+ List <String > matchingLabels = new ArrayList <>();
141
+ for (String staticLabel : staticLabels ) {
142
+ if (labels .contains (staticLabel )) {
143
+ matchingLabels .add (staticLabel );
144
+ } else {
145
+ unmatchedLabelsCount ++;
146
+ }
147
147
}
148
148
149
- Integer newUnmatchedLabelCount = unmatchedLabelsCache .get (nd );
150
- Integer existingUnmatchedLabelCount = unmatchedLabelsCache .get (mostMatchingNodeDescription );
151
- if (newUnmatchedLabelCount < existingUnmatchedLabelCount ) {
149
+ unmatchedLabelsCache .put (nd , unmatchedLabelsCount );
150
+ if (mostMatchingNodeDescription == null || unmatchedLabelsCount < unmatchedLabelsCache .get (mostMatchingNodeDescription )) {
152
151
mostMatchingNodeDescription = nd ;
152
+ mostMatchingStaticLabels = matchingLabels ;
153
153
}
154
154
}
155
155
0 commit comments