diff --git a/modules/reitit-core/java-src/reitit/MatchResult.java b/modules/reitit-core/java-src/reitit/MatchResult.java new file mode 100644 index 00000000..56c2e31f --- /dev/null +++ b/modules/reitit-core/java-src/reitit/MatchResult.java @@ -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 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 params; + + /** + * End index in the URI when match stopped. -1 implies match fully ended. + */ + private final int endIndex; // excluding + + protected MatchResult(Map params, int endIndex) { + this.params = params; + this.endIndex = endIndex; + } + + // ----- factory methods ----- + + public static MatchResult partialMatch(Map 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 params) { + return new MatchResult(params, FULL_MATCH_INDEX); + } + + // ----- utility methods ----- + + public Map 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(); + } +} diff --git a/modules/reitit-core/java-src/reitit/Segment2.java b/modules/reitit-core/java-src/reitit/Segment2.java new file mode 100644 index 00000000..b968b547 --- /dev/null +++ b/modules/reitit-core/java-src/reitit/Segment2.java @@ -0,0 +1,97 @@ +package reitit; + +import java.util.*; + +public class Segment2 { + + private List 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 createHash(List paths) { + Map m = new HashMap<>(); + for (String p : paths) { + m.put(p, p); + } + return m; + } + + public static List createArray(List paths) { + return new ArrayList<>(paths); + } + + public static Object hashLookup(Map m, String path) { + return m.get(path); + } + + public static Object arrayLookup(ArrayList paths, String path) { + Object data = null; + for (String p : paths) { + if (p.equals(path)) { + data = path; + break; + } + } + return data; + } +}