All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openl.codegen.template.NumberValueChildren-functions.vm Maven / Gradle / Ivy

There is a newer version: 5.19.9
Show newest version
    private $tool.getTypeName($primitiveType) value;

#foreach ($formula in $formulas)
    /**
     * @param value1 $tool.getTypeName($type)
     * @param value2 $tool.getTypeName($type) 
     * @return boolean true or false
     */
    public static $tool.getTypeName($type) ${formula.Name}($tool.getTypeName($type) value1, $tool.getTypeName($type) value2) {
        validate(value1, value2, ${formula.FullName}.toString());

        return new $tool.getTypeName($type)(value1, value2, Operators.${formula.Name}(value1.getValue(), value2.getValue()),
            ${formula.FullName});
    }
#end

#foreach ($logicalExpression in $logicalExpressions)
    /**
     * Compares two values
     * @param value1
     * @param value2
     * @return true if  value1 ${logicalExpression.toString()} value2
     */
    public static boolean ${logicalExpression.Name}($tool.getTypeName($type) value1, $tool.getTypeName($type) value2) {
        validate(value1, value2, ${logicalExpression.FullName}.toString());

        return Operators.${logicalExpression.Name}(value1.getValue(), value2.getValue());
    }
#end

#foreach ($mathFunction in $mathFunctions1)
     /**
     * ${mathFunction.toString()}
     * @param values  array of $tool.getTypeName($type) values
     * @return the ${mathFunction.toString()} value from the array
     */
    public static $tool.getTypeName($type) ${mathFunction.Name}($tool.getTypeName($type)[] values) {
        if (ArrayUtils.isEmpty(values)) {
            return null;
        }
        $tool.getTypeName($primitiveType)[] primitiveArray = unwrap(values);
        $tool.getTypeName($primitiveType) ${mathFunction.Name} = MathUtils.${mathFunction.Name}(primitiveArray);
        return new $tool.getTypeName($type)(new $tool.getTypeName($type)(${mathFunction.Name}), ${mathFunction.FullName}, values);
    }
#end

#foreach ($mathFunction in $mathFunctions2)
     /**
     * Compares value1 and value2 and returns the ${mathFunction.toString()} value
     * @param value1
     * @param value2
     * @return ${mathFunction.toString()} value
     */
    public static $tool.getTypeName($type) ${mathFunction.Name}($tool.getTypeName($type) value1, $tool.getTypeName($type) value2) {
        // Commented to support operations with nulls
        // "null" means that data does not exist
        // validate(value1, value2, ${mathFunction.FullName}.toString());
        if (value1 == null)
            return value2; 
        if (value2 == null)
            return value1; 

        return new $tool.getTypeName($type)(MathUtils.${mathFunction.Name}(value1.getValue(), value2.getValue()) ? value1 : value2,
            ${mathFunction.FullName},
            new $tool.getTypeName($type)[] { value1, value2 });
    }
#end

#foreach ($mathFunction in $mathFunctions3)
    /**
     * 
     * @param values an array $tool.getTypeName($type), must not be null
     * @return $tool.getTypeName($type) the ${mathFunction.toString()} element from array
     */
    public static $tool.getTypeName($type) ${mathFunction.toString()}($tool.getTypeName($type)[] values) {
        $tool.getTypeName($type) result = ($tool.getTypeName($type)) MathUtils.${mathFunction.toString()}(values);

        return new $tool.getTypeName($type)(($tool.getTypeName($type)) getAppropriateValue(values, result),
            ${mathFunction.FullName}, values);
    }
