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-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.Function;
import java.util.function.ToDoubleFunction;

import functionalj.tuple.Tuple;
import functionalj.tuple.Tuple2;
import nullablej.nullable.Nullable;

@SuppressWarnings("javadoc")
@FunctionalInterface
public interface DoubleAccess 
        extends 
            NumberAccess>, 
            ToDoubleFunction, 
            ConcreteAccess> {
    
    @Override
    public default DoubleAccess newAccess(Function access) {
        return access::apply;
    }
    
    public default double applyAsDouble(HOST host) {
        return apply(host).doubleValue();
    }

    
    public default MathOperators __mathOperators() {
        return __DoubleMathOperators;
    }
    
    
    public static MathOperators __DoubleMathOperators = new MathOperators() {
        
        @Override
        public Double zero() {
            return 0.0;
        }
        @Override
        public Double one() {
            return 1.0;
        }
        @Override
        public Double minusOne() {
            return -1.0;
        }
        @Override
        public Integer toInteger(Double number) {
            return toDouble(number).intValue();
        }
        @Override
        public Long toLong(Double number) {
            return toDouble(number).longValue();
        }
        @Override
        public Double toDouble(Double number) {
            return Nullable.of(number).orElse(0.0);
        }
        @Override
        public BigInteger toBigInteger(Double number) {
            return BigInteger.valueOf(toLong(number));
        }
        @Override
        public BigDecimal toBigDecimal(Double number) {
            return new BigDecimal(toDouble(number));
        }
        
        @Override
        public Double add(Double number1, Double number2) {
            double v1 = (number1 == null) ? 0 : number1.doubleValue();
            double v2 = (number2 == null) ? 0 : number2.doubleValue();
            return v1 + v2;
        }
        @Override
        public Double subtract(Double number1, Double number2) {
            double v1 = (number1 == null) ? 0 : number1.doubleValue();
            double v2 = (number2 == null) ? 0 : number2.doubleValue();
            return v1 - v2;
        }
        @Override
        public Double multiply(Double number1, Double number2) {
            double v1 = (number1 == null) ? 0 : number1.doubleValue();
            double v2 = (number2 == null) ? 0 : number2.doubleValue();
            return v1 * v2;
        }
        @Override
        public Double divide(Double number1, Double number2) {
            double v1 = (number1 == null) ? 0 : number1.doubleValue();
            double v2 = (number2 == null) ? 0 : number2.doubleValue();
            return v1 / v2;
        }
        @Override
        public Double remainder(Double number1, Double number2) {
            double v1 = (number1 == null) ? 0 : number1.doubleValue();
            double v2 = (number2 == null) ? 0 : number2.doubleValue();
            return v1 % v2;
        }

        @Override
        public Tuple2 divideAndRemainder(Double number1, Double number2) {
            double v1 = (number1 == null) ? 0 : number1.doubleValue();
            double v2 = (number2 == null) ? 0 : number2.doubleValue();
            return Tuple.of(v1 / v2, v1 % v2);
        }

        @Override
        public Double pow(Double number1, Double number2) {
            double v1 = (number1 == null) ? 0 : number1.doubleValue();
            double v2 = (number2 == null) ? 0 : number2.doubleValue();
            return Math.pow(v1, v2);
        }

        @Override
        public Double abs(Double number) {
            double v = (number == null) ? 0 : number.doubleValue();
            return Math.abs(v);
        }
        @Override
        public Double negate(Double number) {
            double v = (number == null) ? 0 : number.doubleValue();
            return -1.0 * v;
        }
        @Override
        public Double signum(Double number) {
            double v = (number == null) ? 0 : number.doubleValue();
            return Math.signum(v);
        }

        @Override
        public Double min(Double number1, Double number2) {
            double v1 = (number1 == null) ? 0 : number1.doubleValue();
            double v2 = (number2 == null) ? 0 : number2.doubleValue();
            return Math.min(v1, v2);
        }
        @Override
        public Double max(Double number1, Double number2) {
            double v1 = (number1 == null) ? 0 : number1.doubleValue();
            double v2 = (number2 == null) ? 0 : number2.doubleValue();
            return Math.max(v1, v2);
        }
        
    };
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy