From 7bf666b062db0a1b0276f11e49ef42fd285f1a09 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 30 Dec 2017 16:11:41 +0000 Subject: [PATCH] fix abuse of static in SubjectReferenceFactory --- .../service/model/LPPermissionService.java | 2 + .../model/SubjectReferenceFactory.java | 58 ++++++++++++++----- .../sponge/service/LuckPermsService.java | 3 + 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionService.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionService.java index 59f7a24fb..8ec51f3d1 100644 --- a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionService.java +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionService.java @@ -48,6 +48,8 @@ public interface LPPermissionService { LuckPermsPlugin getPlugin(); + SubjectReferenceFactory getReferenceFactory(); + PermissionService sponge(); LPSubjectCollection getUserSubjects(); diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReferenceFactory.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReferenceFactory.java index 644863245..10846b5ec 100644 --- a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReferenceFactory.java +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReferenceFactory.java @@ -27,7 +27,7 @@ package me.lucko.luckperms.sponge.service.model; import lombok.AllArgsConstructor; import lombok.NonNull; -import lombok.experimental.UtilityClass; +import lombok.RequiredArgsConstructor; import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.LoadingCache; @@ -41,9 +41,37 @@ import java.util.concurrent.TimeUnit; /** * Caches the creation of {@link SubjectReference}s. */ -@UtilityClass +@RequiredArgsConstructor public final class SubjectReferenceFactory { + // static util access + + @Deprecated + public static SubjectReference deserialize(@NonNull LPPermissionService service, String serialisedReference) { + return service.getReferenceFactory().deserialize(serialisedReference); + } + + public static SubjectReference obtain(@NonNull LPPermissionService service, LPSubject subject) { + return service.getReferenceFactory().obtain(subject); + } + + public static SubjectReference obtain(@NonNull LPPermissionService service, Subject subject) { + return service.getReferenceFactory().obtain(subject); + } + + public static SubjectReference obtain(@NonNull LPPermissionService service, org.spongepowered.api.service.permission.SubjectReference reference) { + return service.getReferenceFactory().obtain(reference); + } + + public static SubjectReference obtain(@NonNull LPPermissionService service, String collectionIdentifier, String subjectIdentifier) { + return service.getReferenceFactory().obtain(collectionIdentifier, subjectIdentifier); + } + + /** + * The permission service to obtain real subject instances from + */ + private final LPPermissionService service; + /** * Cache based factory for SubjectReferences. * @@ -53,40 +81,40 @@ public final class SubjectReferenceFactory { * * It's perfectly ok if two instances of the same SubjectReference exist. (hence the 1 hour expiry) */ - private static final LoadingCache REFERENCE_CACHE = Caffeine.newBuilder() + private final LoadingCache referenceCache = Caffeine.newBuilder() .expireAfterAccess(1, TimeUnit.HOURS) - .build(a -> new SubjectReference(a.permissionService, a.collectionId, a.id)); + .build(a -> new SubjectReference(service, a.collectionId, a.id)); @Deprecated - public static SubjectReference deserialize(@NonNull LPPermissionService service, @NonNull String serialisedReference) { + public SubjectReference deserialize(@NonNull String serialisedReference) { List parts = Splitter.on('/').limit(2).splitToList(serialisedReference); - return obtain(service, parts.get(0), parts.get(1)); + return obtain(parts.get(0), parts.get(1)); } - public static SubjectReference obtain(@NonNull LPPermissionService service, @NonNull LPSubject subject) { - SubjectReference ret = obtain(service, subject.getParentCollection().getIdentifier(), subject.getIdentifier()); + public SubjectReference obtain(@NonNull LPSubject subject) { + SubjectReference ret = obtain(subject.getParentCollection().getIdentifier(), subject.getIdentifier()); ret.fillCache(subject); return ret; } - public static SubjectReference obtain(@NonNull LPPermissionService service, @NonNull Subject subject) { + public SubjectReference obtain(@NonNull Subject subject) { if (subject instanceof ProxiedSubject) { return ((ProxiedSubject) subject).getReference(); } - return obtain(service, subject.getContainingCollection().getIdentifier(), subject.getIdentifier()); + return obtain(subject.getContainingCollection().getIdentifier(), subject.getIdentifier()); } - public static SubjectReference obtain(@NonNull LPPermissionService service, @NonNull org.spongepowered.api.service.permission.SubjectReference reference) { + public SubjectReference obtain(@NonNull org.spongepowered.api.service.permission.SubjectReference reference) { if (reference instanceof SubjectReference) { return ((SubjectReference) reference); } else { - return obtain(service, reference.getCollectionIdentifier(), reference.getSubjectIdentifier()); + return obtain(reference.getCollectionIdentifier(), reference.getSubjectIdentifier()); } } - public static SubjectReference obtain(@NonNull LPPermissionService service, @NonNull String collectionIdentifier, @NonNull String subjectIdentifier) { - return REFERENCE_CACHE.get(new SubjectReferenceAttributes(service, collectionIdentifier, subjectIdentifier)); + public SubjectReference obtain(@NonNull String collectionIdentifier, @NonNull String subjectIdentifier) { + return referenceCache.get(new SubjectReferenceAttributes(collectionIdentifier, subjectIdentifier)); } /** @@ -94,8 +122,6 @@ public final class SubjectReferenceFactory { */ @AllArgsConstructor private static final class SubjectReferenceAttributes { - private final LPPermissionService permissionService; - private final String collectionId; private final String id; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java index 3c72be9b6..81658e307 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java @@ -47,6 +47,7 @@ import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; import me.lucko.luckperms.sponge.service.model.SubjectReference; +import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory; import me.lucko.luckperms.sponge.service.persisted.PersistedCollection; import me.lucko.luckperms.sponge.service.storage.SubjectStorage; @@ -77,6 +78,7 @@ public class LuckPermsService implements LPPermissionService { @Getter(AccessLevel.NONE) private final PermissionService spongeProxy; + private final SubjectReferenceFactory referenceFactory; private final SubjectStorage storage; private final SpongeUserManager userSubjects; private final SpongeGroupManager groupSubjects; @@ -89,6 +91,7 @@ public class LuckPermsService implements LPPermissionService { public LuckPermsService(LPSpongePlugin plugin) { this.plugin = plugin; + this.referenceFactory = new SubjectReferenceFactory(this); this.spongeProxy = ProxyFactory.toSponge(this); storage = new SubjectStorage(this, new File(plugin.getDataDirectory(), "sponge-data"));