mirror of
https://github.com/metosin/reitit.git
synced 2026-02-18 08:45:55 +00:00
Dead code
This commit is contained in:
parent
19213dcba7
commit
54aded4442
4 changed files with 0 additions and 270 deletions
|
|
@ -1,77 +0,0 @@
|
||||||
package reitit;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class MatchResult {
|
|
||||||
|
|
||||||
public static final MatchResult NO_MATCH = null;
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public static final Map<?, String> NO_PARAMS = Collections.EMPTY_MAP;
|
|
||||||
|
|
||||||
public static final int FULL_MATCH_INDEX = -1;
|
|
||||||
|
|
||||||
public static final MatchResult FULL_MATCH_NO_PARAMS = new MatchResult(NO_PARAMS, FULL_MATCH_INDEX);
|
|
||||||
|
|
||||||
private final Map<?, String> params;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* End index in the URI when match stopped. -1 implies match fully ended.
|
|
||||||
*/
|
|
||||||
private final int endIndex; // excluding
|
|
||||||
|
|
||||||
protected MatchResult(Map<?, String> params, int endIndex) {
|
|
||||||
this.params = params;
|
|
||||||
this.endIndex = endIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----- factory methods -----
|
|
||||||
|
|
||||||
public static MatchResult partialMatch(Map<?, String> params, int endIndex) {
|
|
||||||
return new MatchResult(params, endIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static MatchResult partialMatch(int endIndex) {
|
|
||||||
return new MatchResult(NO_PARAMS, endIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static MatchResult fullMatch(Map<?, String> params) {
|
|
||||||
return new MatchResult(params, FULL_MATCH_INDEX);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----- utility methods -----
|
|
||||||
|
|
||||||
public Map<?, String> getParams() {
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getEndIndex() {
|
|
||||||
return endIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isFullMatch() {
|
|
||||||
return endIndex == FULL_MATCH_INDEX;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----- overridden methods -----
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return String.format("params: %s, endIndex: %d", params, endIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (obj instanceof MatchResult) {
|
|
||||||
MatchResult other = (MatchResult) obj;
|
|
||||||
return other.params.equals(params) && other.endIndex == endIndex;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return toString().hashCode();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,97 +0,0 @@
|
||||||
package reitit;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class Segment2 {
|
|
||||||
|
|
||||||
private List<Edge> edges = new ArrayList<>();
|
|
||||||
private Object data;
|
|
||||||
|
|
||||||
public boolean isLeaf() {
|
|
||||||
return edges.isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Edge {
|
|
||||||
String path;
|
|
||||||
Segment2 segment;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Object lookup(Segment2 root, String path) {
|
|
||||||
Segment2 segment = root;
|
|
||||||
Integer pathLength = path.length();
|
|
||||||
|
|
||||||
while (segment != null && !segment.isLeaf()) {
|
|
||||||
Edge edge = null;
|
|
||||||
for (Edge e : segment.edges) {
|
|
||||||
System.out.println("EDGE:" + e.path + "/" + e.segment);
|
|
||||||
if (path.equals(e.path)) {
|
|
||||||
edge = e;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (edge != null) {
|
|
||||||
segment = edge.segment;
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return segment != null ? segment.data : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Edge endpoint(String path) {
|
|
||||||
Edge edge = new Edge();
|
|
||||||
edge.path = path;
|
|
||||||
Segment2 s = new Segment2();
|
|
||||||
s.data = path;
|
|
||||||
edge.segment = s;
|
|
||||||
return edge;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Edge context(String path, Edge... edges) {
|
|
||||||
Edge edge = new Edge();
|
|
||||||
edge.path = path;
|
|
||||||
Segment2 s = new Segment2();
|
|
||||||
s.edges.addAll(Arrays.asList(edges));
|
|
||||||
edge.segment = s;
|
|
||||||
return edge;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Segment2 root = new Segment2();
|
|
||||||
|
|
||||||
root.edges.add(endpoint("/kikka"));
|
|
||||||
root.edges.add(endpoint("/kukka"));
|
|
||||||
root.edges.add(
|
|
||||||
context("/api",
|
|
||||||
endpoint("/ping"),
|
|
||||||
endpoint("pong")));
|
|
||||||
System.out.println(lookup(root, "/api/ping"));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Map<String, String> createHash(List<String> paths) {
|
|
||||||
Map<String, String> m = new HashMap<>();
|
|
||||||
for (String p : paths) {
|
|
||||||
m.put(p, p);
|
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<String> createArray(List<String> paths) {
|
|
||||||
return new ArrayList<>(paths);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Object hashLookup(Map m, String path) {
|
|
||||||
return m.get(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Object arrayLookup(ArrayList<String> paths, String path) {
|
|
||||||
Object data = null;
|
|
||||||
for (String p : paths) {
|
|
||||||
if (p.equals(path)) {
|
|
||||||
data = path;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,96 +0,0 @@
|
||||||
package reitit;
|
|
||||||
|
|
||||||
import clojure.lang.Keyword;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class Util {
|
|
||||||
|
|
||||||
public static Map<?, String> matchURI(String uri, List<?> patternTokens) {
|
|
||||||
final MatchResult result = matchURI(uri, 0, patternTokens, false);
|
|
||||||
return result == null ? null : result.getParams();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static MatchResult matchURI(String uri, int beginIndex, List<?> patternTokens) {
|
|
||||||
return matchURI(uri, beginIndex, patternTokens, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Match a URI against URI-pattern tokens and return match result on successful match, {@code null} otherwise.
|
|
||||||
* When argument {@code attemptPartialMatch} is {@code true}, both full and partial match are attempted without any
|
|
||||||
* performance penalty. When argument {@code attemptPartialMatch} is {@code false}, only a full match is attempted.
|
|
||||||
*
|
|
||||||
* @param uri the URI string to match
|
|
||||||
* @param beginIndex beginning index in the URI string to match
|
|
||||||
* @param patternTokens URI pattern tokens to match the URI against
|
|
||||||
* @param attemptPartialMatch whether attempt partial match when full match is not possible
|
|
||||||
* @return a match result on successful match, {@literal null} otherwise
|
|
||||||
*/
|
|
||||||
public static MatchResult matchURI(String uri, int beginIndex, List<?> patternTokens, boolean attemptPartialMatch) {
|
|
||||||
if (beginIndex == MatchResult.FULL_MATCH_INDEX) { // if already a full-match then no need to match further
|
|
||||||
return MatchResult.NO_MATCH;
|
|
||||||
}
|
|
||||||
final int tokenCount = patternTokens.size();
|
|
||||||
// if length==1, then token must be string (static URI path)
|
|
||||||
if (tokenCount == 1) {
|
|
||||||
final String staticPath = (String) patternTokens.get(0);
|
|
||||||
if (uri.startsWith(staticPath, beginIndex)) { // URI begins with the path, so at least partial match exists
|
|
||||||
if ((uri.length() - beginIndex) == staticPath.length()) { // if full match exists, then return as such
|
|
||||||
return MatchResult.FULL_MATCH_NO_PARAMS;
|
|
||||||
}
|
|
||||||
return attemptPartialMatch ? MatchResult.partialMatch(staticPath.length()) : MatchResult.NO_MATCH;
|
|
||||||
} else {
|
|
||||||
return MatchResult.NO_MATCH;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
final int uriLength = uri.length();
|
|
||||||
final Map<Object, String> pathParams = new HashMap<Object, String>(tokenCount);
|
|
||||||
int uriIndex = beginIndex;
|
|
||||||
OUTER:
|
|
||||||
for (final Object token : patternTokens) {
|
|
||||||
if (uriIndex >= uriLength) {
|
|
||||||
return attemptPartialMatch ? MatchResult.partialMatch(pathParams, uriIndex) : MatchResult.NO_MATCH;
|
|
||||||
}
|
|
||||||
if (token instanceof String) {
|
|
||||||
final String tokenStr = (String) token;
|
|
||||||
if (uri.startsWith(tokenStr, uriIndex)) {
|
|
||||||
uriIndex += tokenStr.length(); // now i==n if last string token
|
|
||||||
} else { // 'string token mismatch' implies no match
|
|
||||||
return MatchResult.NO_MATCH;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
final StringBuilder sb = new StringBuilder();
|
|
||||||
for (int j = uriIndex; j < uriLength; j++) { // capture param chars in one pass
|
|
||||||
final char ch = uri.charAt(j);
|
|
||||||
if (ch == '/') { // separator implies we got param value, now continue
|
|
||||||
pathParams.put(token, sb.toString());
|
|
||||||
uriIndex = j;
|
|
||||||
continue OUTER;
|
|
||||||
} else {
|
|
||||||
sb.append(ch);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 'separator not found' implies URI has ended
|
|
||||||
pathParams.put(token, sb.toString());
|
|
||||||
uriIndex = uriLength;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (uriIndex < uriLength) { // 'tokens finished but URI still in progress' implies partial or no match
|
|
||||||
return attemptPartialMatch ? MatchResult.partialMatch(pathParams, uriIndex) : MatchResult.NO_MATCH;
|
|
||||||
}
|
|
||||||
return MatchResult.fullMatch(pathParams);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
List<Object> list = new ArrayList<>();
|
|
||||||
list.add("/user/");
|
|
||||||
list.add(Keyword.intern("userId"));
|
|
||||||
list.add("/profile/");
|
|
||||||
list.add(Keyword.intern("type"));
|
|
||||||
list.add("/");
|
|
||||||
System.out.println(Util.matchURI("/user/1234/profile/compact/", list));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue