From 54aded44421ef7d78784b9e17de473b3d6ab3f98 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sun, 13 Jan 2019 16:44:59 +0200 Subject: [PATCH] Dead code --- .../java-src/reitit/MatchResult.java | 77 --------------- .../reitit-core/java-src/reitit/Segment2.java | 97 ------------------- .../reitit/{Trie.java => SegmentTrie.java} | 0 modules/reitit-core/java-src/reitit/Util.java | 96 ------------------ 4 files changed, 270 deletions(-) delete mode 100644 modules/reitit-core/java-src/reitit/MatchResult.java delete mode 100644 modules/reitit-core/java-src/reitit/Segment2.java rename modules/reitit-core/java-src/reitit/{Trie.java => SegmentTrie.java} (100%) delete mode 100644 modules/reitit-core/java-src/reitit/Util.java diff --git a/modules/reitit-core/java-src/reitit/MatchResult.java b/modules/reitit-core/java-src/reitit/MatchResult.java deleted file mode 100644 index 56c2e31f..00000000 --- a/modules/reitit-core/java-src/reitit/MatchResult.java +++ /dev/null @@ -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 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 deleted file mode 100644 index b968b547..00000000 --- a/modules/reitit-core/java-src/reitit/Segment2.java +++ /dev/null @@ -1,97 +0,0 @@ -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; - } -} diff --git a/modules/reitit-core/java-src/reitit/Trie.java b/modules/reitit-core/java-src/reitit/SegmentTrie.java similarity index 100% rename from modules/reitit-core/java-src/reitit/Trie.java rename to modules/reitit-core/java-src/reitit/SegmentTrie.java diff --git a/modules/reitit-core/java-src/reitit/Util.java b/modules/reitit-core/java-src/reitit/Util.java deleted file mode 100644 index 767731c4..00000000 --- a/modules/reitit-core/java-src/reitit/Util.java +++ /dev/null @@ -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 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 pathParams = new HashMap(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 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)); - } -}