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

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

There is a newer version: 1.0.17
Show newest version
// ============================================================================
// Copyright (c) 2017-2021 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 static functionalj.function.Apply.access;
import static functionalj.function.Apply.applyPrimitive;
import static functionalj.function.Apply.getPrimitive;
import static functionalj.function.Compare.compareOrNull;
import static java.util.Objects.requireNonNull;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Comparator;
import java.util.function.Function;
import java.util.function.IntPredicate;
import java.util.function.IntSupplier;
import java.util.function.IntUnaryOperator;
import java.util.function.ToIntFunction;

import functionalj.function.Func1;
import functionalj.functions.IntFuncs;
import functionalj.list.intlist.IntFuncList;
import functionalj.ref.Ref;
import lombok.val;


/**
 * Classes implementing this interface know how to access to an integer value.
 **/
public interface IntegerAccess 
                    extends 
                        NumberAccess>,
                        ToIntFunction,
                        ConcreteAccess> {
    
    /** The reference to a function to calculate factorial for integer. **/
    public static final Ref factorialRef = Ref.ofValue(value -> IntFuncs.factorial(value));
    
    public static  IntegerAccess of(Function accessToValue) {
        requireNonNull(accessToValue);
        
        if (accessToValue instanceof IntegerAccess) {
            return (IntegerAccess)accessToValue;
        }
        
        if (accessToValue instanceof ToIntFunction) {
            @SuppressWarnings("unchecked")
            val func1  = (ToIntFunction)accessToValue;
            val access = ofPrimitive(func1);
            return access;
        }
        
        if (accessToValue instanceof Func1) {
            val func1  = (Func1)accessToValue;
            val access = (IntegerAccessBoxed)func1::applyUnsafe;
            return access;
        }
        
        val func   = (Function)accessToValue;
        val access = (IntegerAccessBoxed)(host -> func.apply(host));
        return access;
    }
    
    public static  IntegerAccessPrimitive ofPrimitive(ToIntFunction accessToValue) {
        requireNonNull(accessToValue);
        val access = (IntegerAccessPrimitive)accessToValue::applyAsInt;
        return access;
    }
    
    @Override
    public default IntegerAccess newAccess(Function accessToValue) {
        return of(accessToValue);
    }
    
    //== abstract functionalities ==
    
    public int applyAsInt(HOST host);
    
    
    public Integer applyUnsafe(HOST host) throws Exception;
    
    
    //-- conversion --
    
    public default IntegerAccessBoxed boxed() {
        return host -> apply(host);
    }
    
    @Override
    public default IntegerAccessPrimitive asInteger() {
        return host -> access(this, host);
    }
    
    @Override
    public default LongAccessPrimitive asLong() {
        return host -> access(this, host);
    }
    
    @Override
    public default DoubleAccessPrimitive asDouble() {
        return host -> access(this, host);
    }
    
    @Override
    public default StringAccess asString() {
        return host -> "" + access(this, host);
    }
    @Override
    public default StringAccess asString(String template) {
        return host -> {
            val value = access(this, host);
            return String.format(template, value);
        };
    }
    public default BigIntegerAccess asBitInteger() {
        return host -> {
            val value = access(this, host);
            return BigInteger.valueOf(value);
        };
    }
    public default BigDecimalAccess asBitDecimal() {
        return host -> {
            val value = access(this, host);
            return BigDecimal.valueOf(value);
        };
    }
    
    //-- Equality --
    
    public default BooleanAccessPrimitive that(IntPredicate checker) {
        return host -> {
            val value = access(this, host);
            return checker.test(value);
        };
    }
    
    public default BooleanAccessPrimitive thatIs(int anotherValue) {
        return host -> {
            val value = access(this, host);
            return value == anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatIs(IntSupplier anotherSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(anotherSupplier);
            return value == anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatIs(ToIntFunction anotherAccess) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherAccess, host);
            return value == anotherValue;
        };
    }
    
    public default BooleanAccessPrimitive thatIsNot(int anotherValue) {
        return host -> {
            val value = access(this, host);
            return value != anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatIsNot(IntSupplier anotherSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(anotherSupplier);
            return value != anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatIsNot(ToIntFunction anotherAccess) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherAccess, host);
            return value != anotherValue;
        };
    }
    
    public default BooleanAccessPrimitive thatIsAnyOf(int ... otherValues) {
        return host -> {
            val value = access(this, host);
            for (val anotherValue : otherValues) {
                if (value == anotherValue) {
                    return true;
                }
            }
            return false;
        };
    }
    public default BooleanAccessPrimitive thatIsAnyOf(IntFuncList otherValues) {
        return host -> {
            val value = access(this, host);
            return otherValues.anyMatch(anotherValue -> value == anotherValue);
        };
    }
    
    public default BooleanAccessPrimitive thatIsNoneOf(int ... otherValues) {
        return host -> {
            val value = access(this, host);
            for (val anotherValue : otherValues) {
                if (value == anotherValue) {
                    return false;
                }
            }
            return true;
        };
    }
    public default BooleanAccessPrimitive thatIsNoneOf(IntFuncList otherValues) {
        return host -> {
            val value = access(this, host);
            return otherValues.noneMatch(anotherValue -> value == anotherValue);
        };
    }
    
    public default BooleanAccessPrimitive thatIsOne() {
        return thatIs(1);
    }
    
    public default BooleanAccessPrimitive thatIsZero() {
        return thatIs(0);
    }
    
    public default BooleanAccessPrimitive thatIsMinusOne() {
        return thatIs(-1);
    }
    
    public default BooleanAccessPrimitive thatIsFourtyTwo() {
        return thatIs(42);
    }
    
    public default BooleanAccessPrimitive thatIsNotOne() {
        return thatIsNot(1);
    }
    
    public default BooleanAccessPrimitive thatIsNotZero() {
        return thatIsNot(0);
    }
    
    public default BooleanAccessPrimitive thatIsNotMinusOne() {
        return thatIsNot(-1);
    }
    
    public default BooleanAccessPrimitive thatIsPositive() {
        return host -> {
            val value = access(this, host);
            return value > 0;
        };
    }
    public default BooleanAccessPrimitive thatIsNegative() {
        return host -> {
            val value = access(this, host);
            return value < 0;
        };
    }
    public default BooleanAccessPrimitive thatIsNotPositive() {
        return host -> {
            val value = access(this, host);
            return value <= 0;
        };
    }
    public default BooleanAccessPrimitive thatIsNotNegative() {
        return host -> {
            val value = access(this, host);
            return value >= 0;
        };
    }
    
    public default BooleanAccessPrimitive thatEquals(int anotherValue) {
        return thatIs(anotherValue);
    }
    public default BooleanAccessPrimitive thatEquals(IntSupplier anotherSupplier) {
        return thatIs(anotherSupplier);
    }
    public default BooleanAccessPrimitive thatEquals(ToIntFunction anotherAccess) {
        return thatIs(anotherAccess);
    }
    
    public default BooleanAccessPrimitive eq(int anotherValue) {
        return thatEquals(anotherValue);
    }
    public default BooleanAccessPrimitive eq(IntSupplier anotherSupplier) {
        return thatEquals(anotherSupplier);
    }
    public default BooleanAccessPrimitive eq(ToIntFunction anotherAccess) {
        return thatEquals(anotherAccess);
    }
    
    public default BooleanAccessPrimitive thatNotEquals(int anotherValue) {
        return thatNotEquals(anotherValue);
    }
    public default BooleanAccessPrimitive thatNotEquals(IntSupplier anotherSupplier) {
        return thatNotEquals(anotherSupplier);
    }
    public default BooleanAccessPrimitive thatNotEquals(ToIntFunction anotherAccess) {
        return thatNotEquals(anotherAccess);
    }
    
    public default BooleanAccessPrimitive neq(int anotherValue) {
        return thatNotEquals(anotherValue);
    }
    public default BooleanAccessPrimitive neq(IntSupplier anotherSupplier) {
        return thatNotEquals(anotherSupplier);
    }
    public default BooleanAccessPrimitive neq(ToIntFunction anotherAccess) {
        return thatNotEquals(anotherAccess);
    }
    
    public default BooleanAccessPrimitive thatEqualsOne() {
        return thatEquals(1);
    }
    
    public default BooleanAccessPrimitive thatEqualsZero() {
        return thatEquals(0);
    }
    
    public default BooleanAccessPrimitive thatEqualsMinusOne() {
        return thatEquals(-1);
    }
    
    public default BooleanAccessPrimitive thatEqualsFourtyTwo() {
        return thatEquals(42);
    }
    
    public default BooleanAccessPrimitive thatNotEqualsOne() {
        return thatEquals(1);
    }
    
    public default BooleanAccessPrimitive thatNotEqualsZero() {
        return thatEquals(0);
    }
    
    public default BooleanAccessPrimitive thatNotEqualsMinusOne() {
        return thatEquals(-1);
    }
    
    //-- Compare --
    
    public default Comparator inOrder() {
        return (a, b) -> {
            val aValue = this.apply(a);
            val bValue = this.apply(b);
            return compareOrNull(aValue, bValue);
        };
    }
    
    public default Comparator inReverseOrder() {
        return (a, b) -> {
            val aValue = this.apply(a);
            val bValue = this.apply(b);
            return compareOrNull(bValue, aValue);
        };
    }
    
    public default IntegerAccessPrimitive compareTo(int anotherValue) {
        return host -> {
            val value   = access(this, host);
            val compare = compareOrNull(value, anotherValue);
            return compare;
        };
    }
    public default IntegerAccessPrimitive compareTo(IntSupplier anotherSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(anotherSupplier);
            val compare      = compareOrNull(value, anotherValue);
            return compare;
        };
    }
    public default IntegerAccessPrimitive compareTo(ToIntFunction anotherFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherFunction, host);
            val compare      = compareOrNull(value, anotherValue);
            return compare;
        };
    }
    
    public default IntegerAccessPrimitive cmp(int anotherValue) {
        return compareTo(anotherValue);
    }
    public default IntegerAccessPrimitive cmp(IntSupplier anotherSupplier) {
        return compareTo(anotherSupplier);
    }
    public default IntegerAccessPrimitive cmp(ToIntFunction anotherAccess) {
        return compareTo(anotherAccess);
    }
    
    public default BooleanAccessPrimitive thatGreaterThan(int anotherValue) {
        return host -> {
            val value = access(this, host);
            return value > anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatGreaterThan(IntSupplier anotherSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(anotherSupplier);
            return value > anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatGreaterThan(ToIntFunction anotherAccess) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherAccess, host);
            return value > anotherValue;
        };
    }
    
    public default BooleanAccessPrimitive gt(int anotherValue) {
        return thatGreaterThan(anotherValue);
    }
    public default BooleanAccessPrimitive gt(IntSupplier anotherSupplier) {
        return thatGreaterThan(anotherSupplier);
    }
    public default BooleanAccessPrimitive gt(ToIntFunction anotherAccess) {
        return thatGreaterThan(anotherAccess);
    }
    
    public default BooleanAccessPrimitive thatLessThan(int anotherValue) {
        return host -> {
            val value = access(this, host);
            return value < anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatLessThan(IntSupplier anotherSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(anotherSupplier);
            return value < anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatLessThan(ToIntFunction anotherAccess) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherAccess, host);
            return value < anotherValue;
        };
    }
    
    public default BooleanAccessPrimitive lt(int anotherValue) {
        return thatLessThan(anotherValue);
    }
    public default BooleanAccessPrimitive lt(IntSupplier anotherSupplier) {
        return thatLessThan(anotherSupplier);
    }
    public default BooleanAccessPrimitive lt(ToIntFunction anotherAccess) {
        return thatLessThan(anotherAccess);
    }
    
    public default BooleanAccessPrimitive thatGreaterThanOrEqualsTo(int anotherValue) {
        return host -> {
            val value = access(this, host);
            return value >= anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatGreaterThanOrEqualsTo(IntSupplier anotherSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(anotherSupplier);
            return value >= anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatGreaterThanOrEqualsTo(ToIntFunction anotherAccess) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherAccess, host);
            return value >= anotherValue;
        };
    }
    
    public default BooleanAccessPrimitive gteq(int anotherValue) {
        return thatGreaterThanOrEqualsTo(anotherValue);
    }
    public default BooleanAccessPrimitive gteq(IntSupplier anotherSupplier) {
        return thatGreaterThanOrEqualsTo(anotherSupplier);
    }
    public default BooleanAccessPrimitive gteq(ToIntFunction anotherAccess) {
        return thatGreaterThanOrEqualsTo(anotherAccess);
    }
    
    public default BooleanAccessPrimitive thatLessThanOrEqualsTo(int anotherValue) {
        return host -> {
            val value = access(this, host);
            return value <= anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatLessThanOrEqualsTo(IntSupplier anotherSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(anotherSupplier);
            return value <= anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatLessThanOrEqualsTo(ToIntFunction anotherAccess) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherAccess, host);
            return value <= anotherValue;
        };
    }
    
    public default BooleanAccessPrimitive lteq(int anotherValue) {
        return thatLessThanOrEqualsTo(anotherValue);
    }
    public default BooleanAccessPrimitive lteq(IntSupplier anotherSupplier) {
        return thatLessThanOrEqualsTo(anotherSupplier);
    }
    public default BooleanAccessPrimitive lteq(ToIntFunction anotherAccess) {
        return thatLessThanOrEqualsTo(anotherAccess);
    }
    
    //-- Min+Max --
    
    public default IntegerAccessPrimitive min(int anotherValue) {
        return host -> {
            val value = access(this, host);
            return Math.min(value, anotherValue);
        };
    }
    public default IntegerAccessPrimitive min(IntSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            return Math.min(value, anotherValue);
        };
    }
    public default IntegerAccessPrimitive min(ToIntFunction valueFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(valueFunction, host);
            return Math.min(value, anotherValue);
        };
    }
    
    public default IntegerAccessPrimitive max(int anotherValue) {
        return host -> {
            val value = access(this, host);
            return Math.max(value, anotherValue);
        };
    }
    public default IntegerAccessPrimitive max(IntSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            return Math.max(value, anotherValue);
        };
    }
    public default IntegerAccessPrimitive max(ToIntFunction valueFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(valueFunction, host);
            return Math.max(value, anotherValue);
        };
    }
    
    //-- Math --
    
    public default MathOperators __mathOperators() {
        return IntMathOperators.instance;
    }
    
    public default BooleanAccessPrimitive thatIsOdd() {
        return host -> {
            val value = access(this, host);
            return value % 2 != 0;
        };
    }
    
    public default BooleanAccessPrimitive thatIsEven() {
        return host -> {
            val value = access(this, host);
            return value % 2 == 0;
        };
    }
    
    public default BooleanAccessPrimitive thatIsDivisibleBy(int anotherValue) {
        return host -> {
            val value = access(this, host);
            return value % anotherValue == 0;
        };
    }
    public default BooleanAccessPrimitive thatIsDivisibleBy(IntSupplier anotherAccess) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = anotherAccess.getAsInt();
            return value % anotherValue == 0;
        };
    }
    public default BooleanAccessPrimitive thatIsDivisibleBy(ToIntFunction anotherAccess) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherAccess, host);
            return value % anotherValue == 0;
        };
    }
    
    public default IntegerAccessPrimitive abs() {
        return host -> {
            val value = access(this, host);
            return (value < 0) ? -value : value;
        };
    }
    
    public default IntegerAccessPrimitive negate() {
        return host -> {
            val value = access(this, host);
            return -value;
        };
    }
    
    public default IntegerAccessPrimitive signum() {
        return host -> {
            val value = access(this, host);
            return (value == 0) ? 0 : (value < 0) ? -1 : 1;
        };
    }
    
    public default IntegerAccessPrimitive plus(int anotherValue) {
        return host -> {
            val value = access(this, host);
            return value + anotherValue;
        };
    }
    public default IntegerAccessPrimitive plus(IntSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            return value + anotherValue;
        };
    }
    public default IntegerAccessPrimitive plus(ToIntFunction valueFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(valueFunction, host);
            return value + anotherValue;
        };
    }
    
    public default IntegerAccessPrimitive minus(int anotherValue) {
        return host -> {
            val value = access(this, host);
            return value - anotherValue;
        };
    }
    public default IntegerAccessPrimitive minus(IntSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            return value - anotherValue;
        };
    }
    public default IntegerAccessPrimitive minus(ToIntFunction valueFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(valueFunction, host);
            return value - anotherValue;
        };
    }
    
    public default IntegerAccessPrimitive time(int anotherValue) {
        return host -> {
            val value = access(this, host);
            return value * anotherValue;
        };
    }
    public default IntegerAccessPrimitive time(IntSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            return value * anotherValue;
        };
    }
    public default IntegerAccessPrimitive time(ToIntFunction valueFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(valueFunction, host);
            return value * anotherValue;
        };
    }
    
    public default DoubleAccessPrimitive dividedBy(int anotherValue) {
        return host -> {
            val value = access(this, host);
            return 1.0 * value / anotherValue;
        };
    }
    public default DoubleAccessPrimitive dividedBy(IntSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            return 1.0 * value / anotherValue;
        };
    }
    public default DoubleAccessPrimitive dividedBy(ToIntFunction valueFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(valueFunction, host);
            return 1.0*value / anotherValue;
        };
    }
    
    public default IntegerAccessPrimitive remainderBy(int anotherValue) {
        return host -> {
            val value = access(this, host);
            return value % anotherValue;
        };
    }
    public default IntegerAccessPrimitive remainderBy(IntSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            return value % anotherValue;
        };
    }
    public default IntegerAccessPrimitive remainderBy(ToIntFunction valueFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(valueFunction, host);
            return value % anotherValue;
        };
    }
    
    public default DoubleAccessPrimitive inverse() {
        return host -> {
            val value = access(this, host);
            return 1/(value * 1.0);
        };
    }
    
    public default IntegerAccessPrimitive square() {
        return host -> {
            val value = access(this, host);
            return value * value;
        };
    }
    
    public default DoubleAccessPrimitive squareRoot () {
        return host -> {
            val value = access(this, host);
            return Math.sqrt(value);
        };
    }
    
    public default IntegerAccessPrimitive factorial() {
        return host -> {
            val value = access(this, host);
            if (value <= 0) {
                return 1;
            }
            
            return factorialRef.get().applyAsInt(value);
        };
    }
    
    // TODO - Make this Long once we are ready.
    
    public default LongAccessPrimitive pow(int anotherValue) {
        return host -> {
            val value = access(this, host);
            return (long)Math.pow(value, anotherValue);
        };
    }
    public default LongAccessPrimitive pow(IntSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            return (long)Math.pow(value, anotherValue);
        };
    }
    public default LongAccessPrimitive pow(ToIntFunction valueFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(valueFunction, host);
            return (long)Math.pow(value, anotherValue);
        };
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy