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

org.op4j.functions.FnBigInteger Maven / Gradle / Ivy

The newest version!
/*
 * =============================================================================
 * 
 *   Copyright (c) 2010, The OP4J team (http://www.op4j.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */
package org.op4j.functions;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.Locale;

import org.apache.commons.lang.Validate;

/**
 * 
 * @since 1.0
 * 
 * @author Soraya Sánchez
 *
 */
@SuppressWarnings({"cast", "unchecked"})
public final class FnBigInteger {

	private static final Function, BigInteger> MAX_FUNC = new Max();
	
	private static final Function, BigInteger> MIN_FUNC = new Min();
	
	private static final Function, BigInteger> SUM_FUNC = new Sum();
	
	private static final Function, BigInteger> AVG_FUNC = new Avg();

    private static final Function MAX_ARRAY_FUNC = new MaxArray();
    
    private static final Function MIN_ARRAY_FUNC = new MinArray();
    
    private static final Function SUM_ARRAY_FUNC = new SumArray();
    
    private static final Function AVG_ARRAY_FUNC = new AvgArray();
	
	private static final Function ABS_FUNC = new Abs();
	
	
	FnBigInteger() {
		super();           
	}

	
	
	
	/**
     * 

* It returns the {@link String} representation of the input number *

* * @return the {@link String} representation of the input */ public static final Function toStr() { return (Function)((Function)FnNumber.toStr()); } /** *

* It returns the {@link String} representation of the input number either * using or not using grouping *

* * @param groupingUsed whether or not grouping will be used * @return the {@link String} representation of the input */ public static final Function toStr(boolean groupingUsed) { return (Function)((Function)FnNumber.toStr(groupingUsed)); } /** *

* It returns the {@link String} representation of the target number in the given {@link Locale} *

* * @param locale the {@link Locale} to be used * @return the {@link String} representation of the input */ public static final Function toStr(Locale locale) { return (Function)((Function)FnNumber.toStr(locale)); } /** *

* It returns the {@link String} representation of the target number in the given locale *

* * @param locale the locale to be used * @return the {@link String} representation of the input */ public static final Function toStr(String locale) { return (Function)((Function)FnNumber.toStr(locale)); } /** *

* It returns the {@link String} representation of the target number in the given {@link Locale}. Grouping * will be used depending on the value of the groupingUsed parameter *

* * @param locale the {@link Locale} to be used * @param groupingUsed whether or not grouping will be used * @return the {@link String} representation of the input */ public static final Function toStr(Locale locale, boolean groupingUsed) { return (Function)((Function)FnNumber.toStr(locale, groupingUsed)); } /** *

* It returns the {@link String} representation of the target number in the given locale. Grouping * will be used depending on the value of the groupingUsed parameter *

* * @param locale the locale to be used * @param groupingUsed whether or not grouping will be used * @return the {@link String} representation of the input */ public static final Function toStr(String locale, boolean groupingUsed) { return (Function)((Function)FnNumber.toStr(locale, groupingUsed)); } /** *

* It returns the {@link String} representation of the target as a currency in the * default {@link Locale} *

* * @return the {@link String} representation of the input as a currency */ public static final Function toCurrencyStr() { return (Function)((Function)FnNumber.toCurrencyStr()); } /** *

* It returns the {@link String} representation of the target as a currency in the * default {@link Locale} *

* * @param groupingUsed whether or not grouping will be used * @return the {@link String} representation of the input as a currency */ public static final Function toCurrencyStr(boolean groupingUsed) { return (Function)((Function)FnNumber.toCurrencyStr(groupingUsed)); } /** *

* It returns the {@link String} representation of the target as a currency in the * given {@link Locale} *

* * @param locale the {@link Locale} to be used * @return the {@link String} representation of the input as a currency */ public static final Function toCurrencyStr(Locale locale) { return (Function)((Function)FnNumber.toCurrencyStr(locale)); } /** *

* It returns the {@link String} representation of the target as a currency in the * given locale *

* * @param locale the locale to be used * @return the {@link String} representation of the input as a currency */ public static final Function toCurrencyStr(String locale) { return (Function)((Function)FnNumber.toCurrencyStr(locale)); } /** *

* It returns the {@link String} representation of the target as a currency in the * given {@link Locale} *

* * @param locale the {@link Locale} to be used * @param groupingUsed whether or not grouping will be used * @return the {@link String} representation of the input as a currency */ public static final Function toCurrencyStr(Locale locale, boolean groupingUsed) { return (Function)((Function)FnNumber.toCurrencyStr(locale, groupingUsed)); } /** *

* It returns the {@link String} representation of the target as a currency in the * given locale *

* * @param locale the locale to be used * @param groupingUsed whether or not grouping will be used * @return the {@link String} representation of the input as a currency */ public static final Function toCurrencyStr(String locale, boolean groupingUsed) { return (Function)((Function)FnNumber.toCurrencyStr(locale, groupingUsed)); } /** *

* A {@link String} representing a percentage is created from the target number. *

* * @return the string representation of the input number as a percentage */ public static final Function toPercentStr() { return (Function)((Function)FnNumber.toPercentStr()); } /** *

* A {@link String} representing a percentage is created from the target number * either using grouping or not depending on the given parameter. So, *

* toPercentStr(true) would return 100,000% if target number is 1000 *
* toPercentStr(false) would return 100000% if target number is 1000 * * @param groupingUsed whether or not grouping will be used * @return the string representation of the input number as a percentage */ public static final Function toPercentStr(boolean groupingUsed) { return (Function)((Function)FnNumber.toPercentStr(groupingUsed)); } /** *

* A {@link String} representing a percentage is created from the target number * in the given {@link Locale} *

* * @param locale the {@link Locale} to be used * @return the string representation of the input number as a percentage */ public static final Function toPercentStr(Locale locale) { return (Function)((Function)FnNumber.toPercentStr(locale)); } /** *

* A {@link String} representing a percentage is created from the target number * in the given locale *

* * @param locale the locale to be used * @return the string representation of the input number as a percentage */ public static final Function toPercentStr(String locale) { return (Function)((Function)FnNumber.toPercentStr(locale)); } /** *

* A {@link String} representing a percentage is created from the target number * in the given {@link Locale}. Grouping will be used depending on the value of the * groupingUsed parameter. *

* * @param locale the {@link Locale} to be used * @param groupingUsed whether or not grouping will be used * @return the string representation of the input number as a percentage */ public static final Function toPercentStr(Locale locale, boolean groupingUsed) { return (Function)((Function)FnNumber.toPercentStr(locale, groupingUsed)); } /** *

* A {@link String} representing a percentage is created from the target number * in the given locale. Grouping will be used depending on the value of the * groupingUsed parameter. *

* * @param locale the locale to be used * @param groupingUsed whether or not grouping will be used * @return the string representation of the input number as a percentage */ public static final Function toPercentStr(String locale, boolean groupingUsed) { return (Function)((Function)FnNumber.toPercentStr(locale, groupingUsed)); } /** *

* Determines whether the target object and the specified object are equal * by calling the equals method on the target object. *

* * @param object the {@link BigInteger} to compare to the target * @return true if both objects are equal, false if not. */ public static final Function eq(final BigInteger object) { return (Function)((Function)FnObject.eq(object)); } /** *

* Determines whether the target object and the specified object are equal * in value, this is, whether target.compareTo(object) == 0. Both * the target and the specified object have to implement {@link Comparable}. *

* * @param object the object to compare to the target * @return true if both objects are equal according to "compareTo", false if not. */ public static final Function eqValue(final Number object) { return (Function)((Function)FnObject.eqValue(object)); } /** *

* Determines whether the target object and the specified object are NOT equal * by calling the equals method on the target object. *

* * @param object the {@link BigInteger} to compare to the target * @return false if both objects are equal, true if not. */ public static final Function notEq(final BigInteger object) { return (Function)((Function)FnObject.notEq(object)); } /** *

* Determines whether the target object and the specified object are NOT equal * in value, this is, whether target.compareTo(object) != 0. Both * the target and the specified object have to implement {@link Comparable}. *

* * @param object the object to compare to the target * @return false if both objects are equal according to "compareTo", true if not. */ public static final Function notEqValue(final Number object) { return (Function)((Function)FnObject.notEqValue(object)); } /** *

* Determines whether the target object is less than the specified object * in value, this is, whether target.compareTo(object) < 0. Both * the target and the specified object have to implement {@link Comparable}. *

* * @param object the object to compare to the target * @return true if target is less than the specified object, false if not */ public static final Function lessThan(final Number object) { return (Function)((Function)FnObject.lessThan(object)); } /** *

* Determines whether the target object is less or equal to the specified object * in value, this is, whether target.compareTo(object) <= 0. Both * the target and the specified object have to implement {@link Comparable}. *

* * @param object the object to compare to the target * @return true if target is less or equal to the specified object, false if not */ public static final Function lessOrEqTo(final Number object) { return (Function)((Function)FnObject.lessOrEqTo(object)); } /** *

* Determines whether the target object is greater than the specified object * in value, this is, whether target.compareTo(object) > 0. Both * the target and the specified object have to implement {@link Comparable}. *

* * @param object the object to compare to the target * @return true if target is greater than the specified object, false if not */ public static final Function greaterThan(final Number object) { return (Function)((Function)FnObject.greaterThan(object)); } /** *

* Determines whether the target object is greater or equal to the specified object * in value, this is, whether target.compareTo(object) >= 0. Both * the target and the specified object have to implement {@link Comparable}. *

* * @param object the object to compare to the target * @return true if target is greater or equal to the specified object, false if not */ public static final Function greaterOrEqTo(final Number object) { return (Function)((Function)FnObject.greaterOrEqTo(object)); } /** *

* Determines whether the result of executing the specified function * on the target object and the specified object parameter are equal * by calling the equals method. *

* * @param object the object to compare to the target * @return true if both objects are equal, false if not. * * @since 1.1 */ public static final Function eqBy(final IFunction by, final Object object) { return (Function)((Function)FnObject.eqBy(by, object)); } /** *

* Determines whether the result of executing the specified function * on the target object and the specified object parameter are equal * in value, this is, whether functionResult.compareTo(object) == 0. * Both the function result and the specified object have to implement * {@link Comparable}. *

* * @param object the object to compare to the target * @return true if both objects are equal according to "compareTo", false if not. * * @since 1.1 */ public static final Function eqValueBy(final IFunction by, final Object object) { return FnObject.eqValueBy(by, object); } /** *

* Determines whether the result of executing the specified function * on the target object and the specified object parameter are NOT equal * by calling the equals method. *

* * @param object the object to compare to the target * @return false if both objects are equal, true if not. * * @since 1.1 */ public static final Function notEqBy(final IFunction by, final Object object) { return (Function)((Function)FnObject.notEqBy(by, object)); } /** *

* Determines whether the result of executing the specified function * on the target object and the specified object parameter are NOT equal * in value, this is, whether functionResult.compareTo(object) != 0. * Both the function result and the specified object have to implement * {@link Comparable}. *

* * @param object the object to compare to the target * @return false if both objects are equal according to "compareTo", true if not. * * @since 1.1 */ public static final Function notEqValueBy(final IFunction by, final Object object) { return FnObject.notEqValueBy(by, object); } /** *

* Determines whether the result of executing the specified function * on the target object is less than the specified object parameter * in value, this is, whether functionResult.compareTo(object) < 0. Both * the target and the specified object have to implement {@link Comparable}. *

* * @param object the object to compare to the target * @return true if function result is less than the specified object, false if not * * @since 1.1 */ public static final Function lessThanBy(final IFunction by, final Object object) { return (Function)((Function)FnObject.lessThanBy(by, object)); } /** *

* Determines whether the result of executing the specified function * on the target object is less or equal to the specified object parameter * in value, this is, whether functionResult.compareTo(object) <= 0. Both * the target and the specified object have to implement {@link Comparable}. *

* * @param object the object to compare to the target * @return true if function result is less or equal to the specified object, false if not * * @since 1.1 */ public static final Function lessOrEqToBy(final IFunction by, final Object object) { return (Function)((Function)FnObject.lessOrEqToBy(by, object)); } /** *

* Determines whether the result of executing the specified function * on the target object is greater than the specified object parameter * in value, this is, whether functionResult.compareTo(object) > 0. Both * the target and the specified object have to implement {@link Comparable}. *

* * @param object the object to compare to the target * @return true if function result is greater than the specified object, false if not * * @since 1.1 */ public static final Function greaterThanBy(final IFunction by, final Object object) { return (Function)((Function)FnObject.greaterThanBy(by, object)); } /** *

* Determines whether the result of executing the specified function * on the target object is greater or equal to the specified object parameter * in value, this is, whether functionResult.compareTo(object) >= 0. Both * the target and the specified object have to implement {@link Comparable}. *

* * @param object the object to compare to the target * @return true if function result is greater or equal to the specified object, false if not * * @since 1.1 */ public static final Function greaterOrEqToBy(final IFunction by, final Object object) { return (Function)((Function)FnObject.greaterOrEqToBy(by, object)); } /** *

* Determines whether the target object is null or not. *

* * @return true if the target object is null, false if not. */ public static final Function isNull() { return (Function)((Function)FnObject.isNull()); } /** *

* Determines whether the target object is null or not. *

* * @return false if the target object is null, true if not. */ public static final Function isNotNull() { return (Function)((Function)FnObject.isNotNull()); } // End From FnNumber /** *

* It returns the maximum number from an {@link Iterable} input object *

* * @return the maximum number */ public final static Function, BigInteger> max() { return MAX_FUNC; } /** *

* It returns the minimum number from an {@link Iterable} input object *

* * @return the minimum number */ public final static Function, BigInteger> min() { return MIN_FUNC; } /** *

* It returns the sum of all the numbers in the {@link Iterable} input object *

* * @return a number equal to the sum of all the elements in the input {@link Iterable} */ public final static Function, BigInteger> sum() { return SUM_FUNC; } /** *

* It returns the average of all the numbers in the {@link Iterable} input object *

* * @return a number representing the average of the input numbers */ public final static Function, BigInteger> avg() { return AVG_FUNC; } /** *

* It returns the average of all the numbers in the {@link Iterable} input object. The * given {@link RoundingMode} will be used to round the output *

* * @param roundingMode the {@link RoundingMode} to round the average * * @return a number representing the average */ public final static Function, BigInteger> avg(RoundingMode roundingMode) { return new Avg(roundingMode); } /** *

* It returns the average of all the numbers in the {@link Iterable} input object. The * given {@link MathContext} will be used to round and set the output precision *

* * @param mathContext the {@link MathContext} to define {@link RoundingMode} and precision * of the average * * @return a number representing the average */ public final static Function, BigInteger> avg(MathContext mathContext) { return new Avg(mathContext); } /** *

* It returns the maximum number from the input array *

* * @return the maximum number */ public final static Function maxArray() { return MAX_ARRAY_FUNC; } /** *

* It returns the minimum number from the input array *

* * @return the minimum number */ public final static Function minArray() { return MIN_ARRAY_FUNC; } /** *

* It returns the sum of all the numbers in the input array *

* * @return a number equal to the sum of all the elements in the input array */ public final static Function sumArray() { return SUM_ARRAY_FUNC; } /** *

* It returns the average of all the numbers in the input array *

* * @return a number representing the average of the input numbers */ public final static Function avgArray() { return AVG_ARRAY_FUNC; } /** *

* It returns the average of all the numbers in the input array. The * given {@link RoundingMode} will be used to round the output *

* * @param roundingMode the {@link RoundingMode} to round the average * * @return a number representing the average */ public final static Function avgArray(RoundingMode roundingMode) { return new AvgArray(roundingMode); } /** *

* It returns the average of all the numbers in the input array. The * given {@link MathContext} will be used to round and set the output precision *

* * @param mathContext the {@link MathContext} to define {@link RoundingMode} and precision * of the average * * @return a number representing the average */ public final static Function avgArray(MathContext mathContext) { return new AvgArray(mathContext); } /** *

* It returns the absolute value of the input *

* * @return the absolute value of the input */ public final static Function abs() { return ABS_FUNC; } /** *

* It adds the given number to the target and returns the result *

* * @param add value to be added to the target * @return the result of the addition of add to the target number */ public final static Function add(Number add) { return new Add(fromNumber(add)); } /** *

* It adds the given number to the target and returns the result *

* * @param add value to be added to the target * @return the result of the addition of add to the target number */ public final static Function add(byte add) { return add(Byte.valueOf(add)); } /** *

* It adds the given number to the target and returns the result *

* * @param add value to be added to the target * @return the result of the addition of add to the target number */ public final static Function add(short add) { return add(Short.valueOf(add)); } /** *

* It adds the given number to the target and returns the result *

* * @param add value to be added to the target * @return the result of the addition of add to the target number */ public final static Function add(int add) { return add(Integer.valueOf(add)); } /** *

* It adds the given number to the target and returns the result *

* * @param add value to be added to the target * @return the result of the addition of add to the target number */ public final static Function add(long add) { return add(Long.valueOf(add)); } /** *

* It adds the given number to the target and returns the result *

* * @param add value to be added to the target * @return the result of the addition of add to the target number */ public final static Function add(float add) { return add(Float.valueOf(add)); } /** *

* It adds the given number to the target and returns the result *

* * @param add value to be added to the target * @return the result of the addition of add to the target number */ public final static Function add(double add) { return add(Double.valueOf(add)); } /** *

* It subtracts the given number from the target and returns the result *

* * @param subtract number to be subtracted from the target * @return the result of the subtraction */ public final static Function subtract(Number subtract) { return new Subtract(fromNumber(subtract)); } /** *

* It subtracts the given number from the target and returns the result *

* * @param subtract number to be subtracted from the target * @return the result of the subtraction */ public final static Function subtract(byte subtract) { return subtract(Byte.valueOf(subtract)); } /** *

* It subtracts the given number from the target and returns the result *

* * @param subtract number to be subtracted from the target * @return the result of the subtraction */ public final static Function subtract(short subtract) { return subtract(Short.valueOf(subtract)); } /** *

* It subtracts the given number from the target and returns the result *

* * @param subtract number to be subtracted from the target * @return the result of the subtraction */ public final static Function subtract(int subtract) { return subtract(Integer.valueOf(subtract)); } /** *

* It subtracts the given number from the target and returns the result *

* * @param subtract number to be subtracted from the target * @return the result of the subtraction */ public final static Function subtract(long subtract) { return subtract(Long.valueOf(subtract)); } /** *

* It subtracts the given number from the target and returns the result *

* * @param subtract number to be subtracted from the target * @return the result of the subtraction */ public final static Function subtract(float subtract) { return subtract(Float.valueOf(subtract)); } /** *

* It subtracts the given number from the target and returns the result *

* * @param subtract number to be subtracted from the target * @return the result of the subtraction */ public final static Function subtract(double subtract) { return subtract(Double.valueOf(subtract)); } /** *

* It divides the target element by the given divisor and returns its result *

* * @param divisor the divisor * @return the result of target/divisor */ public final static Function divideBy(Number divisor) { return new Divide(fromNumber(divisor)); } /** *

* It divides the target element by the given divisor and returns its result *

* * @param divisor the divisor * @return the result of target/divisor */ public final static Function divideBy(byte divisor) { return divideBy(Byte.valueOf(divisor)); } /** *

* It divides the target element by the given divisor and returns its result *

* * @param divisor the divisor * @return the result of target/divisor */ public final static Function divideBy(short divisor) { return divideBy(Short.valueOf(divisor)); } /** *

* It divides the target element by the given divisor and returns its result *

* * @param divisor the divisor * @return the result of target/divisor */ public final static Function divideBy(int divisor) { return divideBy(Integer.valueOf(divisor)); } /** *

* It divides the target element by the given divisor and returns its result *

* * @param divisor the divisor * @return the result of target/divisor */ public final static Function divideBy(long divisor) { return divideBy(Long.valueOf(divisor)); } /** *

* It divides the target element by the given divisor and returns its result *

* * @param divisor the divisor * @return the result of target/divisor */ public final static Function divideBy(float divisor) { return divideBy(Float.valueOf(divisor)); } /** *

* It divides the target element by the given divisor and returns its result *

* * @param divisor the divisor * @return the result of target/divisor */ public final static Function divideBy(double divisor) { return divideBy(Double.valueOf(divisor)); } /** *

* It performs a module operation and returns the value * of (input mod module) which is always positive * (whereas remainder is not) *

* * @param module the module * @return the result of (input mod module) */ public final static Function module(byte module) { return new Module(fromNumber(Byte.valueOf(module))); } /** *

* It performs a module operation and returns the value * of (input mod module) which is always positive * (whereas remainder is not) *

* * @param module the module * @return the result of (input mod module) */ public final static Function module(short module) { return new Module(fromNumber(Short.valueOf(module))); } /** *

* It performs a module operation and returns the value * of (input mod module) which is always positive * (whereas remainder is not) *

* * @param module the module * @return the result of (input mod module) */ public final static Function module(int module) { return new Module(fromNumber(Integer.valueOf(module))); } /** *

* It performs a module operation and returns the value * of (input mod module) which is always positive * (whereas remainder is not) *

* * @param module the module * @return the result of (input mod module) */ public final static Function module(long module) { return new Module(fromNumber(Long.valueOf(module))); } /** *

* It performs a module operation and returns the value * of (input mod module) which is always positive * (whereas remainder is not) *

* * @param module the module * @return the result of (input mod module) */ public final static Function module(Byte module) { return new Module(fromNumber(module)); } /** *

* It performs a module operation and returns the value * of (input mod module) which is always positive * (whereas remainder is not) *

* * @param module the module * @return the result of (input mod module) */ public final static Function module(Short module) { return new Module(fromNumber(module)); } /** *

* It performs a module operation and returns the value * of (input mod module) which is always positive * (whereas remainder is not) *

* * @param module the module * @return the result of (input mod module) */ public final static Function module(Integer module) { return new Module(fromNumber(module)); } /** *

* It performs a module operation and returns the value * of (input mod module) which is always positive * (whereas remainder is not) *

* * @param module the module * @return the result of (input mod module) */ public final static Function module(Long module) { return new Module(fromNumber(module)); } /** *

* It performs a module operation and returns the value * of (input mod module) which is always positive * (whereas remainder is not) *

* * @param module the module * @return the result of (input mod module) */ public final static Function module(BigInteger module) { return new Module(module); } /** *

* It divides the target element by the given divisor and returns the * remainder (target % divisor) *

* * @param divisor the divisor * @return the remainder of target/divisor */ public final static Function remainder(Number divisor) { return new Remainder(fromNumber(divisor)); } /** *

* It divides the target element by the given divisor and returns the * remainder (target % divisor) *

* * @param divisor the divisor * @return the remainder of target/divisor */ public final static Function remainder(byte divisor) { return remainder(Byte.valueOf(divisor)); } /** *

* It divides the target element by the given divisor and returns the * remainder (target % divisor) *

* * @param divisor the divisor * @return the remainder of target/divisor */ public final static Function remainder(short divisor) { return remainder(Short.valueOf(divisor)); } /** *

* It divides the target element by the given divisor and returns the * remainder (target % divisor) *

* * @param divisor the divisor * @return the remainder of target/divisor */ public final static Function remainder(int divisor) { return remainder(Integer.valueOf(divisor)); } /** *

* It divides the target element by the given divisor and returns the * remainder (target % divisor) *

* * @param divisor the divisor * @return the remainder of target/divisor */ public final static Function remainder(long divisor) { return remainder(Long.valueOf(divisor)); } /** *

* It divides the target element by the given divisor and returns the * remainder (target % divisor) *

* * @param divisor the divisor * @return the remainder of target/divisor */ public final static Function remainder(float divisor) { return remainder(Float.valueOf(divisor)); } /** *

* It divides the target element by the given divisor and returns the * remainder (target % divisor) *

* * @param divisor the divisor * @return the remainder of target/divisor */ public final static Function remainder(double divisor) { return remainder(Double.valueOf(divisor)); } /** *

* It multiplies target by multiplicand and returns its value *

* * @param multiplicand the multiplicand * @return the result of target * multiplicand */ public final static Function multiplyBy(Number multiplicand) { return new Multiply(fromNumber(multiplicand)); } /** *

* It multiplies target by multiplicand and returns its value *

* * @param multiplicand the multiplicand * @return the result of target * multiplicand */ public final static Function multiplyBy(byte multiplicand) { return multiplyBy(Byte.valueOf(multiplicand)); } /** *

* It multiplies target by multiplicand and returns its value *

* * @param multiplicand the multiplicand * @return the result of target * multiplicand */ public final static Function multiplyBy(short multiplicand) { return multiplyBy(Short.valueOf(multiplicand)); } /** *

* It multiplies target by multiplicand and returns its value *

* * @param multiplicand the multiplicand * @return the result of target * multiplicand */ public final static Function multiplyBy(int multiplicand) { return multiplyBy(Integer.valueOf(multiplicand)); } /** *

* It multiplies target by multiplicand and returns its value *

* * @param multiplicand the multiplicand * @return the result of target * multiplicand */ public final static Function multiplyBy(long multiplicand) { return multiplyBy(Long.valueOf(multiplicand)); } /** *

* It multiplies target by multiplicand and returns its value *

* * @param multiplicand the multiplicand * @return the result of target * multiplicand */ public final static Function multiplyBy(float multiplicand) { return multiplyBy(Float.valueOf(multiplicand)); } /** *

* It multiplies target by multiplicand and returns its value *

* * @param multiplicand the multiplicand * @return the result of target * multiplicand */ public final static Function multiplyBy(double multiplicand) { return multiplyBy(Double.valueOf(multiplicand)); } /** *

* It performs the operation targetpower and returns its value *

* * @param power the power to raise the target to * @return the result of targetpower */ public final static Function pow(int power) { return new Pow(power); } /** *

* Determines whether the target object is between min and max * in value, this is, whether * target.compareTo(min) >= 0 && target.compareTo(max) <= 0. * The target and the specified min and max have to implement {@link Comparable}. *

* * @param min the minimum value of the target * @param max the maximum value of the target * @return true if the target is between min and max (or it's equal to any of them) */ public static final Function between(final Number min, final Number max) { return (Function)((Function)FnObject.between(min, max)); } private static BigInteger fromNumber(final Number number) { if (number == null) { return null; } if (number instanceof BigDecimal) { return ((BigDecimal) number).toBigInteger(); } if (number instanceof BigInteger) { return (BigInteger)number; } return (BigDecimal.valueOf(number.doubleValue())).toBigInteger(); } static final class Max extends AbstractNotNullFunction,BigInteger> { Max() { super(); } @Override protected BigInteger notNullExecute(final Iterable input, final ExecCtx ctx) throws Exception { if (input.iterator().hasNext() == false) { return null; } BigInteger max = input.iterator().next(); for (BigInteger number : input) { if (number != null) { if (number.compareTo(max) > 0) { max = number; } } } return max; } } static final class Min extends AbstractNotNullFunction,BigInteger> { Min() { super(); } @Override protected BigInteger notNullExecute(final Iterable input, final ExecCtx ctx) throws Exception { if (input.iterator().hasNext() == false) { return null; } BigInteger min = input.iterator().next(); for (BigInteger number : input) { if (number != null) { if (number.compareTo(min) < 0) { min = number; } } } return min; } } static final class Sum extends AbstractNotNullFunction,BigInteger> { Sum() { super(); } @Override protected BigInteger notNullExecute(final Iterable input, final ExecCtx ctx) throws Exception { BigInteger sum = BigInteger.valueOf(0); for (BigInteger number : input) { if (number != null) { sum = sum.add(number); } } return sum; } } static final class Avg extends AbstractNotNullFunction,BigInteger> { private final RoundingMode roundingMode; private final MathContext mathContext; Avg() { super(); this.roundingMode = null; this.mathContext = null; } Avg(RoundingMode roundingMode) { super(); Validate.notNull(roundingMode, "RoundingMode can't be null"); this.roundingMode = roundingMode; this.mathContext = null; } Avg(MathContext mathContext) { super(); Validate.notNull(mathContext, "MathContext can't be null"); this.roundingMode = null; this.mathContext = mathContext; } @Override protected BigInteger notNullExecute(final Iterable input, final ExecCtx ctx) throws Exception { int countNotNull = 0; BigDecimal sum = BigDecimal.valueOf(0); for (BigInteger number : input) { if (number != null) { sum = sum.add(new BigDecimal(number)); countNotNull++; } } if (this.roundingMode != null) { return sum.divide(BigDecimal.valueOf(countNotNull), this.roundingMode).setScale(0,this.roundingMode).toBigInteger(); } if (this.mathContext != null) { return sum.divide(BigDecimal.valueOf(countNotNull), this.mathContext).setScale(0,this.mathContext.getRoundingMode()).toBigInteger(); } return sum.divide(BigDecimal.valueOf(countNotNull), RoundingMode.DOWN).setScale(0, RoundingMode.DOWN).toBigInteger(); } } static final class MaxArray extends AbstractNotNullFunction { MaxArray() { super(); } @Override protected BigInteger notNullExecute(final BigInteger[] input, final ExecCtx ctx) throws Exception { if (input.length == 0) { return null; } BigInteger max = input[0]; for (BigInteger number : input) { if (number != null) { if (number.compareTo(max) > 0) { max = number; } } } return max; } } static final class MinArray extends AbstractNotNullFunction { MinArray() { super(); } @Override protected BigInteger notNullExecute(final BigInteger[] input, final ExecCtx ctx) throws Exception { if (input.length == 0) { return null; } BigInteger min = input[0]; for (BigInteger number : input) { if (number != null) { if (number.compareTo(min) < 0) { min = number; } } } return min; } } static final class SumArray extends AbstractNotNullFunction { SumArray() { super(); } @Override protected BigInteger notNullExecute(final BigInteger[] input, final ExecCtx ctx) throws Exception { BigInteger sum = BigInteger.valueOf(0); for (BigInteger number : input) { if (number != null) { sum = sum.add(number); } } return sum; } } static final class AvgArray extends AbstractNotNullFunction { private final RoundingMode roundingMode; private final MathContext mathContext; AvgArray() { super(); this.roundingMode = null; this.mathContext = null; } AvgArray(RoundingMode roundingMode) { super(); Validate.notNull(roundingMode, "RoundingMode can't be null"); this.roundingMode = roundingMode; this.mathContext = null; } AvgArray(MathContext mathContext) { super(); Validate.notNull(mathContext, "MathContext can't be null"); this.roundingMode = null; this.mathContext = mathContext; } @Override protected BigInteger notNullExecute(final BigInteger[] input, final ExecCtx ctx) throws Exception { int countNotNull = 0; BigDecimal sum = BigDecimal.valueOf(0); for (BigInteger number : input) { if (number != null) { sum = sum.add(new BigDecimal(number)); countNotNull++; } } if (this.roundingMode != null) { return sum.divide(BigDecimal.valueOf(countNotNull), this.roundingMode).setScale(0,this.roundingMode).toBigInteger(); } if (this.mathContext != null) { return sum.divide(BigDecimal.valueOf(countNotNull), this.mathContext).setScale(0,this.mathContext.getRoundingMode()).toBigInteger(); } return sum.divide(BigDecimal.valueOf(countNotNull), RoundingMode.DOWN).setScale(0, RoundingMode.DOWN).toBigInteger(); } } static final class Abs extends AbstractNullAsNullFunction { Abs() { super(); } @Override protected BigInteger nullAsNullExecute(final BigInteger input, final ExecCtx ctx) throws Exception { return input.abs(); } } static final class Add extends AbstractNullAsNullFunction { private final BigInteger add; Add(BigInteger add) { super(); Validate.notNull(add, "Number to be added can't be null"); this.add = add; } @Override protected BigInteger nullAsNullExecute(final BigInteger input, final ExecCtx ctx) throws Exception { BigInteger result = input; result = result.add(this.add); return result; } } static final class Subtract extends AbstractNullAsNullFunction { private final BigInteger subtract; Subtract(BigInteger subtract) { super(); Validate.notNull(subtract, "Number to be subtracted can't be null"); Validate.notNull(subtract, "Number to be added can't be null"); this.subtract = subtract; } @Override protected BigInteger nullAsNullExecute(final BigInteger input, final ExecCtx ctx) throws Exception { BigInteger result = input; result = result.subtract(this.subtract); return result; } } static final class Divide extends AbstractNullAsNullFunction { private final BigInteger divisor; private final RoundingMode roundingMode; private final MathContext mathContext; Divide(BigInteger divisor) { super(); Validate.notNull(divisor, "Divisor can't be null"); this.divisor = divisor; this.roundingMode = null; this.mathContext = null; } Divide(BigInteger divisor, RoundingMode roundingMode) { super(); Validate.notNull(divisor, "Divisor can't be null"); Validate.notNull(roundingMode, "RoundingMode can't be null"); this.divisor = divisor; this.roundingMode = roundingMode; this.mathContext = null; } Divide(BigInteger divisor, MathContext mathContext) { super(); Validate.notNull(divisor, "Divisor can't be null"); Validate.notNull(mathContext, "MathContext can't be null"); this.divisor = divisor; this.roundingMode = null; this.mathContext = mathContext; } @Override protected BigInteger nullAsNullExecute(final BigInteger input, final ExecCtx ctx) throws Exception { BigDecimal bigInput = new BigDecimal(input); if (this.roundingMode != null) { return bigInput.divide(new BigDecimal(this.divisor), this.roundingMode).setScale(0, this.roundingMode).toBigInteger(); } else if (this.mathContext != null) { return bigInput.divide(new BigDecimal(this.divisor), this.mathContext).setScale(0, this.mathContext.getRoundingMode()).toBigInteger(); } else { return bigInput.divide(new BigDecimal(this.divisor), RoundingMode.DOWN).setScale(0, RoundingMode.DOWN).toBigInteger(); } } } static final class Module extends AbstractNullAsNullFunction { private final BigInteger module; Module(BigInteger module) { super(); this.module = module; } @Override protected BigInteger nullAsNullExecute(final BigInteger input, final ExecCtx ctx) throws Exception { return input.mod(this.module); } } static final class Remainder extends AbstractNullAsNullFunction { private final BigInteger divisor; Remainder(BigInteger divisor) { super(); this.divisor = divisor; } @Override protected BigInteger nullAsNullExecute(final BigInteger input, final ExecCtx ctx) throws Exception { return input.remainder(this.divisor); } } static final class Multiply extends AbstractNullAsNullFunction { private final BigInteger multiplicand; Multiply(BigInteger multiplicand) { super(); Validate.notNull(multiplicand, "Multiplicand can't be null"); this.multiplicand = multiplicand; } @Override protected BigInteger nullAsNullExecute(final BigInteger input, final ExecCtx ctx) throws Exception { BigInteger result = input; result = result.multiply(this.multiplicand); return result; } } static final class Pow extends AbstractNullAsNullFunction { private final int power; Pow(int power) { super(); this.power = power; } @Override protected BigInteger nullAsNullExecute(final BigInteger input, final ExecCtx ctx) throws Exception { BigInteger result = input; result = result.pow(this.power); return result; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy