All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.codetaco.math.impl.support.DefaultEquationSupport Maven / Gradle / Ivy
package com.codetaco.math.impl.support;
import com.codetaco.date.CalendarFactory;
import com.codetaco.math.impl.token.TokVariableWithValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;
public class DefaultEquationSupport implements EquationSupport {
static class Variable {
boolean systemGenerated;
Object value;
String name;
public Variable(final String _name, final Object _value) {
this(_name, _value, false);
}
public Variable(final String _name, final Object _value, final boolean sysgen) {
name = _name;
value = _value;
systemGenerated = sysgen;
}
}
private final static Logger logger = LoggerFactory.getLogger(DefaultEquationSupport.class.getName());
private Hashtable variables;
public DefaultEquationSupport() {
super();
setVariables(new Hashtable());
initializeWellKnownVariables();
}
@Override
public void assignVariable(final String variableName, final Object value) throws Exception {
assignVariable(variableName, value, false);
}
@Override
public void assignVariable(final String variableName, final Object value, final boolean sysgen) throws Exception {
/*
* Only store supported types in the stack.
*/
if (value instanceof Date) {
getVariables().put(variableName.toLowerCase(),
new Variable(variableName, CalendarFactory.asZoned((Date) value), sysgen));
} else if (value instanceof Calendar) {
getVariables().put(variableName.toLowerCase(),
new Variable(variableName, CalendarFactory.asZoned((Calendar) value), sysgen));
} else if (value instanceof LocalDateTime) {
getVariables().put(variableName.toLowerCase(),
new Variable(variableName, CalendarFactory.asZoned((LocalDateTime) value), sysgen));
} else if (value instanceof LocalDate) {
getVariables().put(variableName.toLowerCase(),
new Variable(variableName, CalendarFactory.asZoned((LocalDate) value), sysgen));
} else if (value instanceof LocalTime) {
getVariables().put(variableName.toLowerCase(),
new Variable(variableName, CalendarFactory.asZoned((LocalTime) value), sysgen));
} else {
getVariables().put(variableName.toLowerCase(),
new Variable(variableName, value, sysgen));
}
}
@Override
public void clear() {
final Enumeration varIter = getVariables().elements();
while (varIter.hasMoreElements()) {
final Variable var = varIter.nextElement();
if (var.systemGenerated) {
continue;
}
removeVariable(var.name);
}
}
@Override
public Set getVariableNames() {
return getVariableNames(true);
}
@Override
public Set getVariableNames(final boolean includeSystemGenerated) {
if (includeSystemGenerated) {
return variables.keySet();
}
final Set names = new HashSet<>();
for (final String name : variables.keySet()) {
if (!getVariables().get(name).systemGenerated) {
names.add(name);
}
}
return names;
}
private Hashtable getVariables() {
return variables;
}
void initializeWellKnownVariables() {
try {
assignVariable("PI", Math.PI, true);
assignVariable("E", Math.E, true);
assignVariable("true", new Boolean(true), true);
assignVariable("false", new Boolean(false), true);
assignVariable("now", "now", true);
assignVariable("today", "today", true);
} catch (final Exception e) {
logger.error("failed to initialize some well known variables: {}", e.getMessage(), e);
}
}
@Override
public void removeVariable(final String name) {
getVariables().remove(name);
}
@Override
public Hashtable resolveRate(
final String tableName,
final java.util.Date baseDate,
final double tableKey) throws Exception {
final Hashtable rates = new Hashtable<>();
rates.put(new Double(0), new Double(1));
return rates;
}
@Override
public double resolveRate(
final String tableId,
final java.util.Date effectiveDate,
final String key1,
final String key2,
final String key3,
final String key4,
final String key5) throws Exception {
return 1D;
}
@Override
public Object resolveVariable(final String variableName) throws Exception {
final Variable variable = getVariables().get(variableName);
if (variable == null) {
return null;
}
return variable.value;
}
@Override
public Object resolveVariable(final String variableName, final java.util.Date baseDate) throws Exception {
final Variable variable = getVariables().get(variableName);
if (variable == null) {
return null;
}
if (variable.value instanceof TokVariableWithValue) {
return ((TokVariableWithValue) variable.value).getCurrentValue();
}
return variable.value;
}
public void setVariables(final Hashtable newVariables) {
variables = newVariables;
}
}