/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package expressiontree.operators.unary;

import expressiontree.ExpressionTreeElement;
import expressiontree.operators.UnaryOperator;
import expressiontree.operands.NumberType;
import expressiontree.operators.IncompatibleNumberFormatException;
import org.apfloat.ApintMath;

/**
 * A faktoriális operátorok osztálya.
 * @author Esta
 */
public class Factorial extends UnaryOperator {

    @Override
    public ExpressionTreeElement getValue() throws IncompatibleNumberFormatException {
        ExpressionTreeElement op1 = this.operands.get(0).getValue();
        if (op1 instanceof NumberType &&
            ((NumberType) op1).isLongValue()) {
            //csak akkor lehet számolni, ha hosszú egész számot találtunk
            NumberType n = (NumberType) op1;
            if (n.getNumericValue().longValue() < 0) {
                throw new IncompatibleNumberFormatException("ERROR_FACT");
            }
            NumberType retValue = new NumberType(this.precision,false);
            retValue.setValue(ApintMath.factorial(n.getNumericValue().longValue()).precision(this.precision));
            return retValue;
        } else { //mátrix, vagy nem egész szám >> exception
            throw new IncompatibleNumberFormatException("ERROR_FACT");
        }
    }

    @Override
    public String toText(boolean b) {
        if (b) {
            return "!()";
        } else {
            return "! ";
        }
    }

    @Override
    public String getLocalizedName() {
        return java.util.ResourceBundle.getBundle("languages/operators").getString("FACTORIAL");
    }
}
