1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-08 08:23:11 +00:00

Several improvements

Signed-off-by: Rogiel <rogiel@rogiel.com>
This commit is contained in:
2011-05-19 01:40:53 -03:00
parent 9bb83652e4
commit 2c4af6d91d
263 changed files with 2135 additions and 663 deletions

View File

@@ -77,7 +77,7 @@ public class RGBColor {
* @return the color integer
*/
public int toInteger() {
return red >> 24 + green >> 16 + blue >> 8;
return (red >> 24) + (green >> 16) + (blue >> 8);
}
public static RGBColor fromByteArray(byte[] rgb) {

View File

@@ -16,50 +16,166 @@
*/
package com.l2jserver.util.calculator;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import com.l2jserver.util.factory.CollectionFactory;
/**
* An calculator is used to compute data and outputs its result. Note also, that
* an calculator is also an operation, that way you can nest calculators.
* an calculator is also an function, that way you can nest calculators.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class Calculator implements Operation<Double> {
public class Calculator implements Function<Double> {
/**
* List of operations in this calculator
*/
private final List<Operation<Double>> operations = CollectionFactory
private final List<FunctionContainer> functions = CollectionFactory
.newList(null);
/**
* Adds a new operation to this calculator. Operations with the same order
* are replaced.
*
* @param order
* the operation order, starting at 0.
* @param operation
* the operation
* Creates a new empty calculator. Functions can be add using
* {@link #add(int, Function)}.
*/
public void add(int order, Operation<Double> operation) {
operations.add(order, operation);
public Calculator() {
}
/**
* Computes the result and output it
* Creates a new calculator with <tt>functions</tt> in the declaration
* order.
*
* @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]));
}
}
/**
* Adds a new function to this calculator. Executing order for functions
* with the same order is undefined.
* <p>
* Once a new function is added, sorting will be performed automatically.
*
* @param order
* the operation order, starting at 0.
* @param function
* the operation
*/
public void add(int order, Function<Double> function) {
functions.add(new FunctionContainer(order, function));
Collections.sort(functions);
}
/**
* Imports all functions in 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.
*
* @param calculator
* the calculator
*/
public void importFunctions(Calculator calculator) {
for (final FunctionContainer container : calculator.functions) {
if (container.function instanceof Calculator) {
importFunctions((Calculator) container.function);
} else {
functions.add(container);
}
}
}
/**
* Computes the result and output it. Input value is 0.
*
* @return the computed value
* @see #calculate(Double)
*/
public double compute() {
return perform(0.00);
public double calculate() {
return calculate(0.00);
}
@Override
public Double perform(Double result) {
for (final Operation<Double> operation : operations) {
result = operation.perform(result);
public Double calculate(Double input) {
double result = input;
for (final FunctionContainer container : functions) {
result = container.function.calculate(result);
}
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;
}
@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;
}
}
}

View File

@@ -17,23 +17,23 @@
package com.l2jserver.util.calculator;
/**
* This operation performs a division: <blockquote><code>chain value /
* This function performs a division: <blockquote><code>chain value /
* value</code></blockquote>
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DivisionOperation implements Operation<Double> {
public class DivisionFunction implements Function<Double> {
/**
* The value
*/
private final double value;
public DivisionOperation(double value) {
public DivisionFunction(double value) {
this.value = value;
}
@Override
public Double perform(Double value) {
public Double calculate(Double value) {
return value / this.value;
}
}

View File

@@ -17,15 +17,21 @@
package com.l2jserver.util.calculator;
/**
* An function is nothing more than a mathematical operation.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public interface Operation<T extends Number> {
public interface Function<T extends Number> {
/**
* Performs the operation in the calculation process
* Performs the operation in the calculation process.
* <p>
* The <tt>value</tt> in the argument is normally used for calculation, but
* an {@link Function} can ignore the value if required to (i.e.
* {@link SetFunction})
*
* @param value
* the chain input value
* the input value
* @return the output value
*/
T perform(T value);
T calculate(T value);
}

View File

@@ -17,14 +17,14 @@
package com.l2jserver.util.calculator;
/**
* This operation performs an modulus: <blockquote><code>|chain value|</code>
* This function performs an modulus: <blockquote><code>|chain value|</code>
* </blockquote>
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class ModulusOperation implements Operation<Double> {
public class ModulusFunction implements Function<Double> {
@Override
public Double perform(Double value) {
public Double calculate(Double value) {
if (value < 0)
return value * -1;
return value;

View File

@@ -17,23 +17,23 @@
package com.l2jserver.util.calculator;
/**
* This operation performs an multiplication: <blockquote><code>chain value *
* This function performs an multiplication: <blockquote><code>chain value *
* value</code></blockquote>
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class MultiplicationOperation implements Operation<Double> {
public class MultiplicationFunction implements Function<Double> {
/**
* The value
*/
private final double value;
public MultiplicationOperation(double value) {
public MultiplicationFunction(double value) {
this.value = value;
}
@Override
public Double perform(Double value) {
public Double calculate(Double value) {
return value * this.value;
}
}

View File

@@ -17,14 +17,14 @@
package com.l2jserver.util.calculator;
/**
* This operation performs an negate: <blockquote><code>-chain value</code>
* This function performs an negate: <blockquote><code>-chain value</code>
* </blockquote>
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class NegateOperation implements Operation<Double> {
public class NegateFunction implements Function<Double> {
@Override
public Double perform(Double value) {
public Double calculate(Double value) {
return -value;
}
}

View File

@@ -17,16 +17,16 @@
package com.l2jserver.util.calculator;
/**
* This operation performs a multiplication: <blockquote><code>chain value *
* This function performs a multiplication: <blockquote><code>chain value *
* (value / 100)</code></blockquote>
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class PercentOperation extends MultiplicationOperation {
public class PercentFunction extends MultiplicationFunction {
/**
* The value
*/
public PercentOperation(double value) {
public PercentFunction(double value) {
super(value / 100);
}
}

View File

@@ -17,13 +17,13 @@
package com.l2jserver.util.calculator;
/**
* This operation performs an rounding in the number.
* This function performs an rounding in the number.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class RoundOperation implements Operation<Double> {
public class RoundFunction implements Function<Double> {
@Override
public Double perform(Double value) {
public Double calculate(Double value) {
return (double) Math.round(value);
}
}

View File

@@ -17,23 +17,23 @@
package com.l2jserver.util.calculator;
/**
* This operation performs an set. It ignores the input value and return its
* 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 SetOperation implements Operation<Double> {
public class SetFunction implements Function<Double> {
/**
* The value
*/
private final double value;
public SetOperation(double value) {
public SetFunction(double value) {
this.value = value;
}
@Override
public Double perform(Double value) {
public Double calculate(Double value) {
return this.value;
}
}

View File

@@ -17,23 +17,23 @@
package com.l2jserver.util.calculator;
/**
* This operation performs an subtraction: <blockquote><code>chain value -
* This function performs an subtraction: <blockquote><code>chain value -
* value</code></blockquote>
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class SubtractOperation implements Operation<Double> {
public class SubtractFunction implements Function<Double> {
/**
* The value
*/
private final double value;
public SubtractOperation(double value) {
public SubtractFunction(double value) {
this.value = value;
}
@Override
public Double perform(Double value) {
public Double calculate(Double value) {
return value - this.value;
}
}

View File

@@ -17,23 +17,23 @@
package com.l2jserver.util.calculator;
/**
* This operation performs a sum: <blockquote><code>chain value +
* This function performs a sum: <blockquote><code>chain value +
* value</code></blockquote>
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class SumOperation implements Operation<Double> {
public class SumFunction implements Function<Double> {
/**
* The value
*/
private final double value;
public SumOperation(double value) {
public SumFunction(double value) {
this.value = value;
}
@Override
public Double perform(Double value) {
public Double calculate(Double value) {
return value + this.value;
}
}