#end
        /**
     * 
     * @param value of variable which should be copied
     * @param name of new variable
     * @return the new $tool.getTypeName($type) variable with name name and value value
     */
    public static $tool.getTypeName($type) ${copyFunction.Name}($tool.getTypeName($type) value, String name) {
        if (value.getName() == null) {
            value.setName(name);

            return value;
        } else if (!value.getName().equals(name)) {
            $tool.getTypeName($type) result = new $tool.getTypeName($type) (value, ${copyFunction.FullName}, 
                new $tool.getTypeName($type)[] { value });
            result.setName(name);

            return result;
        }
        return value;
    }

    //REM
    /**
     * Divides left hand operand by right hand operand and returns remainder
     * @param value1 $tool.getTypeName($type) 
     * @param value2 $tool.getTypeName($type) 
     * @return remainder from division value1 by value2
     */
    public static $tool.getTypeName($type) ${remFormula.Name}($tool.getTypeName($type) value1, $tool.getTypeName($type) value2) {
        // Commented to support operations with nulls. See also MathUtils.mod()
        // validate(value1, value2, ${remFormula.FullName}.toString());
        if (value1 == null || value2 == null) {
            return ZERO;
        }

        return new $tool.getTypeName($type)(value1, value2, Operators.${remFormula.Name}(value1.getValue(), value2.getValue()),
            ${remFormula.FullName});
    }

    //ADD
     /**
     * Adds left hand operand to right hand operand
     * @param value1 $tool.getTypeName($type)
     * @param value2 $tool.getTypeName($type)
     * @return the result of addition operation
     */
    public static $tool.getTypeName($type) ${addFormula.Name}($tool.getTypeName($type) value1, $tool.getTypeName($type) value2) {
        // temporary commented to support operations with nulls
        //
        //        validate(value1, value2, Formulas.ADD.toString());
#if (${primitiveType.isPrimitive()})
        //conditions for classes that are wrappers over primitives
        if (value1 == null) {
            return value2;
        }

        if (value2 == null) {
            return value1;
        }
#else
        //conditions big types
        if (value1 == null) {
            return value2;
        }

        if (value2 == null) {
            return value1;
        }
#end

        return new $tool.getTypeName($type)(value1, value2, Operators.${addFormula.Name}(value1.getValue(), value2.getValue()),
            ${addFormula.FullName});
}

    // MULTIPLY
     /**
     * Multiplies left hand operand to right hand operand
     * @param value1 $tool.getTypeName($type)
     * @param value2 $tool.getTypeName($type)
     * @return the result of multiplication  operation
     */
    public static $tool.getTypeName($type) ${multiplyFormula.Name}($tool.getTypeName($type) value1, $tool.getTypeName($type) value2) {
        // temporary commented to support operations with nulls
        //
        //        validate(value1, value2, Formulas.MULTIPLY.toString());
        if (value1 == null) {
            return value2;
        }

        if (value2 == null) {
            return value1;
        }

        return new $tool.getTypeName($type)(value1, value2, Operators.${multiplyFormula.Name}(value1.getValue(), value2.getValue()),
            ${multiplyFormula.FullName});
    }

    //SUBTRACT
    /**
     * Subtracts left hand operand to right hand operand
     * @param value1 $tool.getTypeName($type)
     * @param value2 $tool.getTypeName($type)
     * @return the result of subtraction  operation
     */
    public static $tool.getTypeName($type) ${subtractFormula.Name}($tool.getTypeName($type) value1, $tool.getTypeName($type) value2) {
        // temporary commented to support operations with nulls
        //
        //        validate(value1, value2, Formulas.SUBTRACT.toString());
        if (value1 == null && value2 == null) {
            return null;
        }

        if (value1 == null) {
            return negative(value2);
        }

        if (value2 == null) {
            return value1;
        }

        return new $tool.getTypeName($type)(value1, value2, Operators.${subtractFormula.Name}(value1.getValue(), value2.getValue()), 
            ${subtractFormula.FullName});
    }

    // DIVIDE
    /**
     * Divides left hand operand by right hand operand
     * @param value1 $tool.getTypeName($type)
     * @param value2 $tool.getTypeName($type)
     * @return the result of division  operation
     */
    public static $tool.getTypeName($type) ${divideFormula.Name}($tool.getTypeName($type) value1, $tool.getTypeName($type) value2) {
        // temporary commented to support operations with nulls
        //
        //        validate(value1, value2, Formulas.DIVIDE.toString());
        if (value1 == null && value2 == null) {
            return null;
        }

        if (value1 == null) {
            if (value2 != null && value2.doubleValue() != 0) {
                return new $tool.getTypeName($type)(value1, value2, ${divideFormula.Name}(ONE, value2).getValue(), ${divideFormula.FullName});
            }
        }

        if (value2 == null) {
            return new $tool.getTypeName($type)(value1, value2, value1.getValue(), ${divideFormula.FullName});
        }

        if (value2.doubleValue() == 0) {
            throw new OpenlNotCheckedException("Division by zero");
        }

        return new $tool.getTypeName($type)(value1, value2, Operators.${divideFormula.Name}(value1.getValue(), value2.getValue()),
            ${divideFormula.FullName});
    }

    // QUAOTIENT
    /**
     * Divides left hand operand by right hand operand
     * @param value1 $tool.getTypeName($type)
     * @param value2 $tool.getTypeName($type)
     * @return LongValue the result of division  operation
     */
    public static LongValue ${quaotientFunction.Name}($tool.getTypeName($type) number, $tool.getTypeName($type) divisor) {
        if (number != null && divisor != null) {
            LongValue result = new LongValue(MathUtils.${quaotientFunction.Name}(number.getValue(), divisor.getValue()));
            return new LongValue(result, NumberOperations.QUOTIENT, null);
        }
        return null;
    }

