Skip to content

Commit

Permalink
Add method context-dependent conditional placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
Almighty-Satan committed Mar 4, 2024
1 parent 5d913c5 commit 80ba151
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/src/main/java/io/github/almightysatan/slams/Placeholder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Objects;
import java.util.function.BooleanSupplier;
import java.util.function.Predicate;

/**
* Represents a placeholder. Extends {@link PlaceholderResolver} as a placeholder can resolve itself.
Expand Down Expand Up @@ -220,6 +221,15 @@ public interface Placeholder extends PlaceholderResolver {
return contextual(key, type, (ContextualValueFunction<T>) contextValueFunction);
}

static <T extends Context> @NotNull Placeholder conditional(@NotNull String key, @NotNull Class<T> type, @NotNull Predicate<@NotNull T> valueFunction) {
Objects.requireNonNull(valueFunction);
return contextual(key, type, (context, arguments) -> {
if (arguments.size() != 2)
return "INVALID_CONDITIONAL";
return valueFunction.test(context) ? arguments.get(0) : arguments.get(1);
});
}

static @NotNull Placeholder conditional(@NotNull String key, @NotNull BooleanSupplier valueFunction) {
Objects.requireNonNull(valueFunction);
return of(key, (context, arguments) -> {
Expand Down

0 comments on commit 80ba151

Please sign in to comment.