Skip to content

Commit ef429fd

Browse files
committed
Improve performance of Permission or/and logic
Signed-off-by: Irmo van den Berge <[email protected]>
1 parent b1e55c5 commit ef429fd

File tree

4 files changed

+91
-16
lines changed

4 files changed

+91
-16
lines changed

cloud-core/src/main/java/cloud/commandframework/CommandTree.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import io.leangen.geantyref.GenericTypeReflector;
4545
import io.leangen.geantyref.TypeToken;
4646
import java.util.ArrayList;
47-
import java.util.Arrays;
4847
import java.util.Collection;
4948
import java.util.Collections;
5049
import java.util.Comparator;
@@ -878,7 +877,7 @@ public void verifyAndRegister() {
878877

879878
CommandPermission permission;
880879
if (existingPermission != null) {
881-
permission = OrPermission.of(Arrays.asList(commandPermission, existingPermission));
880+
permission = commandPermission.or(existingPermission);
882881
} else {
883882
permission = commandPermission;
884883
}
@@ -893,7 +892,7 @@ public void verifyAndRegister() {
893892
.getSetting(CommandManager.ManagerSettings.ENFORCE_INTERMEDIARY_PERMISSIONS)) {
894893
permission = command.getCommandPermission();
895894
} else {
896-
permission = OrPermission.of(Arrays.asList(permission, command.getCommandPermission()));
895+
permission = permission.or(command.getCommandPermission());
897896
}
898897
}
899898

cloud-core/src/main/java/cloud/commandframework/permission/AndPermission.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,32 @@ public final class AndPermission implements CommandPermission {
6767
return this.permissions;
6868
}
6969

70+
@Override
71+
public @NonNull CommandPermission and(final @NonNull CommandPermission other) {
72+
if (this.permissions.contains(other)) {
73+
return this;
74+
} else {
75+
final Set<CommandPermission> objects = new HashSet<>(this.permissions);
76+
addToSet(objects, other);
77+
return new AndPermission(objects);
78+
}
79+
}
80+
81+
@Override
82+
public @NonNull CommandPermission and(final @NonNull CommandPermission @NonNull... other) {
83+
if (other.length == 0) {
84+
return this;
85+
} else if (other.length == 1) {
86+
return this.and(other[0]);
87+
} else {
88+
final Set<CommandPermission> objects = new HashSet<>(this.permissions);
89+
for (final CommandPermission permission : other) {
90+
addToSet(objects, permission);
91+
}
92+
return new AndPermission(objects);
93+
}
94+
}
95+
7096
@Override
7197
public String toString() {
7298
final StringBuilder stringBuilder = new StringBuilder();
@@ -97,4 +123,15 @@ public boolean equals(final Object o) {
97123
public int hashCode() {
98124
return Objects.hash(this.getPermissions());
99125
}
126+
127+
private static void addToSet(
128+
@NonNull final Set<CommandPermission> objects,
129+
@NonNull final CommandPermission permission
130+
) {
131+
if (permission instanceof AndPermission) {
132+
objects.addAll(permission.getPermissions());
133+
} else {
134+
objects.add(permission);
135+
}
136+
}
100137
}

cloud-core/src/main/java/cloud/commandframework/permission/CommandPermission.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ public interface CommandPermission {
6363
@API(status = API.Status.STABLE, since = "1.4.0")
6464
default @NonNull CommandPermission or(final @NonNull CommandPermission other) {
6565
requireNonNull(other, "other");
66-
final Set<CommandPermission> permission = new HashSet<>(2);
67-
permission.add(this);
68-
permission.add(other);
69-
return OrPermission.of(permission);
66+
67+
// Performance optimization
68+
if (other instanceof OrPermission) {
69+
return other.or(this);
70+
}
71+
72+
return OrPermission.of(Arrays.asList(this, other));
7073
}
7174

7275
/**
@@ -95,10 +98,13 @@ public interface CommandPermission {
9598
@API(status = API.Status.STABLE, since = "1.4.0")
9699
default @NonNull CommandPermission and(final @NonNull CommandPermission other) {
97100
requireNonNull(other, "other");
98-
final Set<CommandPermission> permission = new HashSet<>(2);
99-
permission.add(this);
100-
permission.add(other);
101-
return AndPermission.of(permission);
101+
102+
// Performance optimization
103+
if (other instanceof AndPermission) {
104+
return other.and(this);
105+
}
106+
107+
return AndPermission.of(Arrays.asList(this, other));
102108
}
103109

104110
/**

cloud-core/src/main/java/cloud/commandframework/permission/OrPermission.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ public final class OrPermission implements CommandPermission {
5353
public static @NonNull CommandPermission of(final @NonNull Collection<CommandPermission> permissions) {
5454
final Set<CommandPermission> objects = new HashSet<>();
5555
for (final CommandPermission permission : permissions) {
56-
if (permission instanceof OrPermission) {
57-
objects.addAll(permission.getPermissions());
58-
} else {
59-
objects.add(permission);
60-
}
56+
addToSet(objects, permission);
6157
}
6258
return new OrPermission(objects);
6359
}
@@ -67,6 +63,32 @@ public final class OrPermission implements CommandPermission {
6763
return this.permissions;
6864
}
6965

66+
@Override
67+
public @NonNull CommandPermission or(final @NonNull CommandPermission other) {
68+
if (this.permissions.contains(other)) {
69+
return this;
70+
} else {
71+
final Set<CommandPermission> objects = new HashSet<>(this.permissions);
72+
addToSet(objects, other);
73+
return new OrPermission(objects);
74+
}
75+
}
76+
77+
@Override
78+
public @NonNull CommandPermission or(final @NonNull CommandPermission @NonNull... other) {
79+
if (other.length == 0) {
80+
return this;
81+
} else if (other.length == 1) {
82+
return this.or(other[0]);
83+
} else {
84+
final Set<CommandPermission> objects = new HashSet<>(this.permissions);
85+
for (final CommandPermission permission : other) {
86+
addToSet(objects, permission);
87+
}
88+
return new OrPermission(objects);
89+
}
90+
}
91+
7092
@Override
7193
public String toString() {
7294
final StringBuilder stringBuilder = new StringBuilder();
@@ -97,4 +119,15 @@ public boolean equals(final Object o) {
97119
public int hashCode() {
98120
return Objects.hash(this.getPermissions());
99121
}
122+
123+
private static void addToSet(
124+
@NonNull final Set<CommandPermission> objects,
125+
@NonNull final CommandPermission permission
126+
) {
127+
if (permission instanceof OrPermission) {
128+
objects.addAll(permission.getPermissions());
129+
} else {
130+
objects.add(permission);
131+
}
132+
}
100133
}

0 commit comments

Comments
 (0)