#if (${primitiveType.isPrimitive()}) 
    // generated product function for types that are wrappers over primitives
     /**
     * Multiplies the numbers from the provided array and returns the product as a number.
     * @param values an array of IntValue which will be converted to DoubleValue
     * @return the product as a number
     */
    public static DoubleValue ${productFunction.Name}($tool.getTypeName($type)[] values) {
        if (ArrayUtils.isEmpty(values)) {
            return null;
        }
        $tool.getTypeName($primitiveType)[] primitiveArray = unwrap(values);
        double ${productFunction.Name} = MathUtils.${productFunction.Name}(primitiveArray);
        // we loose the parameters, but not the result of computation.
        return new DoubleValue(new DoubleValue(${productFunction.Name}), ${productFunction.FullName}, null);
    }
#else 
    // generated product function for big types
     /**
     * Multiplies the numbers from the provided array and returns the product as a number.
     * @param values an array of IntValue which will be converted to DoubleValue
     * @return the product as a number
     */
    public static $tool.getTypeName($type) ${productFunction.Name}($tool.getTypeName($type)[] values) {
        if (ArrayUtils.isEmpty(values)) {
            return null;
        }
        $tool.getTypeName($primitiveType)[] primitiveArray = unwrap(values);
        $tool.getTypeName($primitiveType) ${productFunction.Name} = MathUtils.${productFunction.Name}(primitiveArray);
        // we loose the parameters, but not the result of computation.
        return new $tool.getTypeName($type)(new $tool.getTypeName($type)(${productFunction.Name}), ${productFunction.FullName}, null);
    }
