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

import expressiontree.ExpressionTreeElement;
import expressiontree.operators.IncompatibleNumberFormatException;
import expressiontree.operators.Operator;
import expressiontree.operands.NumberType;
import org.apfloat.Apfloat;
import org.apfloat.ApfloatMath;

/**
 * A produktum operátor osztálya
 * @author Esta
 */
public class Product extends Operator {

    @Override
    public ExpressionTreeElement getValue() throws IncompatibleNumberFormatException {
        Apfloat[] temp = new Apfloat[this.operands.size()];
        for (int i = 0; i < this.operands.size(); i++) {
            ExpressionTreeElement number = this.operands.get(i).getValue();
            if (!(number instanceof NumberType)) {
                //mátrix >> exception
                throw new IncompatibleNumberFormatException("ERROR_PROD");
            } else {
                NumberType n = (NumberType) number;
                temp[i] = n.getNumericValue();
            }
        }
        //csak akkor lehet számolni, ha számot találtunk
        NumberType retValue = new NumberType(this.precision, false);
        retValue.setValue(ApfloatMath.product(temp));
        return retValue;
    }

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

    @Override
    public int operandOffset(boolean b) {
        if (b) return this.toText(true).indexOf("(")+1;
        else return this.toText(false).length()-1;
    }

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