Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,38 @@

import org.jetbrains.annotations.NotNull;

import java.util.concurrent.atomic.AtomicReference;

public final class PlayTimeApiProvider {

private static volatile PlayTimeApi API; // visibility across threads
private static final AtomicReference<PlayTimeApi> API = new AtomicReference<>();

private PlayTimeApiProvider() {
throw new UnsupportedOperationException("This class cannot be instantiated.");
}

@NotNull
public static PlayTimeApi get() {
final PlayTimeApi api = API;
final PlayTimeApi api = API.get();
if (api == null) {
throw new IllegalStateException("PlayTimeAPI is not registered.");
}
return api;
}

public static boolean isRegistered() {
return API != null;
return API.get() != null;
}

static synchronized void register(@NotNull PlayTimeApi api) {
if (API != null) {
static void register(@NotNull PlayTimeApi api) {
if (!API.compareAndSet(null, api)) {
throw new IllegalStateException("PlayTimeAPI is already registered.");
}
API = api;
}

static synchronized void unregister() {
if (API == null) {
static void unregister() {
if (API.getAndSet(null) == null) {
throw new IllegalStateException("PlayTimeAPI is not registered.");
}
API = null;
}
}
}
Loading