Skip to content

Commit

Permalink
chore: Make the Fallback interface optional
Browse files Browse the repository at this point in the history
  • Loading branch information
kezz authored and zml2008 committed May 15, 2023
1 parent c0fef72 commit d24330f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
23 changes: 11 additions & 12 deletions api/src/main/java/net/kyori/adventure/util/Services.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,14 @@ private Services() {
}

/**
* A service that may be a fallback.
* A fallback service.
*
* <p>When used in tandem with {@link #serviceWithFallback(Class)}, classes that implement this interface
* will be ignored in favour of classes that do not implement this interface.</p>
*
* @since 4.13.0
*/
public interface Fallback {
/**
* Checks if this service is a fallback.
*
* @return if this service is a fallback service
* @since 4.13.0
*/
boolean isFallback();
}

/**
Expand All @@ -94,8 +90,9 @@ public interface Fallback {
* @param <P> the service type
* @return a service, or {@link Optional#empty()}
* @since 4.13.0
* @see Fallback
*/
public static <P extends Fallback> @NotNull Optional<P> serviceWithFallback(final @NotNull Class<P> type) {
public static <P> @NotNull Optional<P> serviceWithFallback(final @NotNull Class<P> type) {
final ServiceLoader<P> loader = Services0.loader(type);
final Iterator<P> it = loader.iterator();
P firstFallback = null;
Expand All @@ -113,10 +110,12 @@ public interface Fallback {
}
}

if (!instance.isFallback()) {
if (instance instanceof Fallback) {
if (firstFallback == null) {
firstFallback = instance;
}
} else {
return Optional.of(instance);
} else if (firstFallback == null) {
firstFallback = instance;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package net.kyori.adventure.text.serializer.gson;

import net.kyori.adventure.text.serializer.json.JsonComponentSerializer;
import net.kyori.adventure.util.Services;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

Expand All @@ -33,12 +34,7 @@
* @since 4.13.0
*/
@ApiStatus.Internal
public final class JsonComponentSerializerProviderImpl implements JsonComponentSerializer.Provider {
@Override
public boolean isFallback() {
return true;
}

public final class JsonComponentSerializerProviderImpl implements JsonComponentSerializer.Provider, Services.Fallback {
@Override
public @NotNull JsonComponentSerializer json() {
return GsonComponentSerializer.gson();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.ComponentSerializer;
import net.kyori.adventure.util.PlatformAPI;
import net.kyori.adventure.util.Services;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

Expand All @@ -53,7 +52,7 @@ public interface JsonComponentSerializer extends ComponentSerializer<Component,
*/
@ApiStatus.Internal
@PlatformAPI
interface Provider extends Services.Fallback {
interface Provider {
/**
* Provides a standard {@link JsonComponentSerializer}.
*
Expand Down

0 comments on commit d24330f

Please sign in to comment.