001/*
002 * The MIT License (MIT)
003 *
004 * Copyright (c) 2015-2023 decimal4j (tools4j), Marco Terzer
005 *
006 * Permission is hereby granted, free of charge, to any person obtaining a copy
007 * of this software and associated documentation files (the "Software"), to deal
008 * in the Software without restriction, including without limitation the rights
009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010 * copies of the Software, and to permit persons to whom the Software is
011 * furnished to do so, subject to the following conditions:
012 *
013 * The above copyright notice and this permission notice shall be included in all
014 * copies or substantial portions of the Software.
015 *
016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
022 * SOFTWARE.
023 */
024package org.decimal4j.arithmetic;
025
026import java.math.BigDecimal;
027import java.math.BigInteger;
028import java.math.RoundingMode;
029
030import org.decimal4j.api.DecimalArithmetic;
031import org.decimal4j.scale.Scales;
032import org.decimal4j.truncate.OverflowMode;
033import org.decimal4j.truncate.TruncationPolicy;
034
035/**
036 * Base class for all arithmetic implementations providing operations which are
037 * common irrespective of {@link #getScale() scale}, {@link RoundingMode
038 * rounding mode} and {@link #getOverflowMode() overflow mode}.
039 */
040abstract public class AbstractArithmetic implements DecimalArithmetic {
041
042        @Override
043        public final DecimalArithmetic deriveArithmetic(int scale) {
044                if (scale != getScale()) {
045                        return Scales.getScaleMetrics(scale).getArithmetic(getTruncationPolicy());
046                }
047                return this;
048        }
049
050        @Override
051        public final DecimalArithmetic deriveArithmetic(RoundingMode roundingMode) {
052                return deriveArithmetic(roundingMode, getOverflowMode());
053        }
054
055        @Override
056        public final DecimalArithmetic deriveArithmetic(RoundingMode roundingMode, OverflowMode overflowMode) {
057                if (roundingMode != getRoundingMode() | overflowMode != getOverflowMode()) {
058                        return overflowMode.isChecked() ? getScaleMetrics().getCheckedArithmetic(roundingMode) : getScaleMetrics().getArithmetic(roundingMode);
059                }
060                return this;
061        }
062
063        @Override
064        public final DecimalArithmetic deriveArithmetic(OverflowMode overflowMode) {
065                return deriveArithmetic(getRoundingMode(), overflowMode);
066        }
067
068        @Override
069        public final DecimalArithmetic deriveArithmetic(TruncationPolicy truncationPolicy) {
070                return deriveArithmetic(truncationPolicy.getRoundingMode(), truncationPolicy.getOverflowMode());
071        }
072
073        @Override
074        public final int signum(long uDecimal) {
075                return Long.signum(uDecimal);
076        }
077
078        @Override
079        public final int compare(long uDecimal1, long uDecimal2) {
080                return Long.compare(uDecimal1, uDecimal2);
081        }
082        
083        @Override
084        public final int compareToUnscaled(long uDecimal, long unscaled, int scale) {
085                return Compare.compareUnscaled(uDecimal, getScale(), unscaled, scale);
086        }
087
088        @Override
089        public final long fromBigInteger(BigInteger value) {
090                return BigIntegerConversion.bigIntegerToUnscaled(getScaleMetrics(), value);
091        }
092
093        @Override
094        public final BigDecimal toBigDecimal(long uDecimal) {
095                return BigDecimalConversion.unscaledToBigDecimal(getScaleMetrics(), uDecimal);
096        }
097
098        @Override
099        public final BigDecimal toBigDecimal(long uDecimal, int scale) {
100                return BigDecimalConversion.unscaledToBigDecimal(getScaleMetrics(), getRoundingMode(), uDecimal, scale);
101        }
102
103}