fix abuse of static in SubjectReferenceFactory

This commit is contained in:
Luck 2017-12-30 16:11:41 +00:00
parent 764665cf15
commit 7bf666b062
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
3 changed files with 47 additions and 16 deletions

View File

@ -48,6 +48,8 @@ public interface LPPermissionService {
LuckPermsPlugin getPlugin();
SubjectReferenceFactory getReferenceFactory();
PermissionService sponge();
LPSubjectCollection getUserSubjects();

View File

@ -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<SubjectReferenceAttributes, SubjectReference> REFERENCE_CACHE = Caffeine.newBuilder()
private final LoadingCache<SubjectReferenceAttributes, SubjectReference> 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<String> 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;

View File

@ -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"));