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

functionalj.lens.lenses.NumberAccess Maven / Gradle / Ivy

// ============================================================================
// Copyright (c) 2017-2019 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.lens.lenses;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

import functionalj.function.Func;
import functionalj.lens.core.AccessCreator;
import functionalj.result.Result;
import functionalj.tuple.Tuple2;
import lombok.val;

@SuppressWarnings("javadoc")
public interface NumberAccess, NUMACCESS extends NumberAccess> 
        extends ComparableAccess {
    
    public default IntegerAccess compareTo(TYPE anotherValue) {
        return intAccess(Integer.MIN_VALUE, any -> any.compareTo(anotherValue));
    }
    public default BooleanAccess thatGreaterThan(TYPE anotherValue) {
        return booleanAccess(false, any -> any.compareTo(anotherValue) > 0);
    }
    public default BooleanAccess thatLessThan(TYPE anotherValue) {
        return booleanAccess(false, any -> any.compareTo(anotherValue) < 0);
    }
    public default BooleanAccess thatGreaterThanOrEqualsTo(TYPE anotherValue) {
        return booleanAccess(false, any -> any.compareTo(anotherValue) >= 0);
    }
    public default BooleanAccess thatLessThanOrEqualsTo(TYPE anotherValue) {
        return booleanAccess(false, any -> any.compareTo(anotherValue) <= 0);
    }
    public default BooleanAccess thatIsZero() {
        return (BooleanAccess)(host -> {
            val value = apply(host);
            val zero  = toZero().apply(host);
            return value.compareTo(zero) == 0;
        });
    }
    public default BooleanAccess thatIsNotZero() {
        return (BooleanAccess)(host -> {
            val value = apply(host);
            val zero  = toZero().apply(host);
            return value.compareTo(zero) != 0;
        });
    }
    public default BooleanAccess thatIsPositive() {
        return (BooleanAccess)(host -> {
            val value = apply(host);
            val zero  = toZero().apply(host);
            return value.compareTo(zero) > 0;
        });
    }
    public default BooleanAccess thatIsNegative() {
        return (BooleanAccess)(host -> {
            val value = apply(host);
            val zero  = toZero().apply(host);
            return value.compareTo(zero) < 0;
        });
    }
    public default BooleanAccess thatIsNotPositive() {
        return (BooleanAccess)(host -> {
            val value = apply(host);
            val zero  = toZero().apply(host);
            return value.compareTo(zero) <= 0;
        });
    }
    public default BooleanAccess thatIsNotNegative() {
        return (BooleanAccess)(host -> {
            val value = apply(host);
            val zero  = toZero().apply(host);
            return value.compareTo(zero) >= 0;
        });
    }
    
    public NUMACCESS newAccess(Function accessToValue);
    
    public MathOperators __mathOperators();
    
    
    public static interface MathOperators {
        
        public NUMBER zero();
        public NUMBER one();
        public NUMBER minusOne();
        
        public Integer    toInteger(NUMBER number);
        public Long       toLong(NUMBER number);
        public Double     toDouble(NUMBER number);
        public BigInteger toBigInteger(NUMBER number);
        public BigDecimal toBigDecimal(NUMBER number);
        
        public NUMBER add(NUMBER number1, NUMBER number2);
        public NUMBER subtract(NUMBER number1, NUMBER number2);
        public NUMBER multiply(NUMBER number1, NUMBER number2);
        public NUMBER divide(NUMBER number1, NUMBER number2);
        public NUMBER remainder(NUMBER number1, NUMBER number2);
        public Tuple2 divideAndRemainder(NUMBER number, NUMBER divisor);
        
        public NUMBER pow(NUMBER number, NUMBER n);
        public NUMBER abs(NUMBER number);
        public NUMBER negate(NUMBER number);
        public NUMBER signum(NUMBER number);
        public NUMBER min(NUMBER number1, NUMBER number2);
        public NUMBER max(NUMBER number1, NUMBER number2);
        
    }
    
    public default IntegerAccess toInteger() {
        return intAccess(0, __mathOperators()::toInteger);
    }
    public default LongAccess toLong() {
        return longAccess(0L, __mathOperators()::toLong);
    }
    public default DoubleAccess toDouble() {
        return doubleAccess(0.0, __mathOperators()::toDouble);
    }
    public default BigIntegerAccess toBigInteger() {
        return bigIntegerAccess(BigInteger.ZERO, __mathOperators()::toBigInteger);
    }
    public default BigDecimalAccess toBigDecimal() {
        return bigDecimalAccess(BigDecimal.ZERO, __mathOperators()::toBigDecimal);
    }
    
    public default ResultAccess> asInteger() {
        return ResultAccess.of(host -> {
            val value = apply(host);
            return Result.from(()->__mathOperators().toInteger(value));
        }, func -> (IntegerAccess)(func::apply));
    }
    public default ResultAccess> asLong() {
        return ResultAccess.of(host -> {
            val value = apply(host);
            return Result.from(()->__mathOperators().toLong(value));
        }, func -> (LongAccess)(func::apply));
    }
    public default ResultAccess> asDouble() {
        return ResultAccess.of(host -> {
            val value = apply(host);
            return Result.from(()->__mathOperators().toDouble(value));
        }, func -> (DoubleAccess)(func::apply));
    }
    public default ResultAccess> asBigInteger() {
        return ResultAccess.of(host -> {
            val value = apply(host);
            return Result.from(()->__mathOperators().toBigInteger(value));
        }, func -> (BigIntegerAccess)(func::apply));
    }
    public default ResultAccess> asBigDecimal() {
        return ResultAccess.of(host -> {
            val value = apply(host);
            return Result.from(()->__mathOperators().toBigDecimal(value));
        }, func -> (BigDecimalAccess)(func::apply));
    }
    
    // TODO -- These __operate should be moved to a util class.
    
    public default NUMACCESS __operate(
            AccessCreator accessCreator,  
            Supplier                       operator) {
        return accessCreator.newAccess(host -> {
            return operator.get();
        });
    }
    public default NUMACCESS __operate(
            TYPE                                 value, 
            NUMACCESS                            orgAccess,
            AccessCreator accessCreator,  
            BiFunction         operator) {
        return accessCreator.newAccess(host -> {
            if (host == null)
                return value;
            val v = orgAccess.apply(host); 
            return operator.apply(v, value);
        });
    }
    public default NUMACCESS __operate(
            TYPE                                 fallbackValue,
            Supplier                       valueSupplier, 
            NUMACCESS                            orgAccess,
            AccessCreator accessCreator,  
            BiFunction         operator) {
        return accessCreator.newAccess(host -> {
            if (host == null)
                return Func.getOrElse(valueSupplier, fallbackValue);
            
            val v     = orgAccess.apply(host); 
            val value = Func.getOrElse(valueSupplier, fallbackValue);
            return operator.apply(v, value);
        });
    }
    public default NUMACCESS __operate(
            TYPE                                 fallbackValue,
            Function                 valueFunction, 
            NUMACCESS                            orgAccess,
            AccessCreator accessCreator,  
            BiFunction         operator) {
        return accessCreator.newAccess(host -> {
            if (host == null)
                return Func.applyOrElse(valueFunction, fallbackValue, fallbackValue);
            
            val v     = orgAccess.apply(host); 
            val value = Func.applyOrElse(valueFunction, v, fallbackValue);
            return operator.apply(v, value);
        });
    }
    public default NUMACCESS __operate(
            TYPE                                 fallbackValue,
            BiFunction         valueFunction, 
            NUMACCESS                            orgAccess,
            AccessCreator accessCreator,  
            BiFunction         operator) {
        return accessCreator.newAccess(host -> {
            if (host == null)
                return Func.applyOrElse(valueFunction, host, fallbackValue, fallbackValue);
            
            val v     = orgAccess.apply(host); 
            val value = Func.applyOrElse(valueFunction, host, v, fallbackValue);
            return operator.apply(v, value);
        });
    }
    
    public default NUMACCESS toZero() {
        return __operate(this::newAccess, __mathOperators()::zero);
    }
    public default NUMACCESS toOne() {
        return __operate(this::newAccess, __mathOperators()::one);
    }
    public default NUMACCESS toMinusOne() {
        return __operate(this::newAccess, __mathOperators()::minusOne);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS add(TYPE value) {
        return __operate(value, (NUMACCESS)this, this::newAccess, __mathOperators()::add);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS add(Supplier valueSupplier) {
        return __operate(__mathOperators().zero(), valueSupplier, (NUMACCESS)this, this::newAccess, __mathOperators()::add);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS add(Function valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::add);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS add(BiFunction valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::add);
    }
    
    
    @SuppressWarnings("unchecked")
    public default NUMACCESS subtract(TYPE value) {
        return __operate(value, (NUMACCESS)this, this::newAccess, __mathOperators()::subtract);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS subtract(Supplier valueSupplier) {
        return __operate(__mathOperators().zero(), valueSupplier, (NUMACCESS)this, this::newAccess, __mathOperators()::subtract);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS subtract(Function valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::subtract);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS subtract(BiFunction valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::subtract);
    }
    
    
    @SuppressWarnings("unchecked")
    public default NUMACCESS multiply(TYPE value) {
        return __operate(value, (NUMACCESS)this, this::newAccess, __mathOperators()::multiply);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS multiply(Supplier valueSupplier) {
        return __operate(__mathOperators().zero(), valueSupplier, (NUMACCESS)this, this::newAccess, __mathOperators()::multiply);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS multiply(Function valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::multiply);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS multiply(BiFunction valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::multiply);
    }
    
    
    @SuppressWarnings("unchecked")
    public default NUMACCESS divide(TYPE value) {
        return __operate(value, (NUMACCESS)this, this::newAccess, __mathOperators()::divide);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS divide(Supplier valueSupplier) {
        return __operate(__mathOperators().zero(), valueSupplier, (NUMACCESS)this, this::newAccess, __mathOperators()::divide);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS divide(Function valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::divide);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS divide(BiFunction valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::divide);
    }
    
    @SuppressWarnings("unchecked")
    public default NUMACCESS remainder(TYPE value) {
        return __operate(value, (NUMACCESS)this, this::newAccess, __mathOperators()::remainder);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS remainder(Supplier valueSupplier) {
        return __operate(__mathOperators().zero(), valueSupplier, (NUMACCESS)this, this::newAccess, __mathOperators()::remainder);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS remainder(Function valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::remainder);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS remainder(BiFunction valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::remainder);
    }
    
    @SuppressWarnings("unchecked")
    public default NUMACCESS mod(TYPE value) {
        return __operate(value, (NUMACCESS)this, this::newAccess, __mathOperators()::remainder);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS mod(Supplier valueSupplier) {
        return __operate(__mathOperators().zero(), valueSupplier, (NUMACCESS)this, this::newAccess, __mathOperators()::remainder);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS mod(Function valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::remainder);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS mod(BiFunction valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::remainder);
    }
    
    
//  public BigDecimal[] divideAndRemainder(BigDecimal divisor); --  Tuple
    
    @SuppressWarnings("unchecked")
    public default NUMACCESS pow(TYPE value) {
        return __operate(value, (NUMACCESS)this, this::newAccess, __mathOperators()::pow);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS pow(Supplier valueSupplier) {
        return __operate(__mathOperators().zero(), valueSupplier, (NUMACCESS)this, this::newAccess, __mathOperators()::pow);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS pow(Function valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::pow);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS pow(BiFunction valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::pow);
    }
    
    public default NUMACCESS abs() {
        return newAccess(host -> {
            if (host == null)
                return __mathOperators().zero();
            val v = apply(host); 
            return __mathOperators().abs(v);
        });
    }
    public default NUMACCESS negate() {
        return newAccess(host -> {
            if (host == null)
                return __mathOperators().zero();
            val v = apply(host); 
            return __mathOperators().negate(v);
        });
    }
    public default NUMACCESS signum() {
        return newAccess(host -> {
            if (host == null)
                return __mathOperators().zero();
            val v = apply(host); 
            return __mathOperators().signum(v);
        });
    }
    
    @SuppressWarnings("unchecked")
    public default NUMACCESS min(TYPE value) {
        return __operate(value, (NUMACCESS)this, this::newAccess, __mathOperators()::min);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS min(Supplier valueSupplier) {
        return __operate(__mathOperators().zero(), valueSupplier, (NUMACCESS)this, this::newAccess, __mathOperators()::min);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS min(Function valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::min);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS min(BiFunction valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::min);
    }
    
    @SuppressWarnings("unchecked")
    public default NUMACCESS max(TYPE value) {
        return __operate(value, (NUMACCESS)this, this::newAccess, __mathOperators()::max);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS max(Supplier valueSupplier) {
        return __operate(__mathOperators().zero(), valueSupplier, (NUMACCESS)this, this::newAccess, __mathOperators()::max);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS max(Function valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::max);
    }
    @SuppressWarnings("unchecked")
    public default NUMACCESS max(BiFunction valueFunction) {
        return __operate(__mathOperators().zero(), valueFunction, (NUMACCESS)this, this::newAccess, __mathOperators()::max);
    }
        
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy