Made a bunch of API holders use AtomicReference

This commit is contained in:
Risto Lahtela 2020-11-05 21:58:31 +02:00
parent 12971b30ba
commit e6f879783d
7 changed files with 26 additions and 18 deletions

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.capability;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer; import java.util.function.Consumer;
/** /**
@ -49,7 +50,7 @@ public interface CapabilityService {
* @param isEnabledListener The boolean given to the method tells if Plan has enabled successfully. * @param isEnabledListener The boolean given to the method tells if Plan has enabled successfully.
*/ */
default void registerEnableListener(Consumer<Boolean> isEnabledListener) { default void registerEnableListener(Consumer<Boolean> isEnabledListener) {
ListHolder.ENABLE_LISTENERS.add(isEnabledListener); ListHolder.ENABLE_LISTENERS.get().add(isEnabledListener);
} }
/** /**
@ -64,6 +65,8 @@ public interface CapabilityService {
} }
class ListHolder { class ListHolder {
volatile static List<Consumer<Boolean>> ENABLE_LISTENERS = new CopyOnWriteArrayList<>(); volatile static AtomicReference<List<Consumer<Boolean>>> ENABLE_LISTENERS = new AtomicReference<>(
new CopyOnWriteArrayList<>()
);
} }
} }

View File

@ -20,6 +20,7 @@ import com.djrapitops.plan.delivery.web.resolver.Resolver;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
@ -34,7 +35,7 @@ import java.util.regex.Pattern;
public interface ResolverService { public interface ResolverService {
static ResolverService getInstance() { static ResolverService getInstance() {
return Optional.ofNullable(ResolverService.Holder.service) return Optional.ofNullable(ResolverService.Holder.service.get())
.orElseThrow(() -> new IllegalStateException("ResolverService has not been initialised yet.")); .orElseThrow(() -> new IllegalStateException("ResolverService has not been initialised yet."));
} }
@ -88,14 +89,14 @@ public interface ResolverService {
List<Resolver> getResolvers(String target); List<Resolver> getResolvers(String target);
class Holder { class Holder {
volatile static ResolverService service; volatile static AtomicReference<ResolverService> service = new AtomicReference<>();
private Holder() { private Holder() {
/* Static variable holder */ /* Static variable holder */
} }
static void set(ResolverService service) { static void set(ResolverService service) {
ResolverService.Holder.service = service; ResolverService.Holder.service.set(service);
} }
} }
} }

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.delivery.web;
import com.djrapitops.plan.delivery.web.resource.WebResource; import com.djrapitops.plan.delivery.web.resource.WebResource;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
@ -29,7 +30,7 @@ import java.util.function.Supplier;
public interface ResourceService { public interface ResourceService {
static ResourceService getInstance() { static ResourceService getInstance() {
return Optional.ofNullable(ResourceService.Holder.service) return Optional.ofNullable(ResourceService.Holder.service.get())
.orElseThrow(() -> new IllegalStateException("ResourceService has not been initialised yet.")); .orElseThrow(() -> new IllegalStateException("ResourceService has not been initialised yet."));
} }
@ -102,14 +103,14 @@ public interface ResourceService {
} }
class Holder { class Holder {
volatile static ResourceService service; volatile static AtomicReference<ResourceService> service = new AtomicReference<>();
private Holder() { private Holder() {
/* Static variable holder */ /* Static variable holder */
} }
static void set(ResourceService service) { static void set(ResourceService service) {
ResourceService.Holder.service = service; ResourceService.Holder.service.set(service);
} }
} }
} }

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.extension;
import com.djrapitops.plan.extension.extractor.ExtensionExtractor; import com.djrapitops.plan.extension.extractor.ExtensionExtractor;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
/** /**
* Interface for registering {@link DataExtension}s. * Interface for registering {@link DataExtension}s.
@ -44,7 +45,7 @@ public interface ExtensionService {
* @throws IllegalStateException If Plan is installed, but not enabled. * @throws IllegalStateException If Plan is installed, but not enabled.
*/ */
static ExtensionService getInstance() { static ExtensionService getInstance() {
return Optional.ofNullable(Holder.service) return Optional.ofNullable(Holder.service.get())
.orElseThrow(() -> new IllegalStateException("ExtensionService has not been initialised yet.")); .orElseThrow(() -> new IllegalStateException("ExtensionService has not been initialised yet."));
} }
@ -70,14 +71,14 @@ public interface ExtensionService {
void unregister(DataExtension extension); void unregister(DataExtension extension);
class Holder { class Holder {
volatile static ExtensionService service; volatile static AtomicReference<ExtensionService> service = new AtomicReference<>();
private Holder() { private Holder() {
/* Static variable holder */ /* Static variable holder */
} }
static void set(ExtensionService service) { static void set(ExtensionService service) {
Holder.service = service; Holder.service.set(service);
} }
} }

View File

@ -21,6 +21,7 @@ import java.sql.SQLException;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer; import java.util.function.Consumer;
/** /**
@ -40,7 +41,7 @@ public interface QueryService {
* @throws IllegalStateException If Plan is installed, but not enabled. * @throws IllegalStateException If Plan is installed, but not enabled.
*/ */
static QueryService getInstance() { static QueryService getInstance() {
return Optional.ofNullable(Holder.service) return Optional.ofNullable(Holder.service.get())
.orElseThrow(() -> new IllegalStateException("QueryService has not been initialised yet.")); .orElseThrow(() -> new IllegalStateException("QueryService has not been initialised yet."));
} }
@ -149,14 +150,14 @@ public interface QueryService {
} }
class Holder { class Holder {
volatile static QueryService service; volatile static AtomicReference<QueryService> service = new AtomicReference<>();
private Holder() { private Holder() {
/* Static variable holder */ /* Static variable holder */
} }
static void set(QueryService service) { static void set(QueryService service) {
Holder.service = service; Holder.service.set(service);
} }
} }

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.settings;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
@ -33,7 +34,7 @@ import java.util.function.Supplier;
public interface SettingsService { public interface SettingsService {
static SettingsService getInstance() { static SettingsService getInstance() {
return Optional.ofNullable(Holder.service) return Optional.ofNullable(Holder.service.get())
.orElseThrow(() -> new IllegalStateException("SettingsService has not been initialised yet.")); .orElseThrow(() -> new IllegalStateException("SettingsService has not been initialised yet."));
} }
@ -65,14 +66,14 @@ public interface SettingsService {
List<String> getStringList(String path, Supplier<List<String>> defaultValue); List<String> getStringList(String path, Supplier<List<String>> defaultValue);
class Holder { class Holder {
volatile static SettingsService service; volatile static AtomicReference<SettingsService> service = new AtomicReference<>();
private Holder() { private Holder() {
/* Static variable holder */ /* Static variable holder */
} }
static void set(SettingsService service) { static void set(SettingsService service) {
Holder.service = service; Holder.service.set(service);
} }
} }

View File

@ -28,7 +28,7 @@ import java.util.function.Consumer;
public class CapabilitySvc implements CapabilityService { public class CapabilitySvc implements CapabilityService {
public static void notifyAboutEnable(boolean isEnabled) { public static void notifyAboutEnable(boolean isEnabled) {
for (Consumer<Boolean> enableListener : CapabilityService.ListHolder.ENABLE_LISTENERS) { for (Consumer<Boolean> enableListener : CapabilityService.ListHolder.ENABLE_LISTENERS.get()) {
enableListener.accept(isEnabled); enableListener.accept(isEnabled);
} }
} }