Add basic boolean operations

This commit is contained in:
Flowsqy 2022-02-15 20:31:52 +01:00
parent 2ce87936c7
commit f36b30ffa1
7 changed files with 283 additions and 0 deletions

View File

@ -0,0 +1,64 @@
package de.epiceric.shopchest.config.hologram.condition;
import de.epiceric.shopchest.config.hologram.HologramFormat;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
public abstract class AbstractEqualityCondition<T> implements Condition {
protected final Function<Map<HologramFormat.Requirement, Object>, T> firstArgProvider;
protected final Function<Map<HologramFormat.Requirement, Object>, T> secondArgProvider;
public AbstractEqualityCondition(
Function<Map<HologramFormat.Requirement, Object>, T> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, T> secondArgProvider
) {
this.firstArgProvider = firstArgProvider;
this.secondArgProvider = secondArgProvider;
}
public static class EqualityCondition<T> extends AbstractEqualityCondition<T> {
public EqualityCondition(
Function<Map<HologramFormat.Requirement, Object>, T> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, T> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return Objects.equals(firstArgProvider.apply(requirementValues), secondArgProvider.apply(requirementValues));
}
@Override
public String toString() {
return "(" + firstArgProvider.toString() + " == " + secondArgProvider.toString() + ")";
}
}
public static class InequalityCondition<T> extends AbstractEqualityCondition<T> {
public InequalityCondition(
Function<Map<HologramFormat.Requirement, Object>, T> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, T> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return !Objects.equals(firstArgProvider.apply(requirementValues), secondArgProvider.apply(requirementValues));
}
@Override
public String toString() {
return "(" + firstArgProvider.toString() + " != " + secondArgProvider.toString() + ")";
}
}
}

View File

@ -0,0 +1,26 @@
package de.epiceric.shopchest.config.hologram.condition;
import de.epiceric.shopchest.config.hologram.HologramFormat;
import java.util.Map;
public class AndCondition implements Condition {
private final Condition firstCondition;
private final Condition secondCondition;
public AndCondition(Condition firstCondition, Condition secondCondition) {
this.firstCondition = firstCondition;
this.secondCondition = secondCondition;
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return firstCondition.test(requirementValues) && secondCondition.test(requirementValues);
}
@Override
public String toString() {
return "(" + firstCondition.toString() + " && " + secondCondition.toString() + ")";
}
}

View File

@ -0,0 +1,25 @@
package de.epiceric.shopchest.config.hologram.condition;
import de.epiceric.shopchest.config.hologram.HologramFormat;
import java.util.Map;
import java.util.function.Function;
public class BooleanCondition implements Condition {
private final Function<Map<HologramFormat.Requirement, Object>, Boolean> booleanProvider;
public BooleanCondition(Function<Map<HologramFormat.Requirement, Object>, Boolean> booleanProvider) {
this.booleanProvider = booleanProvider;
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return booleanProvider.apply(requirementValues);
}
@Override
public String toString() {
return booleanProvider.toString();
}
}

View File

@ -0,0 +1,105 @@
package de.epiceric.shopchest.config.hologram.condition;
import de.epiceric.shopchest.config.hologram.HologramFormat;
import java.util.Map;
import java.util.function.Function;
public abstract class ComparisonCondition implements Condition {
protected final Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider;
protected final Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider;
public ComparisonCondition(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
) {
this.firstArgProvider = firstArgProvider;
this.secondArgProvider = secondArgProvider;
}
public static class GreaterCondition extends ComparisonCondition {
public GreaterCondition(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return firstArgProvider.apply(requirementValues) > secondArgProvider.apply(requirementValues);
}
@Override
public String toString() {
return "(" + firstArgProvider.toString() + " > " + secondArgProvider.toString() + ")";
}
}
public static class LessCondition extends ComparisonCondition {
public LessCondition(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return firstArgProvider.apply(requirementValues) < secondArgProvider.apply(requirementValues);
}
@Override
public String toString() {
return "(" + firstArgProvider.toString() + " < " + secondArgProvider.toString() + ")";
}
}
public static class GreaterOrEqualCondition extends ComparisonCondition {
public GreaterOrEqualCondition(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return firstArgProvider.apply(requirementValues) >= secondArgProvider.apply(requirementValues);
}
@Override
public String toString() {
return "(" + firstArgProvider.toString() + " >= " + secondArgProvider.toString() + ")";
}
}
public static class LessOrEqualCondition extends ComparisonCondition {
public LessOrEqualCondition(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return firstArgProvider.apply(requirementValues) <= secondArgProvider.apply(requirementValues);
}
@Override
public String toString() {
return "(" + firstArgProvider.toString() + " <= " + secondArgProvider.toString() + ")";
}
}
}

View File

@ -0,0 +1,13 @@
package de.epiceric.shopchest.config.hologram.condition;
import de.epiceric.shopchest.config.hologram.HologramFormat;
import java.util.Map;
import java.util.function.Predicate;
/**
* Represent an hologram requirement condition
*/
public interface Condition extends Predicate<Map<HologramFormat.Requirement, Object>> {
}

View File

@ -0,0 +1,26 @@
package de.epiceric.shopchest.config.hologram.condition;
import de.epiceric.shopchest.config.hologram.HologramFormat;
import java.util.Map;
public class OrCondition implements Condition {
private final Condition firstCondition;
private final Condition secondCondition;
public OrCondition(Condition firstCondition, Condition secondCondition) {
this.firstCondition = firstCondition;
this.secondCondition = secondCondition;
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return firstCondition.test(requirementValues) || secondCondition.test(requirementValues);
}
@Override
public String toString() {
return "(" + firstCondition.toString() + " || " + secondCondition.toString() + ")";
}
}

View File

@ -0,0 +1,24 @@
package de.epiceric.shopchest.config.hologram.condition;
import de.epiceric.shopchest.config.hologram.HologramFormat;
import java.util.Map;
public class ReverseCondition implements Condition {
private final Condition condition;
public ReverseCondition(Condition condition) {
this.condition = condition;
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return !condition.test(requirementValues);
}
@Override
public String toString() {
return "(!" + condition.toString() + ")";
}
}