32
32
import java .util .Map ;
33
33
import java .util .Map .Entry ;
34
34
import java .util .function .BiConsumer ;
35
+ import java .util .function .BiFunction ;
35
36
import java .util .function .Predicate ;
36
37
37
38
/**
@@ -63,7 +64,18 @@ public RadixTree() {
63
64
this (true , '/' );
64
65
}
65
66
67
+ /**
68
+ * This is a default implementation that does not check for equality.
69
+ */
66
70
public T addPath (PathExpression path , T value ) {
71
+ return addPath (path , value , (t , t2 ) -> Boolean .TRUE );
72
+ }
73
+
74
+ /**
75
+ * When equalFunction is not null, if a node is found at the path, it will not be returned immediately; <br/>
76
+ * instead, whether to return it depends on the result of the function.
77
+ */
78
+ public T addPath (PathExpression path , T value , BiFunction <T , T , Boolean > equalFunction ) {
67
79
if (path .isDirect ()) {
68
80
KeyString key = new KeyString (path .getPath (), caseSensitive );
69
81
List <Match <T >> matches = directPathMap .computeIfAbsent (key , k -> new ArrayList <>());
@@ -83,9 +95,16 @@ public T addPath(PathExpression path, T value) {
83
95
Node <T > child = getChild (current , segments [i ]);
84
96
if (i == len - 1 ) {
85
97
List <Pair <PathExpression , T >> values = child .values ;
86
- for (int j = 0 , size = values .size (); j < size ; j ++) {
87
- if (values .get (j ).getLeft ().equals (path )) {
88
- return values .get (j ).getRight ();
98
+ for (Pair <PathExpression , T > pathExpressionTPair : values ) {
99
+ if (pathExpressionTPair .getLeft ().equals (path )) {
100
+ T rightVal = pathExpressionTPair .getRight ();
101
+ if (equalFunction == null ) {
102
+ return rightVal ;
103
+ } else {
104
+ if (equalFunction .apply (rightVal , value )) {
105
+ return rightVal ;
106
+ }
107
+ }
89
108
}
90
109
}
91
110
values .add (Pair .of (path , value ));
0 commit comments