FileDocCategorySizeDatePackage
BinaryOpValueExp.javaAPI DocJava SE 5 API3875Fri Aug 26 14:57:32 BST 2005javax.management

BinaryOpValueExp

public class BinaryOpValueExp extends QueryEval implements ValueExp
This class is used by the query-building mechanism to represent binary operations.
serial
include
since
1.5

Fields Summary
private static final long
serialVersionUID
private int
op
private ValueExp
exp1
private ValueExp
exp2
Constructors Summary
public BinaryOpValueExp()
Basic Constructor.



           
       
    
public BinaryOpValueExp(int o, ValueExp v1, ValueExp v2)
Creates a new BinaryOpValueExp using operator o applied on v1 and v2 values.

 
	op   = o;
	exp1 = v1;
	exp2 = v2;
    
Methods Summary
public javax.management.ValueExpapply(javax.management.ObjectName name)
Applies the BinaryOpValueExp on a MBean.

param
name The name of the MBean on which the BinaryOpValueExp will be applied.
return
The ValueExp.
exception
BadStringOperationException
exception
BadBinaryOpValueExpException
exception
BadAttributeValueExpException
exception
InvalidApplicationException

 
	ValueExp val1 = exp1.apply(name);
	ValueExp val2 = exp2.apply(name);
	String sval1;
	String sval2;
	double dval1;
	double dval2;
	long   lval1;
	long   lval2;
	boolean numeric = val1 instanceof NumericValueExp;
	
	if (numeric) {
	    if (((NumericValueExp)val1).isLong()) {
		lval1 = ((NumericValueExp)val1).longValue();
		lval2 = ((NumericValueExp)val2).longValue();

		switch (op) {
		case Query.PLUS:
		    return Query.value(lval1 + lval2);
		case Query.TIMES:
		    return Query.value(lval1 * lval2);
		case Query.MINUS:
		    return Query.value(lval1 - lval2);
		case Query.DIV:
		    return Query.value(lval1 / lval2);
		}
		
	    } else {
		dval1 = ((NumericValueExp)val1).doubleValue();
		dval2 = ((NumericValueExp)val2).doubleValue();

		switch (op) {
		case Query.PLUS:
		    return Query.value(dval1 + dval2);
		case Query.TIMES:
		    return Query.value(dval1 * dval2);
		case Query.MINUS:
		    return Query.value(dval1 - dval2);
		case Query.DIV:
		    return Query.value(dval1 / dval2);
		}
	    }
	} else {
	    sval1 = ((StringValueExp)val1).getValue();
	    sval2 = ((StringValueExp)val2).getValue();

	    switch (op) {
	    case Query.PLUS:
		return new StringValueExp(sval1 + sval2);
	    default:
		throw new BadStringOperationException(opString());
	    }
	}
	
	throw new BadBinaryOpValueExpException(this);
    
public javax.management.ValueExpgetLeftValue()
Returns the left value of the value expression.

 
	return exp1;
    
public intgetOperator()
Returns the operator of the value expression.

 
	return op;
    
public javax.management.ValueExpgetRightValue()
Returns the right value of the value expression.

 
	return exp2;
    
private java.lang.StringopString()

	switch (op) {
	case Query.PLUS:
	    return "+";
	case Query.TIMES:
	    return "*";
	case Query.MINUS:
	    return "-";
	case Query.DIV:
	    return "/";
	}
	
	throw new BadBinaryOpValueExpException(this);
    
public java.lang.StringtoString()
Returns the string representing the object

 
	try {
	    return exp1 + " " + opString() + " " + exp2;
	} catch (BadBinaryOpValueExpException ex) {
	    return "invalid expression";
	}