Skip to content

Commit

Permalink
Improve performance of Permission or/and logic
Browse files Browse the repository at this point in the history
Signed-off-by: Irmo van den Berge <[email protected]>
  • Loading branch information
bergerkiller committed Oct 1, 2023
1 parent ea160ac commit 5cf3d50
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import cloud.commandframework.types.tuples.Pair;
import io.leangen.geantyref.TypeToken;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -882,7 +881,7 @@ public void verifyAndRegister() {

CommandPermission permission;
if (existingPermission != null) {
permission = OrPermission.of(Arrays.asList(commandPermission, existingPermission));
permission = commandPermission.or(existingPermission);
} else {
permission = commandPermission;
}
Expand All @@ -897,7 +896,7 @@ public void verifyAndRegister() {
.getSetting(CommandManager.ManagerSettings.ENFORCE_INTERMEDIARY_PERMISSIONS)) {
permission = command.getCommandPermission();
} else {
permission = OrPermission.of(Arrays.asList(permission, command.getCommandPermission()));
permission = permission.or(command.getCommandPermission());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,32 @@ public final class AndPermission implements CommandPermission {
return this.permissions;
}

@Override
public @NonNull CommandPermission and(final @NonNull CommandPermission other) {
if (this.permissions.contains(other)) {
return this;
} else {
final Set<CommandPermission> objects = new HashSet<>(this.permissions);
addToSet(objects, other);
return new AndPermission(objects);
}
}

@Override
public @NonNull CommandPermission and(final @NonNull CommandPermission @NonNull... other) {
if (other.length == 0) {
return this;
} else if (other.length == 1) {
return this.and(other[0]);
} else {
final Set<CommandPermission> objects = new HashSet<>(this.permissions);
for (final CommandPermission permission : other) {
addToSet(objects, permission);
}
return new AndPermission(objects);
}
}

@Override
public String toString() {
final StringBuilder stringBuilder = new StringBuilder();
Expand Down Expand Up @@ -97,4 +123,15 @@ public boolean equals(final Object o) {
public int hashCode() {
return Objects.hash(this.getPermissions());
}

private static void addToSet(
@NonNull final Set<CommandPermission> objects,
@NonNull final CommandPermission permission
) {
if (permission instanceof AndPermission) {
objects.addAll(permission.getPermissions());
} else {
objects.add(permission);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ public interface CommandPermission {
@API(status = API.Status.STABLE, since = "1.4.0")
default @NonNull CommandPermission or(final @NonNull CommandPermission other) {
requireNonNull(other, "other");
final Set<CommandPermission> permission = new HashSet<>(2);
permission.add(this);
permission.add(other);
return OrPermission.of(permission);

// Performance optimization
if (other instanceof OrPermission) {
return other.or(this);
}

return OrPermission.of(Arrays.asList(this, other));
}

/**
Expand Down Expand Up @@ -95,10 +98,13 @@ public interface CommandPermission {
@API(status = API.Status.STABLE, since = "1.4.0")
default @NonNull CommandPermission and(final @NonNull CommandPermission other) {
requireNonNull(other, "other");
final Set<CommandPermission> permission = new HashSet<>(2);
permission.add(this);
permission.add(other);
return AndPermission.of(permission);

// Performance optimization
if (other instanceof AndPermission) {
return other.and(this);
}

return AndPermission.of(Arrays.asList(this, other));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ public final class OrPermission implements CommandPermission {
public static @NonNull CommandPermission of(final @NonNull Collection<CommandPermission> permissions) {
final Set<CommandPermission> objects = new HashSet<>();
for (final CommandPermission permission : permissions) {
if (permission instanceof OrPermission) {
objects.addAll(permission.getPermissions());
} else {
objects.add(permission);
}
addToSet(objects, permission);
}
return new OrPermission(objects);
}
Expand All @@ -67,6 +63,32 @@ public final class OrPermission implements CommandPermission {
return this.permissions;
}

@Override
public @NonNull CommandPermission or(final @NonNull CommandPermission other) {
if (this.permissions.contains(other)) {
return this;
} else {
final Set<CommandPermission> objects = new HashSet<>(this.permissions);
addToSet(objects, other);
return new OrPermission(objects);
}
}

@Override
public @NonNull CommandPermission or(final @NonNull CommandPermission @NonNull... other) {
if (other.length == 0) {
return this;
} else if (other.length == 1) {
return this.or(other[0]);
} else {
final Set<CommandPermission> objects = new HashSet<>(this.permissions);
for (final CommandPermission permission : other) {
addToSet(objects, permission);
}
return new OrPermission(objects);
}
}

@Override
public String toString() {
final StringBuilder stringBuilder = new StringBuilder();
Expand Down Expand Up @@ -97,4 +119,15 @@ public boolean equals(final Object o) {
public int hashCode() {
return Objects.hash(this.getPermissions());
}

private static void addToSet(
@NonNull final Set<CommandPermission> objects,
@NonNull final CommandPermission permission
) {
if (permission instanceof OrPermission) {
objects.addAll(permission.getPermissions());
} else {
objects.add(permission);
}
}
}

0 comments on commit 5cf3d50

Please sign in to comment.