mirror of
https://github.com/Rogiel/l2jserver2
synced 2025-12-08 08:23:11 +00:00
@@ -16,12 +16,13 @@
|
||||
*/
|
||||
package com.l2jserver.util.calculator;
|
||||
|
||||
import com.l2jserver.model.world.Actor;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractFunction<T extends CalculatorContext> implements
|
||||
Function<T> {
|
||||
public abstract class AbstractFunction<O extends Actor> implements Function<O> {
|
||||
private final int order;
|
||||
|
||||
public AbstractFunction(int order) {
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import com.l2jserver.model.world.Actor;
|
||||
import com.l2jserver.util.factory.CollectionFactory;
|
||||
|
||||
/**
|
||||
@@ -28,12 +29,11 @@ import com.l2jserver.util.factory.CollectionFactory;
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class Calculator<T extends CalculatorContext> extends
|
||||
AbstractFunction<T> {
|
||||
public class Calculator<O extends Actor> extends AbstractFunction<O> {
|
||||
/**
|
||||
* List of operations in this calculator
|
||||
*/
|
||||
private final List<Function<T>> functions = CollectionFactory.newList();
|
||||
private final List<Function<O>> functions = CollectionFactory.newList();
|
||||
|
||||
/**
|
||||
* Creates a new empty calculator. Functions can be add using
|
||||
@@ -50,9 +50,9 @@ public class Calculator<T extends CalculatorContext> extends
|
||||
* @param functions
|
||||
* the calculator functions
|
||||
*/
|
||||
public Calculator(Function<T>... functions) {
|
||||
public Calculator(Function<O>... functions) {
|
||||
super(0x00);
|
||||
for (final Function<T> func : functions) {
|
||||
for (final Function<O> func : functions) {
|
||||
this.functions.add(func);
|
||||
}
|
||||
}
|
||||
@@ -68,7 +68,7 @@ public class Calculator<T extends CalculatorContext> extends
|
||||
* @param function
|
||||
* the operation
|
||||
*/
|
||||
public void add(Function<T> function) {
|
||||
public void add(Function<O> function) {
|
||||
functions.add(function);
|
||||
Collections.sort(functions, FunctionOrderComparator.SHARED_INSTANCE);
|
||||
}
|
||||
@@ -83,10 +83,10 @@ public class Calculator<T extends CalculatorContext> extends
|
||||
* @param calculator
|
||||
* the calculator
|
||||
*/
|
||||
public void importFunctions(Calculator<T> calculator) {
|
||||
for (final Function<T> function : calculator.functions) {
|
||||
public void importFunctions(Calculator<O> calculator) {
|
||||
for (final Function<O> function : calculator.functions) {
|
||||
if (function instanceof Calculator) {
|
||||
importFunctions((Calculator<T>) function);
|
||||
importFunctions((Calculator<O>) function);
|
||||
} else {
|
||||
functions.add(function);
|
||||
}
|
||||
@@ -101,10 +101,10 @@ public class Calculator<T extends CalculatorContext> extends
|
||||
* @param calculator
|
||||
* the calculator
|
||||
*/
|
||||
public void removeFunctions(Calculator<T> calculator) {
|
||||
for (final Function<T> function : calculator.functions) {
|
||||
public void removeFunctions(Calculator<O> calculator) {
|
||||
for (final Function<O> function : calculator.functions) {
|
||||
if (function instanceof Calculator) {
|
||||
removeFunctions((Calculator<T>) function);
|
||||
removeFunctions((Calculator<O>) function);
|
||||
} else {
|
||||
functions.remove(function);
|
||||
}
|
||||
@@ -112,9 +112,9 @@ public class Calculator<T extends CalculatorContext> extends
|
||||
}
|
||||
|
||||
@Override
|
||||
public void calculate(T ctx) {
|
||||
for (final Function<T> function : functions) {
|
||||
function.calculate(ctx);
|
||||
public void calculate(O object, CalculatorContext ctx) {
|
||||
for (final Function<O> function : functions) {
|
||||
function.calculate(object, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,13 +16,15 @@
|
||||
*/
|
||||
package com.l2jserver.util.calculator;
|
||||
|
||||
import com.l2jserver.model.world.Actor;
|
||||
|
||||
/**
|
||||
* This function performs a division: <blockquote><code>chain value /
|
||||
* value</code></blockquote>
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class DivisionFunction extends AbstractFunction<CalculatorContext> {
|
||||
public class DivisionFunction<O extends Actor> extends AbstractFunction<O> {
|
||||
/**
|
||||
* The value
|
||||
*/
|
||||
@@ -34,7 +36,7 @@ public class DivisionFunction extends AbstractFunction<CalculatorContext> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
public void calculate(O actor, CalculatorContext ctx) {
|
||||
ctx.result /= this.value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,12 +16,14 @@
|
||||
*/
|
||||
package com.l2jserver.util.calculator;
|
||||
|
||||
import com.l2jserver.model.world.Actor;
|
||||
|
||||
/**
|
||||
* An function is nothing more than a mathematical operation.
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public interface Function<T extends CalculatorContext> {
|
||||
public interface Function<O extends Actor> {
|
||||
/**
|
||||
* Performs the operation in the calculation process.
|
||||
* <p>
|
||||
@@ -33,7 +35,7 @@ public interface Function<T extends CalculatorContext> {
|
||||
* the input value
|
||||
* @return the output value
|
||||
*/
|
||||
void calculate(T ctx);
|
||||
void calculate(O actor, CalculatorContext ctx);
|
||||
|
||||
/**
|
||||
* @return the order this function will be executed
|
||||
|
||||
@@ -16,19 +16,21 @@
|
||||
*/
|
||||
package com.l2jserver.util.calculator;
|
||||
|
||||
import com.l2jserver.model.world.Actor;
|
||||
|
||||
/**
|
||||
* This function performs an modulus: <blockquote><code>|chain value|</code>
|
||||
* </blockquote>
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class ModulusFunction extends AbstractFunction<CalculatorContext> {
|
||||
public class ModulusFunction<O extends Actor> extends AbstractFunction<O> {
|
||||
public ModulusFunction(int order) {
|
||||
super(order);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
public void calculate(O actor, CalculatorContext ctx) {
|
||||
if (ctx.result < 0)
|
||||
ctx.result *= -1;
|
||||
}
|
||||
|
||||
@@ -16,13 +16,16 @@
|
||||
*/
|
||||
package com.l2jserver.util.calculator;
|
||||
|
||||
import com.l2jserver.model.world.Actor;
|
||||
|
||||
/**
|
||||
* This function performs an multiplication: <blockquote><code>chain value *
|
||||
* value</code></blockquote>
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class MultiplicationFunction extends AbstractFunction<CalculatorContext> {
|
||||
public class MultiplicationFunction<O extends Actor> extends
|
||||
AbstractFunction<O> {
|
||||
/**
|
||||
* The value
|
||||
*/
|
||||
@@ -34,7 +37,7 @@ public class MultiplicationFunction extends AbstractFunction<CalculatorContext>
|
||||
}
|
||||
|
||||
@Override
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
public void calculate(O actor, CalculatorContext ctx) {
|
||||
ctx.result *= this.value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,19 +16,21 @@
|
||||
*/
|
||||
package com.l2jserver.util.calculator;
|
||||
|
||||
import com.l2jserver.model.world.Actor;
|
||||
|
||||
/**
|
||||
* This function performs an negate: <blockquote><code>-chain value</code>
|
||||
* </blockquote>
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class NegateFunction extends AbstractFunction<CalculatorContext> {
|
||||
public class NegateFunction<O extends Actor> extends AbstractFunction<O> {
|
||||
public NegateFunction(int order) {
|
||||
super(order);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
public void calculate(O actor, CalculatorContext ctx) {
|
||||
ctx.result = -ctx.result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,18 +16,20 @@
|
||||
*/
|
||||
package com.l2jserver.util.calculator;
|
||||
|
||||
import com.l2jserver.model.world.Actor;
|
||||
|
||||
/**
|
||||
* This function performs an rounding in the number.
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class RoundFunction extends AbstractFunction<CalculatorContext> {
|
||||
public class RoundFunction<O extends Actor> extends AbstractFunction<O> {
|
||||
public RoundFunction(int order) {
|
||||
super(order);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
public void calculate(O actor, CalculatorContext ctx) {
|
||||
ctx.result = Math.round(ctx.result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,12 +16,14 @@
|
||||
*/
|
||||
package com.l2jserver.util.calculator;
|
||||
|
||||
import com.l2jserver.model.world.Actor;
|
||||
|
||||
/**
|
||||
* 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 extends AbstractFunction<CalculatorContext> {
|
||||
public class SetFunction<O extends Actor> extends AbstractFunction<O> {
|
||||
/**
|
||||
* The value
|
||||
*/
|
||||
@@ -33,7 +35,7 @@ public class SetFunction extends AbstractFunction<CalculatorContext> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
public void calculate(O actor, CalculatorContext ctx) {
|
||||
ctx.result = value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,13 +16,15 @@
|
||||
*/
|
||||
package com.l2jserver.util.calculator;
|
||||
|
||||
import com.l2jserver.model.world.Actor;
|
||||
|
||||
/**
|
||||
* This function performs an subtraction: <blockquote><code>chain value -
|
||||
* value</code></blockquote>
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class SubtractFunction extends AbstractFunction<CalculatorContext> {
|
||||
public class SubtractFunction<O extends Actor> extends AbstractFunction<O> {
|
||||
/**
|
||||
* The value
|
||||
*/
|
||||
@@ -34,7 +36,7 @@ public class SubtractFunction extends AbstractFunction<CalculatorContext> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
public void calculate(O actor, CalculatorContext ctx) {
|
||||
ctx.result -= value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,13 +16,15 @@
|
||||
*/
|
||||
package com.l2jserver.util.calculator;
|
||||
|
||||
import com.l2jserver.model.world.Actor;
|
||||
|
||||
/**
|
||||
* This function performs a sum: <blockquote><code>chain value +
|
||||
* value</code></blockquote>
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class SumFunction extends AbstractFunction<CalculatorContext> {
|
||||
public class SumFunction<O extends Actor> extends AbstractFunction<O> {
|
||||
/**
|
||||
* The value
|
||||
*/
|
||||
@@ -34,7 +36,7 @@ public class SumFunction extends AbstractFunction<CalculatorContext> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void calculate(CalculatorContext ctx) {
|
||||
public void calculate(O actor, CalculatorContext ctx) {
|
||||
ctx.result += value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user