/** * Return a modified new node tree with the specified values set. * * @param values The values to set * @return The new node tree */ public NodeTree withAll(Map<String, Tristate> values) { NodeTree ret = this; for (Map.Entry<String, Tristate> ent : values.entrySet()) { ret = ret.withValue(ent.getKey(), ent.getValue()); } return ret; }
@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 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")); }
/** * Return a modified new node tree with the specified values set. * * @param values The values to set * @return The new node tree */ public NodeTree withAll(Map<String, Tristate> values) { NodeTree ret = this; for (Map.Entry<String, Tristate> ent : values.entrySet()) { ret = ret.withValue(ent.getKey(), ent.getValue()); } return ret; }
@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; }