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