/** * Create a new node tree with the given values, and a default value of * {@link Tristate#UNDEFINED}. * * @param values The values to set * @return The new node tree */ public static NodeTree of(Map<String, Boolean> values) { return of(values, Tristate.UNDEFINED); }
/** * Gets the calculated node tree representation of the permissions for this * subject data instance. If no data is present for the given context, * returns null. * * @param contexts The contexts to get a node tree for * @return The node tree */ public NodeTree getNodeTree(Set<Context> contexts) { NodeTree perms = this.permissions.get(contexts); return perms == null ? NodeTree.of(Collections.emptyMap()) : perms; }
@Override public CompletableFuture<Boolean> setPermission(Set<Context> contexts, String permission, Tristate value) { contexts = ImmutableSet.copyOf(contexts); while (true) { NodeTree oldTree = this.permissions.get(contexts); if (oldTree != null && oldTree.get(permission) == value) { return CompletableFuture.completedFuture(false); } if (oldTree == null && value != Tristate.UNDEFINED) { if (this.permissions.putIfAbsent(contexts, NodeTree.of(ImmutableMap.of(permission, value.asBoolean()))) == null) { break; } } else { if (oldTree == null || this.permissions.replace(contexts, oldTree, oldTree.withValue(permission, value))) { break; } } } return CompletableFuture.completedFuture(true); }
@Test public void testAsMap() { final Map<String, Boolean> testPermissions = new HashMap<>(); testPermissions.put("generate.rainbow", true); testPermissions.put("generate.sunset", false); testPermissions.put("generate", true); testPermissions.put("generate.thunderstorm.explosive", false); NodeTree oldTree = NodeTree.of(testPermissions); assertEquals(testPermissions, oldTree.asMap()); }
@Test public void testWithValue() throws Exception { final Map<String, Boolean> testPermissions = new HashMap<>(); testPermissions.put("generate.rainbow", true); testPermissions.put("generate.sunset", false); testPermissions.put("generate", true); testPermissions.put("generate.thunderstorm.explosive", false); NodeTree oldTree = NodeTree.of(testPermissions); assertEquals(Tristate.FALSE, oldTree.get("generate.thunderstorm.explosive")); NodeTree newTree = oldTree.withValue("generate.thunderstorm.explosive", Tristate.TRUE); assertEquals(Tristate.FALSE, oldTree.get("generate.thunderstorm.explosive")); assertEquals(Tristate.TRUE, newTree.get("generate.thunderstorm.explosive")); }
@Test public void testWithAll() throws Exception { final Map<String, Boolean> testPermissions = new HashMap<>(); testPermissions.put("generate.rainbow", true); testPermissions.put("generate.sunset", false); testPermissions.put("generate", true); testPermissions.put("generate.thunderstorm.explosive", false); NodeTree oldTree = NodeTree.of(testPermissions); final Map<String, Tristate> newPermissions = new HashMap<>(); newPermissions.put("generate.sunset.red", Tristate.TRUE); newPermissions.put("generate.thunderstorm.explosive", Tristate.UNDEFINED); newPermissions.put("something.new", Tristate.FALSE); NodeTree newTree = oldTree.withAll(newPermissions); assertEquals(Tristate.FALSE, oldTree.get("generate.sunset.red")); assertEquals(Tristate.TRUE, newTree.get("generate.sunset.red")); assertEquals(Tristate.FALSE, oldTree.get("generate.thunderstorm.explosive")); assertEquals(Tristate.UNDEFINED, newTree.get("generate.thunderstorm.explosive")); assertEquals(Tristate.UNDEFINED, oldTree.get("something.new")); assertEquals(Tristate.FALSE, newTree.get("something.new")); }
@Test public void testCreateFromValues() throws Exception { final Map<String, Boolean> testPermissions = new HashMap<>(); testPermissions.put("generate.rainbow", true); testPermissions.put("generate.sunset", false); testPermissions.put("generate", true); testPermissions.put("generate.thunderstorm.explosive", false); NodeTree nodes = NodeTree.of(testPermissions, Tristate.UNDEFINED); assertEquals(Tristate.TRUE, nodes.get("generate.rainbow")); assertEquals(Tristate.TRUE, nodes.get("generate.rainbow.double")); assertEquals(Tristate.FALSE, nodes.get("generate.sunset")); assertEquals(Tristate.FALSE, nodes.get("generate.sunset.east")); assertEquals(Tristate.TRUE, nodes.get("generate.thunderstorm")); assertEquals(Tristate.FALSE, nodes.get("generate.thunderstorm.explosive")); assertEquals(Tristate.UNDEFINED, nodes.get("random.perm")); } }
/** * Create a new node tree with the given values, and a default value of UNDEFINED. * * @param values The values to set * @return The new node tree */ public static NodeTree of(Map<String, Boolean> values) { return of(values, Tristate.UNDEFINED); }
/** * Get the calculated node tree representation of the permissions for this subject data instance. * If no data is present for the given context, returns null. * * @param contexts The contexts to get a node tree for * @return The node tree */ public NodeTree getNodeTree(Set<Context> contexts) { NodeTree perms = this.permissions.get(contexts); return perms == null ? NodeTree.of(Collections.emptyMap()) : perms; }
@Override public boolean setPermission(Set<Context> contexts, String permission, Tristate value) { contexts = ImmutableSet.copyOf(contexts); while (true) { NodeTree oldTree = this.permissions.get(contexts); if (oldTree != null && oldTree.get(permission) == value) { return false; } if (oldTree == null && value != Tristate.UNDEFINED) { if (this.permissions.putIfAbsent(contexts, NodeTree.of(ImmutableMap.of(permission, value.asBoolean()))) == null) { break; } } else { if (oldTree == null || this.permissions.replace(contexts, oldTree, oldTree.withValue(permission, value))) { break; } } } return true; }