com.codetaco.math.operator.OpNegate Maven / Gradle / Ivy
package com.codetaco.math.operator;
import com.codetaco.math.EquPart;
import com.codetaco.math.Operator;
import com.codetaco.math.ValueStack;
import java.text.ParseException;
/**
*
* OpNegate class.
*
*
* @author Chris DeGreef [email protected]
*/
public class OpNegate extends Operator {
/**
*
* Constructor for OpNegate.
*
*/
public OpNegate() {
super();
}
/**
*
* Constructor for OpNegate.
*
*
* @param opTok a {@link com.codetaco.math.EquPart} object.
*/
public OpNegate(final EquPart opTok) {
super(opTok);
}
/**
* {@inheritDoc}
*/
@Override
protected int precedence() {
return 3;
}
/**
* {@inheritDoc}
*/
@Override
public void resolve(final ValueStack values) throws Exception {
if (values.size() < 1) {
throw new Exception("missing operands for " + toString());
}
try {
final Object value = values.popWhatever();
if (value instanceof Long) {
values.push(new Long(0L - (Long) value));
} else if (value instanceof Double) {
values.push(new Double(0.0 - (Double) value));
} else {
throw new Exception(toString() + "; invalid type: " + value.toString());
}
} catch (final ParseException e) {
e.fillInStackTrace();
throw new Exception(toString() + "; " + e.getMessage(), e);
}
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "op(negate)";
}
}