Package dev.polv.taleapi.permission
Class PermissionTree
java.lang.Object
dev.polv.taleapi.permission.PermissionTree
A Radix Tree (Trie) data structure for efficient permission lookups.
This hierarchical structure provides O(k) lookup time where k is the length of the permission string, regardless of how many permissions exist in the tree.
Performance Advantages
- Wildcards are instant: If traversal hits a
*node, it stops immediately - No string parsing: Permissions are naturally segmented by dots
- Organization: Related permissions are grouped (cmd.teleport, cmd.give)
Example Structure
root
├── cmd
│ ├── teleport [ALLOW]
│ ├── give [DENY]
│ └── * [ALLOW] // Wildcard: cmd.anything = ALLOW
└── plots
├── create [ALLOW]
└── limit [ALLOW, payload=5]
Thread Safety
This implementation uses ConcurrentHashMap for thread-safe reads. For bulk modifications, external synchronization is recommended.
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty permission tree.PermissionTree(Collection<PermissionNode> nodes) Creates a permission tree from a collection of nodes. -
Method Summary
Modifier and TypeMethodDescriptionvoidSets a simple permission (ALLOW with no payload).voidclear()Clears all permissions from this tree.voidSets a simple deny permission.static PermissionTreeflatten(PermissionTree... trees) Creates a flattened tree by merging multiple trees with priority.Gets all permission keys in this tree.Gets all permission nodes in this tree.booleanChecks if a permission is allowed (returns true for ALLOW state).booleanhas(String key, ContextSet context) Checks if a permission is allowed with context.booleanisEmpty()Checks if this tree is empty.voidmerge(PermissionTree other) Merges another tree into this one.Queries the tree for a permission result.query(String key, ContextSet context) Queries the tree for a permission result with context.booleanRemoves all permission nodes for a key.voidset(PermissionNode node) Sets a permission in the tree.intsize()Returns the number of permission nodes in this tree.
-
Field Details
-
WILDCARD
The wildcard character that matches any permission segment.- See Also:
-
-
Constructor Details
-
PermissionTree
public PermissionTree()Creates an empty permission tree. -
PermissionTree
Creates a permission tree from a collection of nodes.- Parameters:
nodes- the permission nodes to add
-
-
Method Details
-
set
Sets a permission in the tree.- Parameters:
node- the permission node to set
-
allow
Sets a simple permission (ALLOW with no payload).- Parameters:
key- the permission key
-
deny
Sets a simple deny permission.- Parameters:
key- the permission key
-
remove
Removes all permission nodes for a key.- Parameters:
key- the permission key to remove- Returns:
trueif any nodes were removed
-
query
Queries the tree for a permission result.This method traverses the tree, checking for wildcards at each level. If a wildcard is found, it returns immediately.
- Parameters:
key- the permission key to query- Returns:
- the permission result
-
query
Queries the tree for a permission result with context.This method traverses the tree segment by segment:
- At each level, check for a wildcard (*) node
- If wildcard exists and matches context, return its result
- Continue to the specific segment if no wildcard match
- At the final segment, return the node's result if it matches context
- Parameters:
key- the permission key to querycontext- the current context to match against- Returns:
- the permission result
-
has
Checks if a permission is allowed (returns true for ALLOW state).- Parameters:
key- the permission key- Returns:
trueif the permission is ALLOW
-
has
Checks if a permission is allowed with context.- Parameters:
key- the permission keycontext- the current context- Returns:
trueif the permission is ALLOW
-
getAllNodes
Gets all permission nodes in this tree.- Returns:
- an unmodifiable list of all nodes
-
getAllKeys
Gets all permission keys in this tree.- Returns:
- a list of all permission keys
-
size
public int size()Returns the number of permission nodes in this tree.- Returns:
- the total node count
-
isEmpty
public boolean isEmpty()Checks if this tree is empty.- Returns:
trueif no permissions are set
-
clear
public void clear()Clears all permissions from this tree. -
merge
Merges another tree into this one.Nodes from the other tree are added to this tree. Existing nodes with the same key are not replaced.
- Parameters:
other- the tree to merge from
-
flatten
Creates a flattened tree by merging multiple trees with priority.Trees are merged in order, with later trees having higher priority. This is used for permission resolution (Personal > Group > Default).
- Parameters:
trees- the trees to merge, in ascending priority order- Returns:
- a new flattened tree
-