Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/renatoathaydes/pathtrie
A Trie implementation specifically designed for paths
https://github.com/renatoathaydes/pathtrie
data-structures java routing trie
Last synced: 9 days ago
JSON representation
A Trie implementation specifically designed for paths
- Host: GitHub
- URL: https://github.com/renatoathaydes/pathtrie
- Owner: renatoathaydes
- Created: 2018-05-24T14:42:11.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-02T18:24:18.000Z (about 6 years ago)
- Last Synced: 2024-10-30T04:55:26.601Z (about 2 months ago)
- Topics: data-structures, java, routing, trie
- Language: Java
- Size: 84 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
Awesome Lists containing this project
README
# PathTrie
A [Trie](https://en.wikipedia.org/wiki/Trie) implementation designed specifically for using paths as keys.
The main use of this data structure is to locate information stored under hierarchical paths, such as files and URLs,
very fast.## Simple Usage
The main type of this library is `com.athaydes.pathtrie.PathTrie`.
To create a new `PathTrie`, a builder is used:
```java
var trie = PathTrie.newBuilder()
.put("/hello/world", 1)
.put("/other/path", 2)
.build();
```Retrieving a value from a `PathTrie`:
```java
Optional value = trie.get("/hello/world");assertTrue(value.isPresent());
assertEquals(Integer.valueOf(1), value.get());
```You can also retrieve a sub-trie:
```java
Optional> helloTrie = trie.getChild("/hello");assertTrue(helloTrie.isPresent());
assertEquals(Integer.valueOf(1), helloTrie.get().get("world").get());
```## Parameterized Usage
`PathTrie` also supports parameterized mappings:
```java
final Map users = new LinkedHashMap<>();
users.put("123", "Joe");
users.put("456", "Mary");PathTrie parameterizedTrie = PathTrie.newBuilder()
.putFun("/users", () -> users)
.putFun("/users/:id", id -> users.get(id))
.build();assertEquals(users, parameterizedTrie.get("/users").orElse(emptyMap()));
assertEquals("Joe", parameterizedTrie.get("/users/123").orElse("NOT FOUND"));
assertEquals("Mary", parameterizedTrie.get("/users/456").orElse("NOT FOUND"));assertFalse(parameterizedTrie.get("/others").isPresent());
assertFalse(parameterizedTrie.get("/users/789").isPresent());
```Up to 4 parameters can be used (if more is needed, you can implement `com.athaydes.pathtrie.functions.Fun` providing
an adapter that can take more parameters):```java
PathTrie parameterizedTrie = PathTrie.newBuilder()
.putFun("/:a/:b/:c/:d", (a, b, c, d) -> "a=" + a + ",b=" + b + ",c=" + c + ",d=" + d)
.build();assertEquals("a=A,b=B,c=C,d=D", parameterizedTrie.get("/A/B/C/D").orElse("NOT FOUND"));
```Notice that the lambda arguments are positional (i.e. their names don't matter, only their positions relative to the
path).