#end
     /**
     *   
     * @param number
     * @param divisor
     * @return the remainder after a number is divided by a divisor. The result is a numeric value and has the same sign as the devisor.
     */
    public static $tool.getTypeName($type) ${modFunction.Name}($tool.getTypeName($type) number, $tool.getTypeName($type) divisor) {
        if (number != null && divisor != null) {
            $tool.getTypeName($type) result = new $tool.getTypeName($type)(MathUtils.mod(number.getValue(), divisor.getValue()));
            return new $tool.getTypeName($type)(result, ${modFunction.FullName}, new $tool.getTypeName($type)[]{number, divisor} );
        }
        return null;
    }

    /**
     * Sorts the array values in ascending order and returns the value from array values at position position
     * @param values array of $tool.getTypeName($type) values 
     * @param position int value
     * @return the value from array values at position position
     */
    public static $tool.getTypeName($type) ${smallFunction.Name}($tool.getTypeName($type)[] values, int position) {
        if (ArrayUtils.isEmpty(values)) {
            return null;
        }
        $tool.getTypeName($primitiveType)[] primitiveArray = unwrap(values);
        $tool.getTypeName($primitiveType) small = MathUtils.${smallFunction.Name}(primitiveArray, position);
        return new $tool.getTypeName($type)(($tool.getTypeName($type)) getAppropriateValue(values, new $tool.getTypeName($type)(small)), 
            ${smallFunction.FullName}, values);
    }

    /**
     * Sorts the array values in descending order and returns the value from array values at position position
     * @param values array of $tool.getTypeName($type) values 
     * @param position int value
     * @return the value from array values at position position
     */
    public static $tool.getTypeName($type) ${bigFunction.Name}($tool.getTypeName($type)[] values, int position) {
        if (ArrayUtils.isEmpty(values)) {
            return null;
        }
        $tool.getTypeName($primitiveType)[] primitiveArray = unwrap(values);
        $tool.getTypeName($primitiveType) big = MathUtils.${bigFunction.Name}(primitiveArray, position);
        return new $tool.getTypeName($type)(($tool.getTypeName($type)) getAppropriateValue(values, new $tool.getTypeName($type)(big)),
            ${bigFunction.FullName}, values);
    }

    /**
     * 
     * @param value1
     * @param value2
     * @return the result of value1 raised to the power of value2
     */
    public static $tool.getTypeName($type) ${powFunction.Name}($tool.getTypeName($type) value1, $tool.getTypeName($type) value2) {
        // Commented to support operations with nulls
        // "null" means that data does not exist
        //
        // validate(value1, value2, ${powFunction.FullName});
        if (value1 == null) {
#if (${primitiveType.isPrimitive()})
            return value2 == null ? null : new $tool.getTypeName($type)(($primitiveType) 0);
#else
            return value2 == null ? null : new $tool.getTypeName($type)("0");
#end
        } else if (value2 == null) {
            return value1;
        }

        return new $tool.getTypeName($type)(new $tool.getTypeName($type)(Operators.${powFunction.Name}(value1.getValue(), value2.getValue())), 
            ${powFunction.FullName}, new $tool.getTypeName($type)[] { value1, value2 });
    }

    /**
     * 
     * @param value
     * @return the absolute value (module) of the value value 
     */
    public static $tool.getTypeName($type) ${absFunction.Name}($tool.getTypeName($type) value) {
        // Commented to support operations with nulls.
        // validate(value, ${absFunction.FullName});
        if (value == null) {
            return null;
        }
        // evaluate result
        $tool.getTypeName($type) result = new $tool.getTypeName($type)(Operators.abs(value.getValue()));
        // create instance with information about last operation
        return new $tool.getTypeName($type)(result, ${absFunction.FullName}, new $tool.getTypeName($type)[] { value });
    }

    /**
     * 
     * @param value
     * @return the negative value of the value
     */
    public static $tool.getTypeName($type) ${negativeFunction.Name}($tool.getTypeName($type) value) {
        if (value == null) {
            return null;
        }
        return multiply(value, MINUS_ONE);
    }

    /**
     * 
     * @param value
     * @return the value increased by 1
     */
    public static $tool.getTypeName($type) ${incFunction.Name}($tool.getTypeName($type) value) {
        return add(value, ONE);
    }

    /**
     * 
     * @param value
     * @return the value
     */
    public static $tool.getTypeName($type) ${positiveFunction.Name}($tool.getTypeName($type) value) {
        return value;
    }

    /**
     * 
     * @param value
     * @return the value  decreased by 1
     */
    public static $tool.getTypeName($type) ${decFunction.Name}($tool.getTypeName($type) value) {
        return subtract(value, ONE);
    }

    // Autocasts

