package foundation.route;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
public class FilterNode<T> {
|
|
private Map<String, FilterNode<T>> nodes;
|
private String path;
|
private T item;
|
private boolean matchAll;
|
|
|
public FilterNode(String path, T item) {
|
nodes = new HashMap<String, FilterNode<T>>();
|
this.path = path;
|
}
|
|
public T match(String[] segments, int idx) {
|
if (matchAll) {
|
return item;
|
}
|
|
if (idx == -1) {
|
return item;
|
}
|
|
String segment = segments[idx];
|
FilterNode<T> node = nodes.get(segment);
|
|
if (node == null) {
|
return null;
|
}
|
|
return node.match(segments, idx - 1);
|
}
|
|
public void add(String[] segments, int idx, T item) {
|
if (idx == -1) {
|
return;
|
}
|
|
String segment = segments[idx];
|
|
if ("*".equals(segment)) {
|
matchAll = true;
|
return;
|
}
|
|
FilterNode<T> node = nodes.get(segment);
|
|
if (node == null) {
|
node = new FilterNode<T>(segment, item);
|
nodes.put(segment, node);
|
}
|
|
node.add(segments, idx - 1, item);
|
}
|
|
public T getItem() {
|
return item;
|
}
|
|
@Override
|
public String toString() {
|
return path;
|
}
|
|
}
|