mirror of
https://github.com/metosin/reitit.git
synced 2026-02-03 11:00:34 +00:00
.
This commit is contained in:
parent
ae2337621f
commit
e1925c8462
2 changed files with 174 additions and 0 deletions
77
modules/reitit-core/java-src/reitit/MatchResult.java
Normal file
77
modules/reitit-core/java-src/reitit/MatchResult.java
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
97
modules/reitit-core/java-src/reitit/Segment2.java
Normal file
97
modules/reitit-core/java-src/reitit/Segment2.java
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue