Add basic calculation operations

This commit is contained in:
Flowsqy 2022-02-15 20:55:08 +01:00
parent f36b30ffa1
commit a475cdc5e6
2 changed files with 131 additions and 1 deletions

View File

@ -0,0 +1,130 @@
package de.epiceric.shopchest.config.hologram.calculation;
import de.epiceric.shopchest.config.hologram.HologramFormat;
import java.util.Map;
import java.util.function.Function;
/**
* Represents a hologram calculation
*/
public interface Calculation {
double calculate(Map<HologramFormat.Requirement, Object> provider);
abstract class AbstractCalculation implements Calculation {
protected final Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider;
protected final Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider;
public AbstractCalculation(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
) {
this.firstArgProvider = firstArgProvider;
this.secondArgProvider = secondArgProvider;
}
}
class Addition extends AbstractCalculation {
public Addition(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public double calculate(Map<HologramFormat.Requirement, Object> requirementValues) {
return this.firstArgProvider.apply(requirementValues) + secondArgProvider.apply(requirementValues);
}
@Override
public String toString() {
return "(" + firstArgProvider.toString() + " + " + secondArgProvider.toString() + ")";
}
}
class Subtraction extends AbstractCalculation {
public Subtraction(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public double calculate(Map<HologramFormat.Requirement, Object> requirementValues) {
return this.firstArgProvider.apply(requirementValues) - secondArgProvider.apply(requirementValues);
}
@Override
public String toString() {
return "(" + firstArgProvider.toString() + " - " + secondArgProvider.toString() + ")";
}
}
class Multiplication extends AbstractCalculation {
public Multiplication(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public double calculate(Map<HologramFormat.Requirement, Object> requirementValues) {
return this.firstArgProvider.apply(requirementValues) * secondArgProvider.apply(requirementValues);
}
@Override
public String toString() {
return "(" + firstArgProvider.toString() + " * " + secondArgProvider.toString() + ")";
}
}
class Division extends AbstractCalculation {
public Division(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public double calculate(Map<HologramFormat.Requirement, Object> requirementValues) {
return this.firstArgProvider.apply(requirementValues) / secondArgProvider.apply(requirementValues);
}
@Override
public String toString() {
return "(" + firstArgProvider.toString() + " / " + secondArgProvider.toString() + ")";
}
}
class Modulo extends AbstractCalculation {
public Modulo(
Function<Map<HologramFormat.Requirement, Object>, Double> firstArgProvider,
Function<Map<HologramFormat.Requirement, Object>, Double> secondArgProvider
) {
super(firstArgProvider, secondArgProvider);
}
@Override
public double calculate(Map<HologramFormat.Requirement, Object> requirementValues) {
return this.firstArgProvider.apply(requirementValues) % secondArgProvider.apply(requirementValues);
}
@Override
public String toString() {
return "(" + firstArgProvider.toString() + " % " + secondArgProvider.toString() + ")";
}
}
}

View File

@ -6,7 +6,7 @@ import java.util.Map;
import java.util.function.Predicate;
/**
* Represent an hologram requirement condition
* Represents a hologram requirement condition
*/
public interface Condition extends Predicate<Map<HologramFormat.Requirement, Object>> {