From 22006617d0d171e274552d0191a55b546c7e80f6 Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 12 Jan 2018 23:51:59 +0000 Subject: [PATCH] misc refactor for PermissionHolder & DummySender --- .../luckperms/common/backup/Importer.java | 1 + .../sender}/DummySender.java | 3 +- .../common/model/PermissionHolder.java | 8 ++- .../lucko/luckperms/common/utils/Cycle.java | 56 ++++++++++++------- .../luckperms/sponge/LPSpongePlugin.java | 2 +- 5 files changed, 43 insertions(+), 27 deletions(-) rename common/src/main/java/me/lucko/luckperms/common/{backup => commands/sender}/DummySender.java (96%) diff --git a/common/src/main/java/me/lucko/luckperms/common/backup/Importer.java b/common/src/main/java/me/lucko/luckperms/common/backup/Importer.java index d6744d45..4b2af9de 100644 --- a/common/src/main/java/me/lucko/luckperms/common/backup/Importer.java +++ b/common/src/main/java/me/lucko/luckperms/common/backup/Importer.java @@ -30,6 +30,7 @@ import com.google.common.collect.ImmutableSet; import me.lucko.luckperms.common.commands.CommandManager; import me.lucko.luckperms.common.commands.CommandResult; +import me.lucko.luckperms.common.commands.sender.DummySender; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.commands.utils.CommandUtils; import me.lucko.luckperms.common.locale.Message; diff --git a/common/src/main/java/me/lucko/luckperms/common/backup/DummySender.java b/common/src/main/java/me/lucko/luckperms/common/commands/sender/DummySender.java similarity index 96% rename from common/src/main/java/me/lucko/luckperms/common/backup/DummySender.java rename to common/src/main/java/me/lucko/luckperms/common/commands/sender/DummySender.java index 2af7f689..0d384c3c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/backup/DummySender.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/sender/DummySender.java @@ -23,11 +23,10 @@ * SOFTWARE. */ -package me.lucko.luckperms.common.backup; +package me.lucko.luckperms.common.commands.sender; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.commands.CommandManager; -import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.utils.TextUtils; diff --git a/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java b/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java index d5ea28d5..054f7eee 100644 --- a/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java +++ b/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java @@ -747,9 +747,11 @@ public abstract class PermissionHolder { if (perms.putIfAbsent(perm, node.getValuePrimitive()) == null) { if (applyShorthand) { - List sh = node.resolveShorthand(); - if (!sh.isEmpty()) { - sh.stream().map(s -> lowerCase ? s.toLowerCase() : s).forEach(s -> perms.putIfAbsent(s, node.getValuePrimitive())); + List shorthand = node.resolveShorthand(); + if (!shorthand.isEmpty()) { + for (String s : shorthand) { + perms.putIfAbsent(lowerCase ? s.toLowerCase() : s, node.getValuePrimitive()); + } } } } diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/Cycle.java b/common/src/main/java/me/lucko/luckperms/common/utils/Cycle.java index 5974f093..d569b3d4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/utils/Cycle.java +++ b/common/src/main/java/me/lucko/luckperms/common/utils/Cycle.java @@ -28,6 +28,7 @@ package me.lucko.luckperms.common.utils; import com.google.common.collect.ImmutableList; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; /** * A cycle of elements, backed by a list. All operations are thread safe. @@ -35,45 +36,58 @@ import java.util.List; * @param the element type */ public class Cycle { - protected final List objects; - protected int index = 0; + + /** + * The list that backs this instance + */ + private final List objects; + + /** + * The number of elements in the cycle + */ + private final int size; + + /** + * The current position of the cursor + */ + private AtomicInteger cursor = new AtomicInteger(0); public Cycle(List objects) { if (objects == null || objects.isEmpty()) { throw new IllegalArgumentException("List of objects cannot be null/empty."); } this.objects = ImmutableList.copyOf(objects); + this.size = this.objects.size(); } - public int getIndex() { - return this.index; + public int cursor() { + return this.cursor.get(); } public E current() { - synchronized (this) { - return this.objects.get(this.index); - } + return this.objects.get(cursor()); } public E next() { - synchronized (this) { - this.index++; - this.index = this.index > this.objects.size() - 1 ? 0 : this.index; - - return this.objects.get(this.index); - } + return this.objects.get(this.cursor.updateAndGet(i -> { + int n = i + 1; + if (n >= this.size) { + return 0; + } + return n; + })); } - public E back() { - synchronized (this) { - this.index--; - this.index = this.index == -1 ? this.objects.size() - 1 : this.index; - - return this.objects.get(this.index); - } + public E previous() { + return this.objects.get(this.cursor.updateAndGet(i -> { + if (i == 0) { + return this.size - 1; + } + return i - 1; + })); } public List getBacking() { return this.objects; } -} +} \ No newline at end of file diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java index 80b87be7..a387820b 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -33,13 +33,13 @@ import me.lucko.luckperms.api.platform.PlatformType; import me.lucko.luckperms.common.actionlog.LogDispatcher; import me.lucko.luckperms.common.api.ApiRegistrationUtil; import me.lucko.luckperms.common.api.LuckPermsApiProvider; -import me.lucko.luckperms.common.backup.DummySender; import me.lucko.luckperms.common.buffers.BufferedRequest; import me.lucko.luckperms.common.buffers.UpdateTaskBuffer; import me.lucko.luckperms.common.caching.handlers.CachedStateManager; import me.lucko.luckperms.common.calculators.CalculatorFactory; import me.lucko.luckperms.common.commands.CommandPermission; import me.lucko.luckperms.common.commands.abstraction.Command; +import me.lucko.luckperms.common.commands.sender.DummySender; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.config.AbstractConfiguration; import me.lucko.luckperms.common.config.ConfigKeys;