mirror of
https://github.com/Rogiel/l2jserver2
synced 2025-12-09 08:52:51 +00:00
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* This file is part of l2jserver <l2jserver.com>.
|
||||
*
|
||||
* l2jserver is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* l2jserver is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jserver.util.calculator;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractFunction<T extends CalculatorContext> implements
|
||||
Function<T> {
|
||||
private final int order;
|
||||
|
||||
public AbstractFunction(int order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int order() {
|
||||
return order;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,8 +16,8 @@
|
||||
*/
|
||||
package com.l2jserver.util.calculator;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import com.l2jserver.util.factory.CollectionFactory;
|
||||
@@ -28,18 +28,19 @@ import com.l2jserver.util.factory.CollectionFactory;
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class Calculator implements Function<Double> {
|
||||
public class Calculator<T extends CalculatorContext> extends
|
||||
AbstractFunction<T> {
|
||||
/**
|
||||
* List of operations in this calculator
|
||||
*/
|
||||
private final List<FunctionContainer> functions = CollectionFactory
|
||||
.newList();
|
||||
private final List<Function<T>> functions = CollectionFactory.newList();
|
||||
|
||||
/**
|
||||
* Creates a new empty calculator. Functions can be add using
|
||||
* {@link #add(int, Function)}.
|
||||
*/
|
||||
public Calculator() {
|
||||
super(0x00);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,9 +50,10 @@ public class Calculator implements Function<Double> {
|
||||
* @param functions
|
||||
* the calculator functions
|
||||
*/
|
||||
public Calculator(Function<Double>... functions) {
|
||||
for (int i = 0; i < functions.length; i++) {
|
||||
this.functions.add(new FunctionContainer(i, functions[i]));
|
||||
public Calculator(Function<T>... functions) {
|
||||
super(0x00);
|
||||
for (final Function<T> func : functions) {
|
||||
this.functions.add(func);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,14 +68,14 @@ public class Calculator implements Function<Double> {
|
||||
* @param function
|
||||
* the operation
|
||||
*/
|
||||
public void add(int order, Function<Double> function) {
|
||||
functions.add(new FunctionContainer(order, function));
|
||||
Collections.sort(functions);
|
||||
public void add(Function<T> function) {
|
||||
functions.add(function);
|
||||
Collections.sort(functions, FunctionOrderComparator.SHARED_INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Imports all functions in the given <tt>calculator</tt>. This is useful to
|
||||
* preserve right calculation ordering but changes to original
|
||||
* Imports all functions from the given <tt>calculator</tt>. This is useful
|
||||
* to preserve right calculation ordering but changes to original
|
||||
* <tt>calculator</tt> will no reflect in this one.
|
||||
* <p>
|
||||
* This method will heuristically search for nested calculators.
|
||||
@@ -81,101 +83,48 @@ public class Calculator implements Function<Double> {
|
||||
* @param calculator
|
||||
* the calculator
|
||||
*/
|
||||
public void importFunctions(Calculator calculator) {
|
||||
for (final FunctionContainer container : calculator.functions) {
|
||||
if (container.function instanceof Calculator) {
|
||||
importFunctions((Calculator) container.function);
|
||||
public void importFunctions(Calculator<T> calculator) {
|
||||
for (final Function<T> function : calculator.functions) {
|
||||
if (function instanceof Calculator) {
|
||||
importFunctions((Calculator<T>) function);
|
||||
} else {
|
||||
functions.add(container);
|
||||
functions.add(function);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the result and output it. Input value is 0.
|
||||
* Removes all imported functions from the given <tt>calculator</tt>.
|
||||
* <p>
|
||||
* This method will heuristically search for nested calculators.
|
||||
*
|
||||
* @return the computed value
|
||||
* @see #calculate(Double)
|
||||
* @param calculator
|
||||
* the calculator
|
||||
*/
|
||||
public double calculate() {
|
||||
return calculate(0.00);
|
||||
public void removeFunctions(Calculator<T> calculator) {
|
||||
for (final Function<T> function : calculator.functions) {
|
||||
if (function instanceof Calculator) {
|
||||
removeFunctions((Calculator<T>) function);
|
||||
} else {
|
||||
functions.remove(function);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double calculate(Double input) {
|
||||
double result = input;
|
||||
for (final FunctionContainer container : functions) {
|
||||
result = container.function.calculate(result);
|
||||
public void calculate(T ctx) {
|
||||
for (final Function<T> function : functions) {
|
||||
function.calculate(ctx);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* <h1>-- Internal use only --</h1> Container used to sort calculator
|
||||
* functions. This class implements {@link Comparable} and can be used to
|
||||
* sort lists using {@link Collections#sort(List)} or
|
||||
* {@link Arrays#sort(Object[])}.
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
private static class FunctionContainer implements
|
||||
Comparable<FunctionContainer> {
|
||||
/**
|
||||
* The execution order
|
||||
*/
|
||||
protected final int order;
|
||||
/**
|
||||
* The function object
|
||||
*/
|
||||
protected final Function<Double> function;
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param order
|
||||
* the execution order
|
||||
* @param function
|
||||
* the function
|
||||
*/
|
||||
public FunctionContainer(int order, Function<Double> function) {
|
||||
this.order = order;
|
||||
this.function = function;
|
||||
}
|
||||
public static class FunctionOrderComparator implements
|
||||
Comparator<Function<?>> {
|
||||
public static final FunctionOrderComparator SHARED_INSTANCE = new FunctionOrderComparator();
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result
|
||||
+ ((function == null) ? 0 : function.hashCode());
|
||||
result = prime * result + order;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
FunctionContainer other = (FunctionContainer) obj;
|
||||
if (function == null) {
|
||||
if (other.function != null)
|
||||
return false;
|
||||
} else if (!function.equals(other.function))
|
||||
return false;
|
||||
if (order != other.order)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(FunctionContainer o) {
|
||||
if (this.equals(o))
|
||||
return 0;
|
||||
return this.order - o.order;
|
||||
public int compare(Function<?> func1, Function<?> func2) {
|
||||
return (func1.order() - func2.order());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* This file is part of l2jserver <l2jserver.com>.
|
||||
*
|
||||
* l2jserver is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* l2jserver is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with l2jserver. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jserver.util.calculator;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*
|
||||
*/
|
||||
public class CalculatorContext {
|
||||
public double result;
|
||||
}
|
||||
@@ -22,18 +22,19 @@ package com.l2jserver.util.calculator;
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class DivisionFunction implements Function<Double> {
|
||||
public class DivisionFunction extends AbstractFunction<CalculatorContext> {
|
||||
/**
|
||||
* The value
|
||||
*/
|
||||
private final double value;
|
||||
|
||||
public DivisionFunction(double value) {
|
||||
public DivisionFunction(int order, double value) {
|
||||
super(order);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double calculate(Double value) {
|
||||
return value / this.value;
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
ctx.result /= this.value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ package com.l2jserver.util.calculator;
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public interface Function<T extends Number> {
|
||||
public interface Function<T extends CalculatorContext> {
|
||||
/**
|
||||
* Performs the operation in the calculation process.
|
||||
* <p>
|
||||
@@ -33,5 +33,10 @@ public interface Function<T extends Number> {
|
||||
* the input value
|
||||
* @return the output value
|
||||
*/
|
||||
T calculate(T value);
|
||||
void calculate(T ctx);
|
||||
|
||||
/**
|
||||
* @return the order this function will be executed
|
||||
*/
|
||||
int order();
|
||||
}
|
||||
|
||||
@@ -22,11 +22,14 @@ package com.l2jserver.util.calculator;
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class ModulusFunction implements Function<Double> {
|
||||
public class ModulusFunction extends AbstractFunction<CalculatorContext> {
|
||||
public ModulusFunction(int order) {
|
||||
super(order);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double calculate(Double value) {
|
||||
if (value < 0)
|
||||
return value * -1;
|
||||
return value;
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
if (ctx.result < 0)
|
||||
ctx.result *= -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,18 +22,19 @@ package com.l2jserver.util.calculator;
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class MultiplicationFunction implements Function<Double> {
|
||||
public class MultiplicationFunction extends AbstractFunction<CalculatorContext> {
|
||||
/**
|
||||
* The value
|
||||
*/
|
||||
private final double value;
|
||||
|
||||
public MultiplicationFunction(double value) {
|
||||
public MultiplicationFunction(int order, double value) {
|
||||
super(order);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double calculate(Double value) {
|
||||
return value * this.value;
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
ctx.result *= this.value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,9 +22,13 @@ package com.l2jserver.util.calculator;
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class NegateFunction implements Function<Double> {
|
||||
public class NegateFunction extends AbstractFunction<CalculatorContext> {
|
||||
public NegateFunction(int order) {
|
||||
super(order);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double calculate(Double value) {
|
||||
return -value;
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
ctx.result = -ctx.result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class PercentFunction extends MultiplicationFunction {
|
||||
/**
|
||||
* The value
|
||||
*/
|
||||
public PercentFunction(double value) {
|
||||
super(value / 100);
|
||||
public PercentFunction(int order, double value) {
|
||||
super(order, value / 100);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,9 +21,13 @@ package com.l2jserver.util.calculator;
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class RoundFunction implements Function<Double> {
|
||||
public class RoundFunction extends AbstractFunction<CalculatorContext> {
|
||||
public RoundFunction(int order) {
|
||||
super(order);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double calculate(Double value) {
|
||||
return (double) Math.round(value);
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
ctx.result = Math.round(ctx.result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,23 +17,23 @@
|
||||
package com.l2jserver.util.calculator;
|
||||
|
||||
/**
|
||||
* This function performs an set. It ignores the input value and return its
|
||||
* own.
|
||||
* This function performs an set. It ignores the input value and return its own.
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class SetFunction implements Function<Double> {
|
||||
public class SetFunction extends AbstractFunction<CalculatorContext> {
|
||||
/**
|
||||
* The value
|
||||
*/
|
||||
private final double value;
|
||||
|
||||
public SetFunction(double value) {
|
||||
public SetFunction(int order, double value) {
|
||||
super(order);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double calculate(Double value) {
|
||||
return this.value;
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
ctx.result = value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,18 +22,19 @@ package com.l2jserver.util.calculator;
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class SubtractFunction implements Function<Double> {
|
||||
public class SubtractFunction extends AbstractFunction<CalculatorContext> {
|
||||
/**
|
||||
* The value
|
||||
*/
|
||||
private final double value;
|
||||
|
||||
public SubtractFunction(double value) {
|
||||
public SubtractFunction(int order, double value) {
|
||||
super(order);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double calculate(Double value) {
|
||||
return value - this.value;
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
ctx.result -= value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,18 +22,19 @@ package com.l2jserver.util.calculator;
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class SumFunction implements Function<Double> {
|
||||
public class SumFunction extends AbstractFunction<CalculatorContext> {
|
||||
/**
|
||||
* The value
|
||||
*/
|
||||
private final double value;
|
||||
|
||||
public SumFunction(double value) {
|
||||
public SumFunction(int order, double value) {
|
||||
super(order);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double calculate(Double value) {
|
||||
return value + this.value;
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
ctx.result += value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user