mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 08:51:12 +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);
|
return decode(new String(chars, begin, end - begin), hasPercent, hasPlus);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Match {
|
public final static class Match {
|
||||||
public IPersistentMap params;
|
public final IPersistentMap params;
|
||||||
public final Object data;
|
public final Object data;
|
||||||
|
|
||||||
public Match(IPersistentMap params, Object data) {
|
public Match(IPersistentMap params, Object data) {
|
||||||
|
|
@ -47,6 +47,10 @@ public class Trie {
|
||||||
this.data = data;
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Match assoc(Object key, Object value) {
|
||||||
|
return new Match(params.assoc(key, value), data);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
Map<Object, Object> m = new HashMap<>();
|
Map<Object, Object> m = new HashMap<>();
|
||||||
|
|
@ -113,19 +117,16 @@ public class Trie {
|
||||||
}
|
}
|
||||||
|
|
||||||
static final class DataMatcher implements Matcher {
|
static final class DataMatcher implements Matcher {
|
||||||
|
private final Match match;
|
||||||
private final IPersistentMap params;
|
|
||||||
private final Object data;
|
|
||||||
|
|
||||||
DataMatcher(IPersistentMap params, Object data) {
|
DataMatcher(IPersistentMap params, Object data) {
|
||||||
this.params = params;
|
this.match = new Match(params, data);
|
||||||
this.data = data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Match match(int i, int max, char[] path) {
|
public Match match(int i, int max, char[] path) {
|
||||||
if (i == max) {
|
if (i == max) {
|
||||||
return new Match(params, data);
|
return match;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -142,7 +143,7 @@ public class Trie {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
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);
|
final Match m = child.match(stop, max, path);
|
||||||
if (m != null) {
|
return m != null ? m.assoc(key, decode(new String(path, i, stop - i), hasPercent, hasPlus)) : null;
|
||||||
m.params = m.params.assoc(key, decode(new String(path, i, stop - i), hasPercent, hasPlus));
|
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -219,7 +217,7 @@ public class Trie {
|
||||||
@Override
|
@Override
|
||||||
public Match match(int i, int max, char[] path) {
|
public Match match(int i, int max, char[] path) {
|
||||||
if (i <= max) {
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue