Move DiscordSRVApiProvider methods to DiscordSRVApi

This commit is contained in:
Vankka 2022-03-28 17:06:25 +03:00
parent 2d379dcb7c
commit 9124c18d7a
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
4 changed files with 37 additions and 43 deletions

View File

@ -24,51 +24,17 @@
package com.discordsrv.api;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
/**
* The {@link DiscordSRVApi} provider.
*/
@SuppressWarnings("unused") // API, Reflection
public final class DiscordSRVApiProvider {
private static DiscordSRVApi API;
private DiscordSRVApiProvider() {}
@ApiStatus.Internal
final class ApiInstanceHolder {
static DiscordSRVApi API;
private ApiInstanceHolder() {}
private static void provide(DiscordSRVApi api) {
DiscordSRVApiProvider.API = api;
ApiInstanceHolder.API = api;
}
/**
* Checks if the API instance is available.
* @return true if {@link #get()} and {@link #optional()} will return the API instance
*/
public static boolean isAvailable() {
return API != null;
}
/**
* Gets the instance of {@link DiscordSRVApi}.
* @return the DiscordSRV api, or {@code null} if not available
* @see #isAvailable()
*/
@Nullable
public static DiscordSRVApi get() {
return API;
}
/**
* Returns a {@link Optional} of {@link DiscordSRVApi}.
* @return the DiscordSRV api in an optional
* @see #isAvailable()
*/
@NotNull
public static Optional<DiscordSRVApi> optional() {
return Optional.ofNullable(API);
}
}

View File

@ -34,17 +34,46 @@ import com.discordsrv.api.profile.IProfileManager;
import net.dv8tion.jda.api.JDA;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
/**
* The DiscordSRV API.
*
* Use your platform's service provider or {@link DiscordSRVApiProvider} to get the instance.
* Use your platform's service provider or {@link #get()} / {@link #optional()} to get the instance.
*/
@SuppressWarnings("unused") // API
public interface DiscordSRVApi {
/**
* Gets the instance of {@link DiscordSRVApi}.
* @return the DiscordSRV api, or {@code null} if not available
* @see #isAvailable()
*/
@Nullable
static DiscordSRVApi get() {
return ApiInstanceHolder.API;
}
/**
* Returns a {@link Optional} of {@link DiscordSRVApi}.
* @return the DiscordSRV api in an optional
* @see #isAvailable()
*/
@NotNull
static Optional<DiscordSRVApi> optional() {
return Optional.ofNullable(ApiInstanceHolder.API);
}
/**
* Checks if the API instance is available.
* @return true if {@link #get()} and {@link #optional()} will return the API instance
*/
static boolean isAvailable() {
return ApiInstanceHolder.API != null;
}
/**
* The status of this DiscordSRV instance.
* @return the current status of this DiscordSRV instance

View File

@ -24,7 +24,6 @@
package com.discordsrv.api.discord.api.entity.message.impl;
import com.discordsrv.api.DiscordSRVApi;
import com.discordsrv.api.DiscordSRVApiProvider;
import com.discordsrv.api.discord.api.entity.message.AllowedMention;
import com.discordsrv.api.discord.api.entity.message.DiscordMessageEmbed;
import com.discordsrv.api.discord.api.entity.message.SendableDiscordMessage;
@ -222,7 +221,7 @@ public class SendableDiscordMessageImpl implements SendableDiscordMessage {
@Override
public @NotNull Formatter applyPlaceholderService() {
DiscordSRVApi api = DiscordSRVApiProvider.get();
DiscordSRVApi api = DiscordSRVApi.get();
if (api == null) {
throw new IllegalStateException("DiscordSRVApi not available");
}

View File

@ -35,7 +35,7 @@ public final class ApiInstanceUtil {
public static void setInstance(@NotNull DiscordSRV discordSRV) {
// Avoids illegal access
try {
Class<?> apiProviderClass = Class.forName("com.discordsrv.api.DiscordSRVApiProvider");
Class<?> apiProviderClass = Class.forName("com.discordsrv.api.InstanceHolder");
Method provideMethod = apiProviderClass.getDeclaredMethod("provide", DiscordSRVApi.class);
provideMethod.setAccessible(true);
provideMethod.invoke(null, discordSRV);