/*
 * 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 java.util.Collections;
import java.util.LinkedList;
import org.apfloat.Apfloat;

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

    @Override
    public ExpressionTreeElement getValue() throws IncompatibleNumberFormatException {
        LinkedList<Apfloat> temp = new LinkedList<Apfloat>();
        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_MAX");
            } else {
                NumberType n = (NumberType) number;
                temp.add(n.getNumericValue());
            }
        }
        //csak akkor lehet számolni, ha számot találtunk
        NumberType retValue = new NumberType(this.precision,false);
        Collections.sort(temp);
        retValue.setValue(temp.getLast());
        return retValue;
    }

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

    @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("MAXIMUM");
    }
}
