mirror of
https://github.com/Rogiel/l2jserver2
synced 2025-12-08 08:23:11 +00:00
38
src/main/java/com/l2jserver/util/calculator/Formula.java
Normal file
38
src/main/java/com/l2jserver/util/calculator/Formula.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.l2jserver.util.calculator;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.l2jserver.util.calculator.operation.AddOperation;
|
||||
import com.l2jserver.util.calculator.operation.CalculatorOperation;
|
||||
import com.l2jserver.util.calculator.operation.SetOperation;
|
||||
import com.l2jserver.util.calculator.operation.SubtractOperation;
|
||||
import com.l2jserver.util.factory.CollectionFactory;
|
||||
|
||||
public class Formula {
|
||||
private List<CalculatorOperation<Integer>> operations = CollectionFactory
|
||||
.newList(null);
|
||||
|
||||
public void addOperation(int order, CalculatorOperation<Integer> operation) {
|
||||
operations.set(order, operation);
|
||||
}
|
||||
|
||||
public void add(int order, int value) {
|
||||
addOperation(order, new AddOperation(value));
|
||||
}
|
||||
|
||||
public void subtract(int order, int value) {
|
||||
addOperation(order, new SubtractOperation(value));
|
||||
}
|
||||
|
||||
public void set(int order, int value) {
|
||||
addOperation(order, new SetOperation(value));
|
||||
}
|
||||
|
||||
public int calculate() {
|
||||
int value = 0;
|
||||
for (CalculatorOperation<Integer> operation : operations) {
|
||||
value = operation.calculate(value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.l2jserver.util.calculator.operation;
|
||||
|
||||
public class AddOperation implements CalculatorOperation<Integer> {
|
||||
private Integer value;
|
||||
|
||||
public AddOperation(Integer value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer calculate(Integer value) {
|
||||
return value + this.value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.l2jserver.util.calculator.operation;
|
||||
|
||||
public interface CalculatorOperation<T extends Number> {
|
||||
T calculate(T value);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.l2jserver.util.calculator.operation;
|
||||
|
||||
public class MultiplyOperation implements CalculatorOperation<Integer> {
|
||||
private Integer value;
|
||||
|
||||
public MultiplyOperation(Integer value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer calculate(Integer value) {
|
||||
return value * this.value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.l2jserver.util.calculator.operation;
|
||||
|
||||
public class SetOperation implements CalculatorOperation<Integer> {
|
||||
private Integer value;
|
||||
|
||||
public SetOperation(Integer value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer calculate(Integer value) {
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.l2jserver.util.calculator.operation;
|
||||
|
||||
public class SubtractOperation implements CalculatorOperation<Integer> {
|
||||
private Integer value;
|
||||
|
||||
public SubtractOperation(Integer value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer calculate(Integer value) {
|
||||
return value - this.value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user