Generify basic operators and conditions and merge 'and' & 'or' condition

This commit is contained in:
Flowsqy 2022-02-16 16:02:38 +01:00
parent 4d7c0434ae
commit 2926e345fa
12 changed files with 171 additions and 220 deletions

View File

@ -1,26 +1,23 @@
package de.epiceric.shopchest.config.hologram.calculation;
import de.epiceric.shopchest.config.hologram.HologramFormat;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
/**
* Represents a hologram calculation
*/
public interface Calculation {
public interface Calculation<P> {
double calculate(Map<HologramFormat.Requirement, Object> provider);
double calculate(P provider);
abstract class AbstractCalculation implements Calculation {
abstract class AbstractCalculation<P> implements Calculation<P> {
protected final Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider;
protected final Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider;
protected final Function<P, Double> firstArgProvider;
protected final Function<P, Double> secondArgProvider;
public AbstractCalculation(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
Function<P, Double> firstArgProvider,
Function<P, Double> secondArgProvider
) {
this.firstArgProvider = firstArgProvider;
this.secondArgProvider = secondArgProvider;
@ -30,7 +27,7 @@ public interface Calculation {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractCalculation that = (AbstractCalculation) o;
AbstractCalculation<?> that = (AbstractCalculation<?>) o;
return Objects.equals(firstArgProvider, that.firstArgProvider) && Objects.equals(secondArgProvider, that.secondArgProvider);
}
@ -41,18 +38,18 @@ public interface Calculation {
}
class Addition extends AbstractCalculation {
class Addition<P> extends AbstractCalculation<P> {
public Addition(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
Function<P, Double> firstArgProvider,
Function<P, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public double calculate(Map<HologramFormat.Requirement, Object> requirementValues) {
return this.firstArgProvider.apply(requirementValues) + secondArgProvider.apply(requirementValues);
public double calculate(P values) {
return this.firstArgProvider.apply(values) + secondArgProvider.apply(values);
}
@Override
@ -61,18 +58,18 @@ public interface Calculation {
}
}
class Subtraction extends AbstractCalculation {
class Subtraction<P> extends AbstractCalculation<P> {
public Subtraction(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
Function<P, Double> firstArgProvider,
Function<P, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public double calculate(Map<HologramFormat.Requirement, Object> requirementValues) {
return this.firstArgProvider.apply(requirementValues) - secondArgProvider.apply(requirementValues);
public double calculate(P values) {
return this.firstArgProvider.apply(values) - secondArgProvider.apply(values);
}
@Override
@ -81,18 +78,18 @@ public interface Calculation {
}
}
class Multiplication extends AbstractCalculation {
class Multiplication<P> extends AbstractCalculation<P> {
public Multiplication(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
Function<P, Double> firstArgProvider,
Function<P, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public double calculate(Map<HologramFormat.Requirement, Object> requirementValues) {
return this.firstArgProvider.apply(requirementValues) * secondArgProvider.apply(requirementValues);
public double calculate(P values) {
return this.firstArgProvider.apply(values) * secondArgProvider.apply(values);
}
@Override
@ -101,18 +98,18 @@ public interface Calculation {
}
}
class Division extends AbstractCalculation {
class Division<P> extends AbstractCalculation<P> {
public Division(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
Function<P, Double> firstArgProvider,
Function<P, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public double calculate(Map<HologramFormat.Requirement, Object> requirementValues) {
return this.firstArgProvider.apply(requirementValues) / secondArgProvider.apply(requirementValues);
public double calculate(P values) {
return this.firstArgProvider.apply(values) / secondArgProvider.apply(values);
}
@Override
@ -121,18 +118,18 @@ public interface Calculation {
}
}
class Modulo extends AbstractCalculation {
class Modulo<P> extends AbstractCalculation<P> {
public Modulo(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
Function<P, Double> firstArgProvider,
Function<P, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public double calculate(Map<HologramFormat.Requirement, Object> requirementValues) {
return this.firstArgProvider.apply(requirementValues) % secondArgProvider.apply(requirementValues);
public double calculate(P values) {
return this.firstArgProvider.apply(values) % secondArgProvider.apply(values);
}
@Override

View File

@ -1,19 +1,16 @@
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 {
public abstract class AbstractEqualityCondition<P, T> implements Condition<P> {
protected final Function<Map<HologramFormat.Requirement, Object>, T> firstArgProvider;
protected final Function<Map<HologramFormat.Requirement, Object>, T> secondArgProvider;
protected final Function<P, T> firstArgProvider;
protected final Function<P, T> secondArgProvider;
public AbstractEqualityCondition(
Function<Map<HologramFormat.Requirement, Object>, T> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, T> secondArgProvider
Function<P, T> firstArgProvider,
Function<P, T> secondArgProvider
) {
this.firstArgProvider = firstArgProvider;
this.secondArgProvider = secondArgProvider;
@ -23,7 +20,7 @@ public abstract class AbstractEqualityCondition<T> implements Condition {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractEqualityCondition<?> that = (AbstractEqualityCondition<?>) o;
AbstractEqualityCondition<?, ?> that = (AbstractEqualityCondition<?, ?>) o;
return Objects.equals(firstArgProvider, that.firstArgProvider) && Objects.equals(secondArgProvider, that.secondArgProvider);
}
@ -32,18 +29,18 @@ public abstract class AbstractEqualityCondition<T> implements Condition {
return Objects.hash(firstArgProvider, secondArgProvider);
}
public static class EqualityCondition<T> extends AbstractEqualityCondition<T> {
public static class EqualityCondition<P, T> extends AbstractEqualityCondition<P, T> {
public EqualityCondition(
Function<Map<HologramFormat.Requirement, Object>, T> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, T> secondArgProvider
Function<P, T> firstArgProvider,
Function<P, T> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return Objects.equals(firstArgProvider.apply(requirementValues), secondArgProvider.apply(requirementValues));
public boolean test(P values) {
return Objects.equals(firstArgProvider.apply(values), secondArgProvider.apply(values));
}
@Override
@ -53,18 +50,18 @@ public abstract class AbstractEqualityCondition<T> implements Condition {
}
public static class InequalityCondition<T> extends AbstractEqualityCondition<T> {
public static class InequalityCondition<P, T> extends AbstractEqualityCondition<P, T> {
public InequalityCondition(
Function<Map<HologramFormat.Requirement, Object>, T> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, T> secondArgProvider
Function<P, T> firstArgProvider,
Function<P, T> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return !Objects.equals(firstArgProvider.apply(requirementValues), secondArgProvider.apply(requirementValues));
public boolean test(P values) {
return !Objects.equals(firstArgProvider.apply(values), secondArgProvider.apply(values));
}
@Override

View File

@ -1,40 +0,0 @@
package de.epiceric.shopchest.config.hologram.condition;
import de.epiceric.shopchest.config.hologram.HologramFormat;
import java.util.Map;
import java.util.Objects;
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 boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AndCondition that = (AndCondition) o;
return Objects.equals(firstCondition, that.firstCondition) && Objects.equals(secondCondition, that.secondCondition);
}
@Override
public int hashCode() {
return Objects.hash(firstCondition, secondCondition);
}
@Override
public String toString() {
return "(" + firstCondition.toString() + " && " + secondCondition.toString() + ")";
}
}

View File

@ -1,29 +1,26 @@
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 class BooleanCondition implements Condition {
public class BooleanCondition<P> implements Condition<P> {
private final Function<Map<HologramFormat.Requirement, Object>, Boolean> booleanProvider;
private final Function<P, Boolean> booleanProvider;
public BooleanCondition(Function<Map<HologramFormat.Requirement, Object>, Boolean> booleanProvider) {
public BooleanCondition(Function<P, Boolean> booleanProvider) {
this.booleanProvider = booleanProvider;
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return booleanProvider.apply(requirementValues);
public boolean test(P values) {
return booleanProvider.apply(values);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BooleanCondition that = (BooleanCondition) o;
BooleanCondition<?> that = (BooleanCondition<?>) o;
return Objects.equals(booleanProvider, that.booleanProvider);
}

View File

@ -1,19 +1,16 @@
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 ComparisonCondition implements Condition {
public abstract class ComparisonCondition<P> implements Condition<P> {
protected final Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider;
protected final Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider;
protected final Function<P, Double> firstArgProvider;
protected final Function<P, Double> secondArgProvider;
public ComparisonCondition(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
Function<P, Double> firstArgProvider,
Function<P, Double> secondArgProvider
) {
this.firstArgProvider = firstArgProvider;
this.secondArgProvider = secondArgProvider;
@ -23,7 +20,7 @@ public abstract class ComparisonCondition implements Condition {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ComparisonCondition that = (ComparisonCondition) o;
ComparisonCondition<?> that = (ComparisonCondition<?>) o;
return Objects.equals(firstArgProvider, that.firstArgProvider) && Objects.equals(secondArgProvider, that.secondArgProvider);
}
@ -32,18 +29,18 @@ public abstract class ComparisonCondition implements Condition {
return Objects.hash(firstArgProvider, secondArgProvider);
}
public static class GreaterCondition extends ComparisonCondition {
public static class GreaterCondition<P> extends ComparisonCondition<P> {
public GreaterCondition(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
Function<P, Double> firstArgProvider,
Function<P, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return firstArgProvider.apply(requirementValues) > secondArgProvider.apply(requirementValues);
public boolean test(P values) {
return firstArgProvider.apply(values) > secondArgProvider.apply(values);
}
@Override
@ -53,18 +50,18 @@ public abstract class ComparisonCondition implements Condition {
}
public static class LessCondition extends ComparisonCondition {
public static class LessCondition<P> extends ComparisonCondition<P> {
public LessCondition(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
Function<P, Double> firstArgProvider,
Function<P, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return firstArgProvider.apply(requirementValues) < secondArgProvider.apply(requirementValues);
public boolean test(P values) {
return firstArgProvider.apply(values) < secondArgProvider.apply(values);
}
@Override
@ -74,18 +71,18 @@ public abstract class ComparisonCondition implements Condition {
}
public static class GreaterOrEqualCondition extends ComparisonCondition {
public static class GreaterOrEqualCondition<P> extends ComparisonCondition<P> {
public GreaterOrEqualCondition(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
Function<P, Double> firstArgProvider,
Function<P, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return firstArgProvider.apply(requirementValues) >= secondArgProvider.apply(requirementValues);
public boolean test(P values) {
return firstArgProvider.apply(values) >= secondArgProvider.apply(values);
}
@Override
@ -95,18 +92,18 @@ public abstract class ComparisonCondition implements Condition {
}
public static class LessOrEqualCondition extends ComparisonCondition {
public static class LessOrEqualCondition<P> extends ComparisonCondition<P> {
public LessOrEqualCondition(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
Function<P, Double> firstArgProvider,
Function<P, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return firstArgProvider.apply(requirementValues) <= secondArgProvider.apply(requirementValues);
public boolean test(P values) {
return firstArgProvider.apply(values) <= secondArgProvider.apply(values);
}
@Override

View File

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

View File

@ -0,0 +1,62 @@
package de.epiceric.shopchest.config.hologram.condition;
import java.util.Objects;
public abstract class LogicCondition<P> implements Condition<P> {
protected final Condition<P> firstCondition;
protected final Condition<P> secondCondition;
public LogicCondition(Condition<P> firstCondition, Condition<P> secondCondition) {
this.firstCondition = firstCondition;
this.secondCondition = secondCondition;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LogicCondition<?> that = (LogicCondition<?>) o;
return Objects.equals(firstCondition, that.firstCondition) && Objects.equals(secondCondition, that.secondCondition);
}
@Override
public int hashCode() {
return Objects.hash(firstCondition, secondCondition);
}
public final static class AndCondition<P> extends LogicCondition<P> {
public AndCondition(Condition<P> firstCondition, Condition<P> secondCondition) {
super(firstCondition, secondCondition);
}
@Override
public boolean test(P values) {
return firstCondition.test(values) && secondCondition.test(values);
}
@Override
public String toString() {
return "(" + firstCondition.toString() + " && " + secondCondition.toString() + ")";
}
}
public final static class OrCondition<P> extends LogicCondition<P> {
public OrCondition(Condition<P> firstCondition, Condition<P> secondCondition) {
super(firstCondition, secondCondition);
}
@Override
public boolean test(P values) {
return firstCondition.test(values) || secondCondition.test(values);
}
@Override
public String toString() {
return "(" + firstCondition.toString() + " || " + secondCondition.toString() + ")";
}
}
}

View File

@ -1,40 +0,0 @@
package de.epiceric.shopchest.config.hologram.condition;
import de.epiceric.shopchest.config.hologram.HologramFormat;
import java.util.Map;
import java.util.Objects;
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 boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
OrCondition that = (OrCondition) o;
return Objects.equals(firstCondition, that.firstCondition) && Objects.equals(secondCondition, that.secondCondition);
}
@Override
public int hashCode() {
return Objects.hash(firstCondition, secondCondition);
}
@Override
public String toString() {
return "(" + firstCondition.toString() + " || " + secondCondition.toString() + ")";
}
}

View File

@ -1,28 +1,25 @@
package de.epiceric.shopchest.config.hologram.condition;
import de.epiceric.shopchest.config.hologram.HologramFormat;
import java.util.Map;
import java.util.Objects;
public class ReverseCondition implements Condition {
public class ReverseCondition<P> implements Condition<P> {
private final Condition condition;
private final Condition<P> condition;
public ReverseCondition(Condition condition) {
public ReverseCondition(Condition<P> condition) {
this.condition = condition;
}
@Override
public boolean test(Map<HologramFormat.Requirement, Object> requirementValues) {
return !condition.test(requirementValues);
public boolean test(P values) {
return !condition.test(values);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ReverseCondition that = (ReverseCondition) o;
ReverseCondition<?> that = (ReverseCondition<?>) o;
return Objects.equals(condition, that.condition);
}

View File

@ -1,11 +1,9 @@
package de.epiceric.shopchest.config.hologram.provider;
import de.epiceric.shopchest.config.hologram.HologramFormat;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
public class ConstantProvider<T> implements RequirementProvider<T> {
public class ConstantProvider<P, T> implements Function<P, T> {
private final T constant;
@ -15,7 +13,7 @@ public class ConstantProvider<T> implements RequirementProvider<T> {
@Override
public T apply(Map<HologramFormat.Requirement, Object> requirementObjectMap) {
public T apply(P values) {
return constant;
}
@ -23,7 +21,7 @@ public class ConstantProvider<T> implements RequirementProvider<T> {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ConstantProvider<?> that = (ConstantProvider<?>) o;
ConstantProvider<?, ?> that = (ConstantProvider<?, ?>) o;
return Objects.equals(constant, that.constant);
}

View File

@ -1,15 +1,14 @@
package de.epiceric.shopchest.config.hologram.provider;
import de.epiceric.shopchest.config.hologram.HologramFormat;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
public abstract class MapProvider<T> implements RequirementProvider<T> {
public abstract class MapProvider<P, T> implements Function<Map<P, Object>, T> {
protected final HologramFormat.Requirement requirement;
protected final P requirement;
public MapProvider(HologramFormat.Requirement requirement) {
public MapProvider(P requirement) {
this.requirement = requirement;
}
@ -17,7 +16,7 @@ public abstract class MapProvider<T> implements RequirementProvider<T> {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MapProvider<?> that = (MapProvider<?>) o;
MapProvider<?, ?> that = (MapProvider<?, ?>) o;
return requirement == that.requirement;
}
@ -31,41 +30,41 @@ public abstract class MapProvider<T> implements RequirementProvider<T> {
return requirement.toString();
}
public final static class StringMapProvider extends MapProvider<String> {
public final static class StringMapProvider<P> extends MapProvider<P, String> {
public StringMapProvider(HologramFormat.Requirement requirement) {
public StringMapProvider(P requirement) {
super(requirement);
}
@Override
public String apply(Map<HologramFormat.Requirement, Object> requirementValues) {
return (String) requirementValues.get(requirement);
public String apply(Map<P, Object> values) {
return (String) values.get(requirement);
}
}
public final static class BooleanMapProvider extends MapProvider<Boolean> {
public final static class BooleanMapProvider<P> extends MapProvider<P, Boolean> {
public BooleanMapProvider(HologramFormat.Requirement requirement) {
public BooleanMapProvider(P requirement) {
super(requirement);
}
@Override
public Boolean apply(Map<HologramFormat.Requirement, Object> requirementValues) {
return (Boolean) requirementValues.get(requirement);
public Boolean apply(Map<P, Object> values) {
return (Boolean) values.get(requirement);
}
}
public final static class DoubleMapProvider extends MapProvider<Double> {
public final static class DoubleMapProvider<P> extends MapProvider<P, Double> {
public DoubleMapProvider(HologramFormat.Requirement requirement) {
public DoubleMapProvider(P requirement) {
super(requirement);
}
@Override
public Double apply(Map<HologramFormat.Requirement, Object> requirementValues) {
return (Double) requirementValues.get(requirement);
public Double apply(Map<P, Object> values) {
return (Double) values.get(requirement);
}
}

View File

@ -1,10 +0,0 @@
package de.epiceric.shopchest.config.hologram.provider;
import de.epiceric.shopchest.config.hologram.HologramFormat;
import java.util.Map;
import java.util.function.Function;
public interface RequirementProvider<T> extends Function<Map<HologramFormat.Requirement, Object>, T> {
}