Search in sources :

Example 1 with NodeTree

use of org.spongepowered.api.service.permission.NodeTree in project SpongeCommon by SpongePowered.

the class NodeTreeTest method testWithValue.

@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);
    final NodeTree oldTree = NodeTreeTest.FACTORY.ofBooleans(testPermissions, Tristate.UNDEFINED);
    Assertions.assertEquals(Tristate.FALSE, oldTree.get("generate.thunderstorm.explosive"));
    final NodeTree newTree = oldTree.withValue("generate.thunderstorm.explosive", Tristate.TRUE);
    Assertions.assertEquals(Tristate.FALSE, oldTree.get("generate.thunderstorm.explosive"));
    Assertions.assertEquals(Tristate.TRUE, newTree.get("generate.thunderstorm.explosive"));
}
Also used : HashMap(java.util.HashMap) NodeTree(org.spongepowered.api.service.permission.NodeTree) Test(org.junit.jupiter.api.Test)

Example 2 with NodeTree

use of org.spongepowered.api.service.permission.NodeTree in project SpongeCommon by SpongePowered.

the class NodeTreeTest method testAsMap.

@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);
    final NodeTree oldTree = NodeTreeTest.FACTORY.ofBooleans(testPermissions, Tristate.UNDEFINED);
    Assertions.assertEquals(testPermissions, oldTree.asMap());
}
Also used : HashMap(java.util.HashMap) NodeTree(org.spongepowered.api.service.permission.NodeTree) Test(org.junit.jupiter.api.Test)

Example 3 with NodeTree

use of org.spongepowered.api.service.permission.NodeTree in project SpongeCommon by SpongePowered.

the class NodeTreeTest method testWithAll.

@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);
    final NodeTree oldTree = NodeTreeTest.FACTORY.ofBooleans(testPermissions, Tristate.UNDEFINED);
    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);
    final NodeTree newTree = oldTree.withAllTristates(newPermissions);
    Assertions.assertEquals(Tristate.FALSE, oldTree.get("generate.sunset.red"));
    Assertions.assertEquals(Tristate.TRUE, newTree.get("generate.sunset.red"));
    Assertions.assertEquals(Tristate.FALSE, oldTree.get("generate.thunderstorm.explosive"));
    Assertions.assertEquals(Tristate.UNDEFINED, newTree.get("generate.thunderstorm.explosive"));
    Assertions.assertEquals(Tristate.UNDEFINED, oldTree.get("something.new"));
    Assertions.assertEquals(Tristate.FALSE, newTree.get("something.new"));
}
Also used : HashMap(java.util.HashMap) Tristate(org.spongepowered.api.util.Tristate) NodeTree(org.spongepowered.api.service.permission.NodeTree) Test(org.junit.jupiter.api.Test)

Example 4 with NodeTree

use of org.spongepowered.api.service.permission.NodeTree in project SpongeCommon by SpongePowered.

the class MemorySubjectData method setPermission.

@Override
public CompletableFuture<Boolean> setPermission(Set<Context> contexts, final String permission, final Tristate value) {
    Objects.requireNonNull(contexts, "contexts");
    Objects.requireNonNull(permission, "permission");
    Objects.requireNonNull(value, "value");
    contexts = ImmutableSet.copyOf(contexts);
    while (true) {
        final 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(Collections.singletonMap(permission, value.asBoolean()))) == null) {
                break;
            }
        } else {
            if (oldTree == null || this.permissions.replace(contexts, oldTree, oldTree.withValue(permission, value))) {
                break;
            }
        }
    }
    this.onUpdate();
    return CompletableFuture.completedFuture(true);
}
Also used : NodeTree(org.spongepowered.api.service.permission.NodeTree)

Example 5 with NodeTree

use of org.spongepowered.api.service.permission.NodeTree in project SpongeCommon by SpongePowered.

the class MemorySubjectData method setFallbackPermissionValue.

@Override
public CompletableFuture<Boolean> setFallbackPermissionValue(Set<Context> contexts, final Tristate fallback) {
    contexts = ImmutableSet.copyOf(Objects.requireNonNull(contexts, "contexts"));
    Objects.requireNonNull(fallback, "fallback");
    while (true) {
        final NodeTree oldTree = this.permissions.get(contexts);
        if (oldTree != null && oldTree.rootValue() == fallback) {
            return CompletableFuture.completedFuture(false);
        }
        if (oldTree == null && fallback != Tristate.UNDEFINED) {
            if (this.permissions.putIfAbsent(contexts, NodeTree.of(ImmutableMap.of(), fallback)) == null) {
                break;
            }
        } else {
            if (oldTree == null || this.permissions.replace(contexts, oldTree, oldTree.withRootValue(fallback))) {
                break;
            }
        }
    }
    this.onUpdate();
    return CompletableFuture.completedFuture(true);
}
Also used : NodeTree(org.spongepowered.api.service.permission.NodeTree)

Aggregations

NodeTree (org.spongepowered.api.service.permission.NodeTree)7 HashMap (java.util.HashMap)4 Test (org.junit.jupiter.api.Test)4 Context (org.spongepowered.api.service.context.Context)1 Tristate (org.spongepowered.api.util.Tristate)1