mirror of
https://github.com/metosin/reitit.git
synced 2025-12-16 16:01:11 +00:00
Make Trie$Match immutable
This commit is contained in:
parent
60ee39bd53
commit
4178acde5f
1 changed files with 12 additions and 14 deletions
|
|
@ -38,8 +38,8 @@ public class Trie {
|
|||
return decode(new String(chars, begin, end - begin), hasPercent, hasPlus);
|
||||
}
|
||||
|
||||
public static class Match {
|
||||
public IPersistentMap params;
|
||||
public final static class Match {
|
||||
public final IPersistentMap params;
|
||||
public final Object data;
|
||||
|
||||
public Match(IPersistentMap params, Object data) {
|
||||
|
|
@ -47,6 +47,10 @@ public class Trie {
|
|||
this.data = data;
|
||||
}
|
||||
|
||||
Match assoc(Object key, Object value) {
|
||||
return new Match(params.assoc(key, value), data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
Map<Object, Object> m = new HashMap<>();
|
||||
|
|
@ -113,19 +117,16 @@ public class Trie {
|
|||
}
|
||||
|
||||
static final class DataMatcher implements Matcher {
|
||||
|
||||
private final IPersistentMap params;
|
||||
private final Object data;
|
||||
private final Match match;
|
||||
|
||||
DataMatcher(IPersistentMap params, Object data) {
|
||||
this.params = params;
|
||||
this.data = data;
|
||||
this.match = new Match(params, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Match match(int i, int max, char[] path) {
|
||||
if (i == max) {
|
||||
return new Match(params, data);
|
||||
return match;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -142,7 +143,7 @@ public class Trie {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return (data != null ? data.toString() : "nil");
|
||||
return (match.data != null ? match.data.toString() : "nil");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -177,10 +178,7 @@ public class Trie {
|
|||
}
|
||||
}
|
||||
final Match m = child.match(stop, max, path);
|
||||
if (m != null) {
|
||||
m.params = m.params.assoc(key, decode(new String(path, i, stop - i), hasPercent, hasPlus));
|
||||
}
|
||||
return m;
|
||||
return m != null ? m.assoc(key, decode(new String(path, i, stop - i), hasPercent, hasPlus)) : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -219,7 +217,7 @@ public class Trie {
|
|||
@Override
|
||||
public Match match(int i, int max, char[] path) {
|
||||
if (i <= max) {
|
||||
return new Match(params.assoc(parameter, decode(path, i, max)), data);
|
||||
return new Match(params, data).assoc(parameter, decode(path, i, max));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue