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.RoundingMode;
028
029import org.decimal4j.scale.Scale0f;
030import org.decimal4j.truncate.CheckedRounding;
031import org.decimal4j.truncate.DecimalRounding;
032
033/**
034 * Arithmetic implementation without rounding but with overflow check for the
035 * special case with {@link Scale0f}, that is, for longs. An exception is thrown
036 * if an operation leads to an overflow.
037 */
038public final class CheckedScale0fTruncatingArithmetic extends AbstractCheckedScale0fArithmetic {
039
040        /**
041         * The singleton instance.
042         */
043        public static final CheckedScale0fTruncatingArithmetic INSTANCE = new CheckedScale0fTruncatingArithmetic();
044
045        @Override
046        public final RoundingMode getRoundingMode() {
047                return RoundingMode.DOWN;
048        }
049
050        @Override
051        public final CheckedRounding getTruncationPolicy() {
052                return CheckedRounding.DOWN;
053        }
054
055        @Override
056        public final long addUnscaled(long uDecimal, long unscaled, int scale) {
057                return Add.addUnscaledUnscaledChecked(this, uDecimal, unscaled, scale);
058        }
059
060        @Override
061        public final long subtractUnscaled(long uDecimal, long unscaled, int scale) {
062                return Sub.subtractUnscaledUnscaledChecked(this, uDecimal, unscaled, scale);
063        }
064
065        @Override
066        public final long multiplyByUnscaled(long uDecimal, long unscaled, int scale) {
067                return Mul.multiplyByUnscaledChecked(this, uDecimal, unscaled, scale);
068        }
069
070        @Override
071        public final long divideByUnscaled(long uDecimal, long unscaled, int scale) {
072                return Div.divideByUnscaledChecked(this, uDecimal, unscaled, scale);
073        }
074
075        @Override
076        public final long divide(long uDecimalDividend, long uDecimalDivisor) {
077                return Checked.divideByLong(this, uDecimalDividend, uDecimalDivisor);
078        }
079
080        @Override
081        public final long divideByLong(long uDecimalDividend, long lDivisor) {
082                return Checked.divideByLong(this, uDecimalDividend, lDivisor);
083        }
084
085        @Override
086        public final long avg(long a, long b) {
087                return Avg.avg(a, b);
088        }
089
090        @Override
091        public final long invert(long uDecimal) {
092                return Invert.invertLong(uDecimal);
093        }
094
095        @Override
096        public final long pow(long uDecimalBase, int exponent) {
097                return Pow.powLongChecked(this, DecimalRounding.DOWN, uDecimalBase, exponent);
098        }
099
100        @Override
101        public final long sqrt(long uDecimal) {
102                return Sqrt.sqrtLong(uDecimal);
103        }
104        
105        @Override
106        public final long divideByPowerOf10(long uDecimal, int n) {
107                return Pow10.divideByPowerOf10Checked(this, uDecimal, n);
108        }
109
110        @Override
111        public final long multiplyByPowerOf10(long uDecimal, int n) {
112                return Pow10.multiplyByPowerOf10Checked(this, uDecimal, n);
113        }
114
115        @Override
116        public final long shiftLeft(long uDecimal, int positions) {
117                return Shift.shiftLeftChecked(this, DecimalRounding.DOWN, uDecimal, positions);
118        }
119
120        @Override
121        public final long shiftRight(long uDecimal, int positions) {
122                return Shift.shiftRightChecked(this, DecimalRounding.DOWN, uDecimal, positions);
123        }
124
125        @Override
126        public final long round(long uDecimal, int precision) {
127                return Round.round(this, uDecimal, precision);
128        }
129
130        @Override
131        public final float toFloat(long uDecimal) {
132                return FloatConversion.longToFloat(this, uDecimal);
133        }
134
135        @Override
136        public final double toDouble(long uDecimal) {
137                return DoubleConversion.longToDouble(this, uDecimal);
138        }
139
140        @Override
141        public final long toUnscaled(long uDecimal, int scale) {
142                return UnscaledConversion.unscaledToUnscaled(scale, this, uDecimal);
143        }
144
145        @Override
146        public final long fromFloat(float value) {
147                return FloatConversion.floatToLong(value);
148        }
149
150        @Override
151        public final long fromDouble(double value) {
152                return DoubleConversion.doubleToLong(value);
153        }
154        
155        @Override
156        public final long fromUnscaled(long unscaledValue, int scale) {
157                return UnscaledConversion.unscaledToLong(this, unscaledValue, scale);
158        }
159
160        @Override
161        public final long fromBigDecimal(BigDecimal value) {
162                return BigDecimalConversion.bigDecimalToLong(RoundingMode.DOWN, value);
163        }
164
165        @Override
166        public final long parse(String value) {
167                return StringConversion.parseLong(this, DecimalRounding.DOWN, value, 0, value.length());
168        }
169        
170        @Override
171        public final long parse(CharSequence value, int start, int end) {
172                return StringConversion.parseLong(this, DecimalRounding.DOWN, value, start, end);
173        }
174}