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

functionalj.lens.lenses.DoubleAccess 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.util.Comparator;
import java.util.function.DoublePredicate;
import java.util.function.DoubleSupplier;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;

import functionalj.function.Func1;
import functionalj.list.doublelist.DoubleFuncList;
import functionalj.ref.Ref;
import lombok.val;


/**
 * Classes implementing this interface know how to access to a double value.
 **/
public interface DoubleAccess 
                    extends 
                        NumberAccess>, 
                        ToDoubleFunction, 
                        ConcreteAccess> {
    
    /** The reference to a function to calculate factorial for integer. **/
    public static final Ref equalPrecision = Ref.ofValue(0.0).whenAbsentUse(0.0);
    
    public static final Ref equalPrecisionToUse 
            = Ref.dictactedTo(() -> Math.abs(DoubleAccess.equalPrecision.get()));
    
    //== Constructor ==
    
    public static  DoubleAccess of(Function accessToValue) {
        requireNonNull(accessToValue);
        
        if (accessToValue instanceof DoubleAccess) {
            return (DoubleAccess)accessToValue;
        }
        
        if (accessToValue instanceof ToDoubleFunction) {
            @SuppressWarnings("unchecked")
            val func1  = (ToDoubleFunction)accessToValue;
            val access = ofPrimitive(func1);
            return access;
        }
        
        if (accessToValue instanceof Func1) {
            val func1  = (Func1)accessToValue;
            val access = (DoubleAccessBoxed)func1::applyUnsafe;
            return access;
        }
        
        val func   = (Function)accessToValue;
        val access = (DoubleAccessBoxed)(host -> func.apply(host));
        return access;
    }
    
    public static  DoubleAccess ofPrimitive(ToDoubleFunction accessToValue) {
        requireNonNull(accessToValue);
        val access = (DoubleAccessPrimitive)accessToValue::applyAsDouble;
        return access;
    }
    
    @Override
    public default DoubleAccess newAccess(Function accessToValue) {
        return of(accessToValue);
    }
    
    //== abstract functionalities ==
    
    public double applyAsDouble(HOST host);
    
    
    public Double applyUnsafe(HOST host) throws Exception;
    
    
    //-- conversion --
    
    public default DoubleAccessBoxed boxed() {
        return host -> apply(host);
    }
    
    @Override
    public default IntegerAccessPrimitive asInteger() {
        return asInteger(Integer.MIN_VALUE, Integer.MAX_VALUE);
    }
    
    @Override
    public default LongAccessPrimitive asLong() {
        return asLong(Long.MIN_VALUE, Long.MAX_VALUE);
    }
    
    @Override
    public default DoubleAccessPrimitive asDouble() {
        return host -> access(this, host);
    }
    
    public default IntegerAccessPrimitive asInteger(int overflowValue) {
        return asInteger(overflowValue, overflowValue);
    }
    
    public default LongAccessPrimitive asLong(long overflowValue) {
        return asLong(overflowValue, overflowValue);
    }
    
    public default IntegerAccessPrimitive asInteger(int negativeOverflowValue, int positiveOverflowValue) {
        return host -> {
            val value = access(this, host);
            if (value < Integer.MIN_VALUE)
                return negativeOverflowValue;
            
            if (value > Integer.MAX_VALUE)
                return positiveOverflowValue;
            
            return (int)Math.round(value);
        };
    }
    
    public default LongAccessPrimitive asLong(long negativeOverflowValue, long positiveOverflowValue) {
        return host -> {
            val value = access(this, host);
            if (value < Long.MIN_VALUE)
                return negativeOverflowValue;
            
            if (value > Long.MAX_VALUE)
                return positiveOverflowValue;
            
            return (long)Math.round(value);
        };
    }
    
    public default IntegerAccessBoxed asIntegerOrNull(Integer negativeOverflowValue, Integer positiveOverflowValue) {
        return host -> {
            double doubleValue = access(this, host);
            if (doubleValue < Integer.MIN_VALUE)
                return negativeOverflowValue;
            
            if (doubleValue > Integer.MIN_VALUE)
                return positiveOverflowValue;
            
            return (int)doubleValue;
        };
    }
    
    public default LongAccessBoxed asLongOrNull(Long negativeOverflowValue, Long positiveOverflowValue) {
        return host -> {
            double doubleValue = access(this, host);
            if (doubleValue < Long.MIN_VALUE)
                return negativeOverflowValue;
            
            if (doubleValue > Long.MIN_VALUE)
                return positiveOverflowValue;
            
            return (long)doubleValue;
        };
    }
    
    public default DoubleAccessPrimitive round() {
        return host -> {
            val value = access(this, host);
            return Math.round(value);
        };
    }
    public default IntegerAccessPrimitive roundToInt() {
        return round().asInteger();
    }
    
    public default LongAccessPrimitive roundToLong() {
        return round().asLong();
    }
    
    public default DoubleAccessPrimitive ceil() {
        return host -> {
            val value = access(this, host);
            return Math.ceil(value);
        };
    }
    
    public default IntegerAccessPrimitive ceilToInt() {
        return round().asInteger();
    }
    
    public default LongAccessPrimitive ceilToLong() {
        return round().asLong();
    }
    
    public default DoubleAccessPrimitive floor() {
        return host -> {
            val value = access(this, host);
            return Math.floor(value);
        };
    }
    public default IntegerAccessPrimitive floorToInt() {
        return floor().asInteger();
    }
    public default LongAccessPrimitive floorToLong() {
        return floor().asLong();
    }
    
    public default DoubleAccessPrimitive roundBy(double precision) {
        return host -> {
            val value = access(this, host);
            if (precision == 0.0) {
                return Math.round(value);
            }
            
            return Math.round(value / precision) * precision;
        };
    }
    public default DoubleAccessPrimitive roundBy(DoubleSupplier precisionSupplier) {
        return host -> {
            val value     = access(this, host);
            val precision = getPrimitive(precisionSupplier);
            if (precision == 0.0) {
                return Math.round(value);
            }
            
            return Math.round(value / precision) * precision;
        };
    }
    public default DoubleAccessPrimitive roundBy(ToDoubleFunction precisionFunction) {
        return host -> {
            val value     = access(this, host);
            val precision = applyPrimitive(precisionFunction, host);
            if (precision == 0.0) {
                return Math.round(value);
            }
            
            return Math.round(value / precision) * precision;
        };
    }
    
    public default DoubleAccessPrimitive ceilBy(double precision) {
        return host -> {
            val value = access(this, host);
            if (precision == 0.0) {
                return Math.ceil(value);
            }
            
            return Math.ceil(value / precision) * precision;
        };
    }
    public default DoubleAccessPrimitive ceilBy(DoubleSupplier precisionSupplier) {
        return host -> {
            val value     = access(this, host);
            val precision = getPrimitive(precisionSupplier);
            if (precision == 0.0) {
                return Math.ceil(value);
            }
            
            return Math.ceil(value / precision) * precision;
        };
    }
    public default DoubleAccessPrimitive ceilBy(ToDoubleFunction precisionFunction) {
        return host -> {
            val value     = access(this, host);
            val precision = applyPrimitive(precisionFunction, host);
            if (precision == 0.0) {
                return Math.ceil(value);
            }
            
            return Math.ceil(value / precision) * precision;
        };
    }
    
    public default DoubleAccessPrimitive floorBy(double precision) {
        return host -> {
            val value = access(this, host);
            if (precision == 0.0) {
                return Math.floor(value);
            }
            
            return Math.floor(value / precision) * precision;
        };
    }
    public default DoubleAccessPrimitive floorBy(DoubleSupplier precisionSupplier) {
        return host -> {
            val value     = access(this, host);
            val precision = getPrimitive(precisionSupplier);
            if (precision == 0.0) {
                return Math.floor(value);
            }
            
            return Math.floor(value / precision) * precision;
        };
    }
    public default DoubleAccessPrimitive floorBy(ToDoubleFunction precisionFunction) {
        return host -> {
            val value     = access(this, host);
            val precision = applyPrimitive(precisionFunction, host);
            if (precision == 0.0) {
                return Math.floor(value);
            }
            
            return Math.floor(value / precision) * precision;
        };
    }
    
    public default StringAccess asString() {
        return host -> "" + access(this, host);
    }
    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 BigDecimal.valueOf(value).toBigInteger();
        };
    }
    public default BigDecimalAccess asBitDecimal() {
        return host -> {
            val value = access(this, host);
            return BigDecimal.valueOf(value);
        };
    }
    
    // TODO - Find a better way to format this that allow a fix width disregards of the magnitude of the value.
    //          or just redirect the format to another function that can be substituted.
    
    //-- Equality --
    
    public default BooleanAccessPrimitive that(DoublePredicate checker) {
        return host -> {
            val value = access(this, host);
            return checker.test(value);
        };
    }
    
    public default BooleanAccessPrimitive thatIs(double anotherValue) {
        return host -> {
            val value = access(this, host);
            return value == anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatIs(DoubleSupplier anotherSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(anotherSupplier);
            return value == anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatIs(ToDoubleFunction anotherAccess) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherAccess, host);
            return value == anotherValue;
        };
    }
    
    public default BooleanAccessPrimitive thatIsNot(double anotherValue) {
        return host -> {
            val value = access(this, host);
            return value != anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatIsNot(DoubleSupplier anotherSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(anotherSupplier);
            return value != anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatIsNot(ToDoubleFunction anotherAccess) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherAccess, host);
            return value != anotherValue;
        };
    }
    
    public default BooleanAccessPrimitive thatIsAnyOf(double ... otherValues) {
        return host -> {
            val value = access(this, host);
            for (val anotherValue : otherValues) {
                if (value == anotherValue) {
                    return true;
                }
            }
            return false;
        };
    }
    public default BooleanAccessPrimitive thatIsAnyOf(DoubleFuncList otherValues) {
        return host -> {
            val value = access(this, host);
            return otherValues.anyMatch(anotherValue -> value == anotherValue);
        };
    }
    
    public default BooleanAccessPrimitive thatIsNoneOf(double ... otherValues) {
        return host -> {
            val value = access(this, host);
            for (val anotherValue : otherValues) {
                if (value == anotherValue) {
                    return false;
                }
            }
            return true;
        };
    }
    public default BooleanAccessPrimitive thatIsNoneOf(DoubleFuncList 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 DoubleAccessEqual thatEquals(double anotherValue) {
        return new DoubleAccessEqual<>(false, this, (host, value) -> anotherValue);
    }
    public default DoubleAccessEqual thatEquals(DoubleSupplier anotherSupplier) {
        return new DoubleAccessEqual<>(false, this, (host, value) -> anotherSupplier.getAsDouble());
    }
    public default DoubleAccessEqual thatEquals(ToDoubleFunction anotherAccess) {
        return new DoubleAccessEqual<>(false, this, (host, value) -> anotherAccess.applyAsDouble(host));
    }
    
    public default DoubleAccessEqual eq(double anotherValue) {
        return thatEquals(anotherValue);
    }
    public default DoubleAccessEqual eq(DoubleSupplier anotherSupplier) {
        return thatEquals(anotherSupplier);
    }
    public default DoubleAccessEqual eq(ToDoubleFunction anotherAccess) {
        return thatEquals(anotherAccess);
    }
    
    public default DoubleAccessEqual thatNotEquals(double anotherValue) {
        return new DoubleAccessEqual<>(true, this, (host, value) -> anotherValue);
    }
    public default DoubleAccessEqual thatNotEquals(DoubleSupplier anotherSupplier) {
        return new DoubleAccessEqual<>(true, this, (host, value) -> anotherSupplier.getAsDouble());
    }
    public default DoubleAccessEqual thatNotEquals(ToDoubleFunction anotherAccess) {
        return new DoubleAccessEqual<>(true, this, (host, value) -> anotherAccess.applyAsDouble(host));
    }
    
    public default DoubleAccessEqual neq(double anotherValue) {
        return thatNotEquals(anotherValue);
    }
    public default DoubleAccessEqual neq(DoubleSupplier anotherSupplier) {
        return thatNotEquals(anotherSupplier);
    }
    public default DoubleAccessEqual neq(ToDoubleFunction anotherAccess) {
        return thatNotEquals(anotherAccess);
    }
    
    public default DoubleAccessEqual thatEqualsOne() {
        return thatEquals(1);
    }
    
    public default DoubleAccessEqual thatEqualsZero() {
        return thatEquals(0);
    }
    
    public default DoubleAccessEqual thatEqualsMinusOne() {
        return thatEquals(-1);
    }
    
    public default DoubleAccessEqual thatEqualsFourtyTwo() {
        return thatEquals(42);
    }
    
    public default DoubleAccessEqual thatNotEqualsOne() {
        return thatEquals(1);
    }
    
    public default DoubleAccessEqual thatNotEqualsZero() {
        return thatEquals(0);
    }
    
    public default DoubleAccessEqual 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(double anotherValue) {
        return host -> {
            val value   = access(this, host);
            val compare = compareOrNull(value, anotherValue);
            return compare;
        };
    }
    public default IntegerAccessPrimitive compareTo(DoubleSupplier anotherSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(anotherSupplier);
            val compare      = compareOrNull(value, anotherValue);
            return compare;
        };
    }
    public default IntegerAccessPrimitive compareTo(ToDoubleFunction anotherFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherFunction, host);
            val compare      = compareOrNull(value, anotherValue);
            return compare;
        };
    }
    
    public default IntegerAccessPrimitive cmp(double anotherValue) {
        return compareTo(anotherValue);
    }
    public default IntegerAccessPrimitive cmp(DoubleSupplier anotherSupplier) {
        return compareTo(anotherSupplier);
    }
    public default IntegerAccessPrimitive cmp(ToDoubleFunction anotherAccess) {
        return compareTo(anotherAccess);
    }
    
    public default BooleanAccessPrimitive thatGreaterThan(double anotherValue) {
        return host -> {
            val value = access(this, host);
            return value > anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatGreaterThan(DoubleSupplier anotherSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(anotherSupplier);
            return value > anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatGreaterThan(ToDoubleFunction anotherAccess) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherAccess, host);
            return value > anotherValue;
        };
    }
    
    public default BooleanAccessPrimitive gt(double anotherValue) {
        return thatGreaterThan(anotherValue);
    }
    public default BooleanAccessPrimitive gt(DoubleSupplier anotherSupplier) {
        return thatGreaterThan(anotherSupplier);
    }
    public default BooleanAccessPrimitive gt(ToDoubleFunction anotherAccess) {
        return thatGreaterThan(anotherAccess);
    }
    
    public default BooleanAccessPrimitive thatLessThan(double anotherValue) {
        return host -> {
            val value = access(this, host);
            return value < anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatLessThan(DoubleSupplier anotherSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(anotherSupplier);
            return value < anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatLessThan(ToDoubleFunction anotherAccess) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherAccess, host);
            return value < anotherValue;
        };
    }
    
    public default BooleanAccessPrimitive lt(double anotherValue) {
        return thatLessThan(anotherValue);
    }
    public default BooleanAccessPrimitive lt(DoubleSupplier anotherSupplier) {
        return thatLessThan(anotherSupplier);
    }
    public default BooleanAccessPrimitive lt(ToDoubleFunction anotherAccess) {
        return thatLessThan(anotherAccess);
    }
    
    public default BooleanAccessPrimitive thatGreaterThanOrEqualsTo(double anotherValue) {
        return host -> {
            val value = access(this, host);
            return value >= anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatGreaterThanOrEqualsTo(DoubleSupplier anotherSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(anotherSupplier);
            return value >= anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatGreaterThanOrEqualsTo(ToDoubleFunction anotherAccess) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherAccess, host);
            return value >= anotherValue;
        };
    }
    
    public default BooleanAccessPrimitive gteq(double anotherValue) {
        return thatGreaterThanOrEqualsTo(anotherValue);
    }
    public default BooleanAccessPrimitive gteq(DoubleSupplier anotherSupplier) {
        return thatGreaterThanOrEqualsTo(anotherSupplier);
    }
    public default BooleanAccessPrimitive gteq(ToDoubleFunction anotherAccess) {
        return thatGreaterThanOrEqualsTo(anotherAccess);
    }
    
    public default BooleanAccessPrimitive thatLessThanOrEqualsTo(double anotherValue) {
        return host -> {
            val value = access(this, host);
            return value <= anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatLessThanOrEqualsTo(DoubleSupplier anotherSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(anotherSupplier);
            return value <= anotherValue;
        };
    }
    public default BooleanAccessPrimitive thatLessThanOrEqualsTo(ToDoubleFunction anotherAccess) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherAccess, host);
            return value <= anotherValue;
        };
    }
    
    public default BooleanAccessPrimitive lteq(double anotherValue) {
        return thatLessThanOrEqualsTo(anotherValue);
    }
    public default BooleanAccessPrimitive lteq(DoubleSupplier anotherSupplier) {
        return thatLessThanOrEqualsTo(anotherSupplier);
    }
    public default BooleanAccessPrimitive lteq(ToDoubleFunction anotherAccess) {
        return thatLessThanOrEqualsTo(anotherAccess);
    }
    
    //-- Min+Max --
    
    public default DoubleAccessPrimitive min(double anotherValue) {
        return host -> {
            val value = access(this, host);
            return Math.min(value, anotherValue);
        };
    }
    public default DoubleAccessPrimitive min(DoubleSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            return Math.min(value, anotherValue);
        };
    }
    public default DoubleAccessPrimitive min(ToDoubleFunction valueFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(valueFunction, host);
            return Math.min(value, anotherValue);
        };
    }
    
    public default DoubleAccessPrimitive max(double anotherValue) {
        return host -> {
            val value = access(this, host);
            return Math.max(value, anotherValue);
        };
    }
    public default DoubleAccessPrimitive max(DoubleSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            return Math.max(value, anotherValue);
        };
    }
    public default DoubleAccessPrimitive max(ToDoubleFunction valueFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(valueFunction, host);
            return Math.max(value, anotherValue);
        };
    }
    
    //-- Math --
    
    public default MathOperators __mathOperators() {
        return DoubleMathOperators.instance;
    }
    
    public default BooleanAccessPrimitive thatIsRound() {
        return host -> {
            val value = access(this, host);
            return 1.0*Math.round(value) == value;
        };
    }
    
    public default DoubleAccessPrimitive abs() {
        return host -> {
            val value = access(this, host);
            return (value < 0) ? -value : value;
        };
    }
    
    public default DoubleAccessPrimitive negate() {
        return host -> {
            val value = access(this, host);
            return -value;
        };
    }
    
    public default DoubleAccessPrimitive signum() {
        return host -> {
            val value = access(this, host);
            return (value == 0) ? 0 : (value < 0) ? -1 : 1;
        };
    }
    
    public default DoubleAccessPrimitive plus(double anotherValue) {
        return host -> {
            val value = access(this, host);
            return value + anotherValue;
        };
    }
    public default DoubleAccessPrimitive plus(DoubleSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            return value + anotherValue;
        };
    }
    public default DoubleAccessPrimitive plus(ToDoubleFunction valueFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(valueFunction, host);
            return value + anotherValue;
        };
    }
    
    public default DoubleAccessPrimitive minus(double anotherValue) {
        return host -> {
            val value = access(this, host);
            return value - anotherValue;
        };
    }
    public default DoubleAccessPrimitive minus(DoubleSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            return value - anotherValue;
        };
    }
    public default DoubleAccessPrimitive minus(ToDoubleFunction valueFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(valueFunction, host);
            return value - anotherValue;
        };
    }
    
    public default DoubleAccessPrimitive time(double anotherValue) {
        return host -> {
            val value = access(this, host);
            return value * anotherValue;
        };
    }
    public default DoubleAccessPrimitive time(DoubleSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            return value * anotherValue;
        };
    }
    public default DoubleAccessPrimitive time(ToDoubleFunction valueFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(valueFunction, host);
            return value * anotherValue;
        };
    }
    
    public default DoubleAccessPrimitive dividedBy(double anotherValue) {
        return host -> {
            val value = access(this, host);
            return 1.0 * value / anotherValue;
        };
    }
    public default DoubleAccessPrimitive dividedBy(DoubleSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            return 1.0 * value / anotherValue;
        };
    }
    public default DoubleAccessPrimitive dividedBy(ToDoubleFunction valueFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(valueFunction, host);
            return 1.0*value / anotherValue;
        };
    }
    
    public default DoubleAccessPrimitive remainderBy(double anotherValue) {
        return host -> {
            val value = access(this, host);
            val division = Math.round(value / anotherValue);
            return value - (division * anotherValue);
        };
    }
    public default DoubleAccessPrimitive remainderBy(DoubleSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            val division     = Math.round(value / anotherValue);
            return value - (division * anotherValue);
        };
    }
    public default DoubleAccessPrimitive remainderBy(ToDoubleFunction valueFunction) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(valueFunction, host);
            val division     = Math.round(value / anotherValue);
            return value - (division * anotherValue);
        };
    }
    
    public default DoubleAccessPrimitive inverse() {
        return host -> {
            val value = access(this, host);
            return 1/(value * 1.0);
        };
    }
    
    public default DoubleAccessPrimitive 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 DoubleAccessPrimitive pow(double anotherValue) {
        return host -> {
            val value = access(this, host);
            return Math.pow(value, anotherValue);
        };
    }
    public default DoubleAccessPrimitive pow(DoubleSupplier valueSupplier) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = getPrimitive(valueSupplier);
            return Math.pow(value, anotherValue);
        };
    }
    public default DoubleAccessPrimitive pow(ToDoubleFunction anotherAccess) {
        return host -> {
            val value        = access(this, host);
            val anotherValue = applyPrimitive(anotherAccess, host);
            return Math.pow(value, anotherValue);
        };
    }
    
    public default DoubleAccessPrimitive exp() {
        return host -> {
            double doubleValue = access(this, host);
            return Math.exp(doubleValue);
        };
    }
    
    /**
     * Returns ex -1.  Note that for values of
     * x near 0, the exact sum of
     * {@code expm1(x)} + 1 is much closer to the true
     * result of ex than {@code exp(x)}.
     **/
    public default DoubleAccessPrimitive expm1() {
        return host -> {
            double doubleValue = access(this, host);
            return Math.expm1(doubleValue);
        };
    }
    
    public default DoubleAccessPrimitive log() {
        return host -> {
            double doubleValue = access(this, host);
            return Math.log(doubleValue);
        };
    }
    
    public default DoubleAccessPrimitive log10() {
        return host -> {
            double doubleValue = access(this, host);
            return Math.log10(doubleValue);
        };
    }
    
    /**
     * Returns the base 10 logarithm of a {@code double} value.
     * Special cases:
     *
     * 
  • If the argument is NaN or less than zero, then the result * is NaN. *
  • If the argument is positive infinity, then the result is * positive infinity. *
  • If the argument is positive zero or negative zero, then the * result is negative infinity. *
  • If the argument is equal to 10n for * integer n, then the result is n. *
* *

The computed result must be within 1 ulp of the exact result. * Results must be semi-monotonic. */ public default DoubleAccessPrimitive log1p() { return host -> { double doubleValue = access(this, host); return Math.log1p(doubleValue); }; } // TODO - Add more // Math.addExact((int)0, (int)0) // Math.addExact((long)0, (long)0) // Math.decrementExact((int)0) // Math.decrementExact((long)0) // Math.incrementExact((int)0) // Math.incrementExact((long)0) // Math.multiplyExact(int, int) // Math.multiplyExact(long, long) // Math.negateExact(int) // Math.negateExact(long) // Math.subtractExact(int, int) // Math.subtractExact(long, long) // Math.toIntExact(0) // TODO - Add more. // Math.acos(doubleValue) // Math.asin(doubleValue) // Math.tan(doubleValue) // Math.tan2(doubleValue) // Math.cos(doubleValue) // Math.cosh(doubleValue) // Math.sin(doubleValue) // Math.sinh(doubleValue) // Math.tan(doubleValue) // Math.tanh(doubleValue) // // Math.toDegrees(doubleValue) // Math.toRadians(doubleValue) }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy