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

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

/**
 * A legnagyobb közös osztó operátorok osztálya.
 * @author Esta
 */
public class GreatestCommonDivisor extends BinaryOperator {

    @Override
    public ExpressionTreeElement getValue() throws IncompatibleNumberFormatException {
        ExpressionTreeElement op1 = this.operands.get(0).getValue();
        ExpressionTreeElement op2 = this.operands.get(1).getValue();
        if (op1 instanceof NumberType && op2 instanceof NumberType) {
            //csak akkor lehet számolni, ha számot találtunk
            NumberType a = (NumberType) op1;
            NumberType b = (NumberType) op2;
            if (!a.isIntegerValue() || !b.isIntegerValue()) {
                //ha nem egészeket kaptunk, akkor nem számolunk semmit.
                throw new ArithmeticException("ERROR_GCD");
            }
            NumberType retValue = new NumberType(this.precision,false);
            retValue.setValue(ApintMath.gcd(a.getNumericValue().truncate(), b.getNumericValue().truncate()).precision(this.precision));
            return retValue;
        } else { //mátrix >> exception
            throw new IncompatibleNumberFormatException("ERROR_GCD");
        }
    }

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

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