com.datastax.data.dataset.DataValue Maven / Gradle / Ivy
The newest version!
package com.datastax.data.dataset;
import com.datastax.data.dataset.event.DataTableEventAdapter;
import com.datastax.data.dataset.event.RowChangeEvent;
import com.datastax.data.dataset.event.TableChangeEvent;
import net.sf.jga.fn.EvaluationException;
import net.sf.jga.fn.Generator;
import net.sf.jga.fn.adaptor.Constant;
import net.sf.jga.parser.GeneratorRef;
import net.sf.jga.parser.ParseException;
import net.sf.jga.parser.UncheckedParseException;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DataValue {
protected static final String DEFAULT_NAME_PREFIX = "DataValue";
private static final NameGenerator NAMEGEN = new NameGenerator(DEFAULT_NAME_PREFIX);
private static final Logger LOG = Logger.getLogger(DataValue.class.getName());
private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
private DataSet dataSet;
private String name;
private String expression;
private Generator> exprImpl;
private Class type = Void.TYPE;
public DataValue(DataSet ds) {
assert ds != null;
this.dataSet = ds;
name = NAMEGEN.generateName(this);
}
public DataValue(DataSet ds, String name) {
this(ds);
if (name != null) {
setName(name);
}
}
public void setName(String name) {
if (this.name != name) {
assert DataSetUtils.isValidName(name);
String oldName = this.name;
this.name = name;
pcs.firePropertyChange("name", oldName, name);
}
}
public String getName() {
return name;
}
public DataSet getDataSet() {
return dataSet;
}
void removeFromDataSet() {
dataSet = null;
if (exprImpl != null) {
Parser.unregister(exprImpl, listener, sources);
}
}
public String getExpression() {
return expression;
}
public void setExpression(String expression) {
if (expression == null) {
expression = "";
}
if ( !(expression.equals(this.expression))) {
if (exprImpl != null) {
Parser.unregister(exprImpl, listener, sources);
exprImpl = null;
}
String oldExpression = this.expression;
this.expression = expression;
pcs.firePropertyChange("expression", oldExpression, expression);
}
}
public Object getValue() {
assert dataSet != null;
if (exprImpl == null) {
parseExpression();
}
try {
return exprImpl == null ? null : exprImpl.gen();
} catch (EvaluationException e) {
LOG.log(Level.WARNING, "Cannot evaluate \""+expression+"\": returning null. {1}",
new Object[]{expression, e.getMessage()});
return null;
}
}
public Class getType() {
if (exprImpl == null) {
parseExpression();
}
return type;
}
private void setType(Class type) {
if (type == null) {
String msg = "A DataValue's type may not be null";
throw new IllegalArgumentException(msg);
}
if (this.type != type) {
Class oldType = this.type;
this.type = type;
pcs.firePropertyChange("type", oldType, type);
}
}
public String toString() {
return "Value " +getName()+" == " +getExpression();
}
Parser getParser() {
return dataSet.getParser();
}
private boolean isParsing = false;
private void parseExpression() {
if (exprImpl != null)
return;
if (expression == null || expression.equals("")) {
exprImpl = new Constant