#foreach ($primitiveNumericType in $primitiveNumericTypes)
    /**
     * Is used to overload implicit cast operators from $tool.getTypeName($primitiveNumericType) to $tool.getTypeName($type)
     * @param x
     * @param y is needed to avoid ambiguity in Java method resolution
     * @return the casted value to $tool.getTypeName($type)
     */
    public static $tool.getTypeName($type) autocast($tool.getTypeName($primitiveNumericType) x, $tool.getTypeName($type) y) {
#if (${primitiveType.isPrimitive()})
        return new $tool.getTypeName($type)(($primitiveType) x);
#else
        return new $tool.getTypeName($type)(String.valueOf(x));
#end
    }
#end

    // Constructors
    public ${type.SimpleName}($tool.getTypeName($primitiveType) value) {
        this.value = value;
    }

    public ${type.SimpleName}($tool.getTypeName($primitiveType) value, String name) {
        super(name);
        this.value = value;
    }

    public ${type.SimpleName}($tool.getTypeName($primitiveType) value, IMetaInfo metaInfo) {
        super(metaInfo);
        this.value = value;
    }

    /**Formula constructor**/
    public ${type.SimpleName}($tool.getTypeName($type) lv1, $tool.getTypeName($type) lv2, $tool.getTypeName($primitiveType) value, Formulas operand) {
        super(lv1, lv2, operand);
        this.value = value;
    }

    /**Cast constructor**/
#if (${primitiveType.isPrimitive()})
    public ${type.SimpleName}($tool.getTypeName($primitiveType) value, ExplanationNumberValue beforeCastValue, boolean autocast) {
        super(beforeCastValue, new CastOperand("${type.SimpleName}", autocast));
        this.value = value;
    }
#else
    public ${type.SimpleName}(String valueString, ExplanationNumberValue beforeCastValue, boolean autocast) {
        super(beforeCastValue, new CastOperand("${type.SimpleName}", autocast));
        this.value = new $tool.getTypeName($primitiveType)(valueString);
    }
#end

    /**
    *Copy the current value with new name name
    */
    @Override
    public $tool.getTypeName($type) copy(String name) {
        return copy(this, name);
    }

    /**
    * Prints the value of the current variable
    */
    public String printValue() {
        return String.valueOf(value);
    }

    /**
    * Returns the value of the current variable
    */
    public $tool.getTypeName($primitiveType) getValue() {
        return value;
    }

    /**
    * Sets the value of the current variable
    */
    public void setValue($tool.getTypeName($primitiveType) value) {
        this.value = value;
    }

    //Equals
    @Override
     /**
     * Indicates whether some other object is "equal to" this $tool.getTypeName($type) variable. 
     */
    public boolean equals(Object obj) {
        if (obj instanceof $tool.getTypeName($type)) {
            $tool.getTypeName($type) secondObj = ($tool.getTypeName($type)) obj;
            return Operators.eq(getValue(), secondObj.getValue());
        }

        return false;
    }

    // sort
    /**
    * Sorts the array values
    * @param values an array for sorting
    * @return the sorted array
    */
    public static $tool.getTypeName($type)[] sort ($tool.getTypeName($type)[] values ) {
        $tool.getTypeName($type)[] sortedArray = null;
        if (values != null) {
            sortedArray = new $tool.getTypeName($type)[values.length];
           $tool.getTypeName($type)[] notNullArray = ArrayTool.removeNulls(values);

            Arrays.sort(notNullArray);

            /* Filling sortedArray by sorted and null values */
            for (int i = 0; i < notNullArray.length; i++) {
                sortedArray[i] = notNullArray[i];
            }
        }
        return sortedArray;
    }
        




© 2015 - 2025 Weber Informatics LLC | Privacy Policy