shz.core.structure.ApiTree Maven / Gradle / Ivy
package shz.core.structure;
import shz.core.io.FileHelp;
import shz.core.lock.ReadWriteLockHolder;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
public final class ApiTree extends ReadWriteLockHolder {
private T data;
private ApiTree child;
private Map> children;
private ApiTree() {
}
public static ApiTree of() {
ApiTree tree = new ApiTree<>();
tree.children = new HashMap<>();
return tree;
}
public void put(String method, String path, T data) {
writeLock.lock();
try {
ApiTree tree = children.computeIfAbsent(method, k -> new ApiTree<>());
path = FileHelp.formatPath(path);
if ("/".equals(path)) {
tree.data = data;
return;
}
if (path.charAt(0) == '/') path = path.substring(1);
if (path.charAt(path.length() - 1) == '/') path = path.substring(0, path.length() - 1);
for (String seg : path.split("/")) {
if (seg.length() == 2 && seg.charAt(0) == '{' && seg.charAt(1) == '}') {
if (tree.child == null) tree.child = new ApiTree<>();
tree = tree.child;
} else {
if (tree.children == null) tree.children = new HashMap<>();
tree = tree.children.computeIfAbsent(seg, k -> new ApiTree<>());
}
}
tree.data = data;
} finally {
writeLock.unlock();
}
}
public T get(String method, String path) {
readLock.lock();
try {
ApiTree tree = children.get(method);
if (tree == null) return null;
if ("/".equals(path)) return tree.data;
for (String seg : path.split("/")) {
if (tree.child != null) tree = tree.child;
else if (tree.children != null) {
tree = tree.children.get(seg);
if (tree == null) return null;
} else return null;
}
return tree.data;
} finally {
readLock.unlock();
}
}
public void remove(String method, String path) {
writeLock.lock();
try {
ApiTree pTree = children.get(method);
if (pTree == null) return;
if ("/".equals(path)) {
pTree.data = null;
if (pTree.child == null && pTree.children == null) children.remove(method);
return;
}
ApiTree tree = pTree;
String markSeg = null;
for (String seg : path.split("/")) {
if (tree.child != null) {
pTree = tree;
tree = tree.child;
markSeg = null;
} else if (tree.children != null) {
pTree = tree;
tree = tree.children.get(seg);
if (tree == null) return;
markSeg = seg;
} else return;
}
tree.data = null;
if (tree.child == null && tree.children == null) {
if (markSeg == null) pTree.child = null;
else {
pTree.children.remove(markSeg);
if (pTree.children.isEmpty()) pTree.children = null;
}
}
} finally {
writeLock.unlock();
}
}
public T filterOne(Predicate filter) {
readLock.lock();
try {
if (data != null && filter.test(data)) return data;
if (child != null) {
T t = child.filterOne(filter);
if (t != null) return t;
}
if (children == null) return null;
for (ApiTree tree : children.values()) {
T t = tree.filterOne(filter);
if (t != null) return t;
}
return null;
} finally {
readLock.unlock();
}
}
public List filter(Predicate filter) {
readLock.lock();
try {
List result = new LinkedList<>();
if (data != null && filter.test(data)) result.add(data);
if (child != null) child.filter0(filter, result);
if (children != null) for (ApiTree tree : children.values()) tree.filter0(filter, result);
return result;
} finally {
readLock.unlock();
}
}
private void filter0(Predicate filter, List result) {
if (data != null && filter.test(data)) result.add(data);
if (child != null) child.filter0(filter, result);
if (children != null) for (ApiTree tree : children.values()) tree.filter0(filter, result);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy