Package reportgen.math.reference.function.values

Source Code of reportgen.math.reference.function.values.MathFuncIf

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package reportgen.math.reference.function.values;

import reportgen.prototype.context.Context;
import reportgen.prototype.context.group.ContextGroup;
import reportgen.utils.ReportException;
import java.util.List;
import java.util.Map;
import reportgen.prototype.context.ContextCompound;
import reportgen.math.MathExpressionOperand;
import reportgen.math.complex.conditions.MathExpressionConditions;

/**
* математический элемент функция.
* Соджержит два математических элемента.
  * Все элементы в выражении должны быть операндами
* @author axe
*/

public class MathFuncIf extends MathFunctionType {

    private static final String TYPE = "IF";
    private static final String TITLE = "Если";

    MathFuncIf() {
        super(TYPE, TITLE, 3, 3);
    }


    @Override
    protected void checkArgumentsTypes(List<Class> operands) throws ReportException {
        if(!operands.get(0).equals(Boolean.class)) {
            throw new ReportException("В функции " + getTitle()
                    + " первый аргумент должен быть булевого типа");
        }

        if(!operands.get(1).equals(operands.get(2))) {
            throw new ReportException("В функции " + getTitle()
                    + " второй и третий аргументы должны быть одного типа");
        }
    }


    @Override
    protected Class getResultProbeClass(List<Class> arguments) throws ReportException {
        return arguments.get(1);
    }


    @Override
    public Object getValue(List<MathExpressionOperand> args, Map constants)
            throws ReportException {
        checkArgumentsCount(args.size());

        //condition
        MathExpressionOperand operand = args.get(0);
        Object conditionValue = operand.getValue(constants);
        if(!(conditionValue instanceof Boolean)) {
            throw new ReportException("Первый аргумент функции 'ЕСЛИ' должен быть булевого типа");
        }
       
        Boolean condition = (conditionValue == null) ? Boolean.FALSE : (Boolean) conditionValue;
        if(condition) {
            operand = args.get(1);
        } else {
            operand = args.get(2);
        }
        return operand.getValue(constants);
    }


    @Override
    public Context getContext(final Context context) {
        return new ContextCompound(context) {
            @Override
            public List<ContextGroup> getAvailiableGroups() {
                List<ContextGroup> res = context.getAvailiableGroups();
                res.add(MathExpressionConditions.GROUP);
                return res;
            }
        };
    }

    @Override
    public boolean canChangeType() {
        return false;
    }

    @Override
    public boolean supportsSQL() {
        return false;
    }

}
TOP

Related Classes of reportgen.math.reference.function.values.MathFuncIf

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.