📄 functionexpression.java
字号:
package prefuse.data.expression;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import prefuse.data.Edge;
import prefuse.data.Node;
import prefuse.data.Schema;
import prefuse.data.Tuple;
import prefuse.util.ColorLib;
import prefuse.util.MathLib;
import prefuse.util.StringLib;
import prefuse.util.collections.CopyOnWriteArrayList;
/**
* Abstract base class for FunctionExpression implementations. Provides
* parameter handling support.
*
* @author <a href="http://jheer.org">jeffrey heer</a>
*/
public abstract class FunctionExpression extends AbstractExpression
implements Function
{
protected CopyOnWriteArrayList m_params;
protected final int m_pcount;
/**
* Protected constructor.
* @param parameterCount the max parameter count
*/
protected FunctionExpression(int parameterCount) {
m_pcount = parameterCount;
}
/**
* @see prefuse.data.expression.Function#getName()
*/
public abstract String getName();
/**
* @see prefuse.data.expression.Function#addParameter(prefuse.data.expression.Expression)
*/
public void addParameter(Expression e) {
int pc = getParameterCount();
if ( pc!=VARARGS && paramCount()+1 > pc ) {
throw new IllegalStateException(
"This function takes only "+pc+" parameters.");
}
if ( m_params == null )
m_params = new CopyOnWriteArrayList();
m_params.add(e);
}
/**
* An internal-only method that returns the current number of
* parameters collected.
*/
protected int paramCount() {
return m_params==null ? 0 : m_params.size();
}
/**
* Return the parameter expression at the given index.
* @param idx the parameter index
* @return the parameter value Expression at the given index
*/
protected final Expression param(int idx) {
return (Expression)m_params.get(idx);
}
/**
* @see prefuse.data.expression.Function#getParameterCount()
*/
public int getParameterCount() {
return m_pcount;
}
/**
* Throw an exception when needed parameters are missing.
*/
protected void missingParams() {
throw new IllegalStateException(
"Function is missing parameters: " + getName());
}
// ------------------------------------------------------------------------
/**
* @see prefuse.data.expression.Expression#visit(prefuse.data.expression.ExpressionVisitor)
*/
public void visit(ExpressionVisitor v) {
v.visitExpression(this);
if ( paramCount() > 0 ) {
Object[] params = m_params.getArray();
for ( int i=0; i<params.length; ++i ) {
v.down();
((Expression)params[i]).visit(v);
v.up();
}
}
}
/**
* @see prefuse.data.expression.AbstractExpression#addChildListeners()
*/
protected void addChildListeners() {
if ( paramCount() > 0 ) {
Object[] params = m_params.getArray();
for ( int i=0; i<params.length; ++i )
((Expression)params[i]).addExpressionListener(this);
}
}
/**
* @see prefuse.data.expression.AbstractExpression#removeChildListeners()
*/
protected void removeChildListeners() {
if ( paramCount() > 0 ) {
Object[] params = m_params.getArray();
for ( int i=0; i<params.length; ++i )
((Expression)params[i]).removeExpressionListener(this);
}
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer sbuf = new StringBuffer();
sbuf.append(getName()).append('(');
for ( int i=0; i<paramCount(); ++i ) {
if ( i > 0 ) sbuf.append(", ");
sbuf.append(param(i).toString());
}
sbuf.append(')');
return sbuf.toString();
}
} // end of class FunctionExpression
/**
* Default Function Implementations
*/
// ----------------------------------------------------------------------------
// Mathematical Functions
abstract class DoubleFunction extends FunctionExpression {
protected DoubleFunction(int parameterCount) {
super(parameterCount);
}
public Class getType(Schema s) {
return double.class;
}
public Object get(Tuple t) {
return new Double(getDouble(t));
}
public int getInt(Tuple t) {
return (int)getDouble(t);
}
public long getLong(Tuple t) {
return (long)getDouble(t);
}
public float getFloat(Tuple t) {
return (float)getDouble(t);
}
}
abstract class IntFunction extends FunctionExpression {
protected IntFunction(int parameterCount) {
super(parameterCount);
}
public Class getType(Schema s) {
return int.class;
}
public Object get(Tuple t) {
return new Integer(getInt(t));
}
public long getLong(Tuple t) {
return (long)getInt(t);
}
public float getFloat(Tuple t) {
return (float)getFloat(t);
}
public double getDouble(Tuple t) {
return (double)getInt(t);
}
}
abstract class BooleanFunction extends FunctionExpression
implements Predicate
{
protected BooleanFunction(int parameterCount) {
super(parameterCount);
}
public Class getType(Schema s) {
return boolean.class;
}
public Object get(Tuple t) {
return getBoolean(t) ? Boolean.TRUE : Boolean.FALSE;
}
}
//ROW()
class RowFunction extends IntFunction {
public RowFunction() { super(0); }
public String getName() { return "ROW"; }
public int getInt(Tuple t) {
return t.getRow();
}
}
//ISNODE()
class IsNodeFunction extends BooleanFunction {
public IsNodeFunction() { super(0); }
public String getName() { return "ISNODE"; }
public boolean getBoolean(Tuple t) {
return (t instanceof Node);
}
}
//ISEDGE()
class IsEdgeFunction extends BooleanFunction {
public IsEdgeFunction() { super(0); }
public String getName() { return "ISEDGE"; }
public boolean getBoolean(Tuple t) {
return (t instanceof Edge);
}
}
//DEGREE()
class DegreeFunction extends IntFunction {
public DegreeFunction() { super(0); }
public String getName() { return "DEGREE"; }
public int getInt(Tuple t) {
return (t instanceof Node ? ((Node)t).getDegree() : 0 );
}
}
//INDEGREE()
class InDegreeFunction extends IntFunction {
public InDegreeFunction() { super(0); }
public String getName() { return "INDEGREE"; }
public int getInt(Tuple t) {
return (t instanceof Node ? ((Node)t).getInDegree() : 0 );
}
}
//OUTDEGREE()
class OutDegreeFunction extends IntFunction {
public OutDegreeFunction() { super(0); }
public String getName() { return "OUTDEGREE"; }
public int getInt(Tuple t) {
return (t instanceof Node ? ((Node)t).getOutDegree() : 0 );
}
}
//CHILDCOUNT()
class ChildCountFunction extends IntFunction {
public ChildCountFunction() { super(0); }
public String getName() { return "CHILDCOUNT"; }
public int getInt(Tuple t) {
return (t instanceof Node ? ((Node)t).getChildCount() : 0 );
}
}
//TREEDEPTH()
class TreeDepthFunction extends IntFunction {
public TreeDepthFunction() { super(0); }
public String getName() { return "TREEDEPTH"; }
public int getInt(Tuple t) {
return (t instanceof Node ? ((Node)t).getDepth() : 0 );
}
}
//ABS(X)
class AbsFunction extends DoubleFunction {
public AbsFunction() { super(1); }
public String getName() { return "ABS"; }
public double getDouble(Tuple t) {
if ( paramCount() == 1 ) {
return Math.abs(param(0).getDouble(t));
} else {
missingParams(); return Double.NaN;
}
}
}
//ACOS(X)
class AcosFunction extends DoubleFunction {
public AcosFunction() { super(1); }
public String getName() { return "ACOS"; }
public double getDouble(Tuple t) {
if ( paramCount() == 1 ) {
return Math.acos(param(0).getDouble(t));
} else {
missingParams(); return Double.NaN;
}
}
}
//ASIN(X)
class AsinFunction extends DoubleFunction {
public AsinFunction() { super(1); }
public String getName() { return "ASIN"; }
public double getDouble(Tuple t) {
if ( paramCount() == 1 ) {
return Math.asin(param(0).getDouble(t));
} else {
missingParams(); return Double.NaN;
}
}
}
//ATAN(X)
class AtanFunction extends DoubleFunction {
public AtanFunction() { super(1); }
public String getName() { return "ATAN"; }
public double getDouble(Tuple t) {
if ( paramCount() == 1 ) {
return Math.atan(param(0).getDouble(t));
} else {
missingParams(); return Double.NaN;
}
}
}
//ATAN2(Y,X)
class Atan2Function extends DoubleFunction {
public Atan2Function() { super(2); }
public String getName() { return "ATAN2"; }
public double getDouble(Tuple t) {
if ( paramCount() == 2 ) {
return Math.atan2(param(0).getDouble(t), param(1).getDouble(t));
} else {
missingParams(); return Double.NaN;
}
}
}
//CEILING(X), CEIL(X)
class CeilFunction extends DoubleFunction {
public CeilFunction() { super(1); }
public String getName() { return "CEIL"; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -