org.jdice.calc.internal.FunctionData Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jcalc Show documentation
Show all versions of jcalc Show documentation
Fluent Java API for easier work with numbers, writing formula and calculations in Java.
The newest version!
/*
* Copyright 2014 Davor Sauer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jdice.calc.internal;
import org.jdice.calc.AbstractCalculator;
import org.jdice.calc.CalculatorException;
import org.jdice.calc.Function;
import org.jdice.calc.Num;
/**
* Data model for data forwarded to concrete function.
*
* @author Davor Sauer
*
*/
public class FunctionData {
private Function function;
private Object[] values;
private Num result;
public FunctionData(Class extends Function> function, Object... values) {
this.function = CacheExtension.getFunction(function);
setValues(values);
}
public FunctionData(Function function, Object ... values) {
this.function = function;
setValues(values);
}
public void setValues(Object ... values) {
this.values = new Object[values.length];
for(int i = 0; i < values.length; i++) {
Object o = values[i];
if (o instanceof AbstractCalculator)
this.values[i] = o;
else if (o instanceof Num)
this.values[i] = o;
else
this.values[i] = new Num(o);
}
}
public Function getFunction() {
return function;
}
public Object[] getValues() {
return values;
}
public Num getResult() {
return result;
}
public Num calculate(AbstractCalculator calc) {
Num [] allValues = new Num[this.values.length];
for(int i = 0; i < this.values.length; i++) {
Object o = this.values[i];
if (o instanceof Num) {
allValues[i] = (Num)o;
} else if (o instanceof AbstractCalculator) {
AbstractCalculator ac = (AbstractCalculator)o;
allValues[i] = ac.calculate();
}
}
try {
result = function.calc(calc, allValues);
}
catch (Exception e) {
throw new CalculatorException(e);
}
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(function.getSymbol());
sb.append("(");
for (int i = 0; i < values.length; i++) {
Object d = values[i];
if (i > 0)
sb.append(", ");
if (d instanceof Num)
sb.append(((Num)d).toString());
else if (d instanceof AbstractCalculator)
try {
sb.append(((AbstractCalculator)d).getInfix());
}
catch (Exception e) {
sb.append("-error-");
}
else
sb.append("-unknown-");
}
sb.append(")");
return sb.toString();
}
public String toStringWithDetail() {
StringBuilder sb = new StringBuilder();
sb.append(function.getSymbol());
sb.append("(");
for (int i = 0; i < values.length; i++) {
Object d = values[i];
if (i > 0)
sb.append(", ");
if (d instanceof Num) {
Num tmp = (Num)d;
sb.append("[" + tmp.getProperties().toString() + "] " + tmp.toString());
// sb.append(((Num)d).toStringWithDetail());
} else if (d instanceof AbstractCalculator)
try {
sb.append(((AbstractCalculator)d).getInfix());
}
catch (Exception e) {
sb.append("-error-");
}
else
sb.append("-unknown-");
}
sb.append(")");
return sb.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy