OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 142 Rev 177
Line 11... Line 11...
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.utils;
14
 package org.openconcerto.utils;
15
 
15
 
-
 
16
import org.openconcerto.utils.convertor.NumberConvertor;
-
 
17
 
16
import java.math.BigDecimal;
18
import java.math.BigDecimal;
17
import java.math.BigInteger;
19
import java.math.BigInteger;
-
 
20
import java.math.RoundingMode;
-
 
21
import java.util.Collections;
-
 
22
import java.util.EnumSet;
-
 
23
import java.util.Set;
18
import java.util.concurrent.atomic.AtomicInteger;
24
import java.util.concurrent.atomic.AtomicInteger;
19
import java.util.concurrent.atomic.AtomicLong;
25
import java.util.concurrent.atomic.AtomicLong;
20
 
26
 
21
import org.openconcerto.utils.convertor.NumberConvertor;
-
 
22
 
-
 
23
public class NumberUtils {
27
public class NumberUtils {
24
 
28
 
25
    /**
29
    /**
26
     * Test class and numerical equality. E.g. {@link BigDecimal#equals(Object)} also tests the
30
     * Test class and numerical equality. E.g. {@link BigDecimal#equals(Object)} also tests the
27
     * scale.
31
     * scale.
Line 213... Line 217...
213
        } else {
217
        } else {
214
            return n.doubleValue() / d;
218
            return n.doubleValue() / d;
215
        }
219
        }
216
    }
220
    }
217
 
221
 
-
 
222
    static public final int divideRoundUp(final int n, final int d) {
-
 
223
        return divideRound(n, d, RoundingMode.UP);
-
 
224
    }
-
 
225
 
-
 
226
    static public final int divideRoundDown(final int n, final int d) {
-
 
227
        return divideRound(n, d, RoundingMode.DOWN);
-
 
228
    }
-
 
229
 
-
 
230
    static public final int divideRoundCeiling(final int n, final int d) {
-
 
231
        return divideRound(n, d, RoundingMode.CEILING);
-
 
232
    }
-
 
233
 
-
 
234
    static public final int divideRoundFloor(final int n, final int d) {
-
 
235
        return divideRound(n, d, RoundingMode.FLOOR);
-
 
236
    }
-
 
237
 
-
 
238
    static final Set<RoundingMode> DIVIDE_SUPPORTED_MODES = Collections.unmodifiableSet(EnumSet.of(RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING, RoundingMode.FLOOR));
-
 
239
 
-
 
240
    static final int divideRound(final int n, final int d, final RoundingMode mode) {
-
 
241
        if (n == 0)
-
 
242
            return 0;
-
 
243
 
-
 
244
        final int mult;
-
 
245
        if (mode == RoundingMode.DOWN) {
-
 
246
            mult = 0;
-
 
247
        } else if (mode == RoundingMode.UP) {
-
 
248
            mult = signum(n) * (d - 1);
-
 
249
        } else if (mode == RoundingMode.CEILING) {
-
 
250
            // If the result is positive, behaves as for RoundingMode.UP; if negative, behaves as
-
 
251
            // for RoundingMode.DOWN
-
 
252
            mult = n > 0 ? (d - 1) : 0;
-
 
253
        } else if (mode == RoundingMode.FLOOR) {
-
 
254
            // If the result is positive, behave as for RoundingMode.DOWN; if negative, behave as
-
 
255
            // for RoundingMode.UP
-
 
256
            mult = n > 0 ? 0 : -(d - 1);
-
 
257
        } else {
-
 
258
            throw new UnsupportedOperationException();
-
 
259
        }
-
 
260
        return (n + mult) / d;
-
 
261
    }
-
 
262
 
218
    static public Number negate(Number n) {
263
    static public Number negate(Number n) {
219
        if (n == null)
264
        if (n == null)
220
            return null;
265
            return null;
221
        final Number res;
266
        final Number res;
222
        final Class<? extends Number> clazz = n.getClass();
267
        final Class<? extends Number> clazz = n.getClass();
Line 247... Line 292...
247
            res = new BigDecimal(n.toString()).negate();
292
            res = new BigDecimal(n.toString()).negate();
248
        }
293
        }
249
        return res;
294
        return res;
250
    }
295
    }
251
 
296
 
-
 
297
    static public int signum(final long l) {
-
 
298
        final int res;
-
 
299
        if (l == 0)
-
 
300
            res = 0;
-
 
301
        else if (l < 0)
-
 
302
            res = -1;
-
 
303
        else
-
 
304
            res = 1;
-
 
305
        return res;
-
 
306
    }
-
 
307
 
252
    static public int signum(Number n) {
308
    static public int signum(Number n) {
253
        if (n == null)
309
        if (n == null)
254
            throw new NullPointerException();
310
            throw new NullPointerException();
255
        final int res;
311
        final int res;
256
        final Class<? extends Number> clazz = n.getClass();
312
        final Class<? extends Number> clazz = n.getClass();
Line 261... Line 317...
261
        } else if (clazz == Double.class) {
317
        } else if (clazz == Double.class) {
262
            res = (int) Math.signum(n.doubleValue());
318
            res = (int) Math.signum(n.doubleValue());
263
        } else if (clazz == Float.class) {
319
        } else if (clazz == Float.class) {
264
            res = (int) Math.signum(n.floatValue());
320
            res = (int) Math.signum(n.floatValue());
265
        } else if (clazz == Byte.class || clazz == Short.class || clazz == Integer.class || clazz == Long.class || clazz == AtomicInteger.class || clazz == AtomicLong.class) {
321
        } else if (clazz == Byte.class || clazz == Short.class || clazz == Integer.class || clazz == Long.class || clazz == AtomicInteger.class || clazz == AtomicLong.class) {
266
            final long l = n.longValue();
322
            res = signum(n.longValue());
267
            if (l == 0)
-
 
268
                res = 0;
-
 
269
            else if (l < 0)
-
 
270
                res = -1;
-
 
271
            else
-
 
272
                res = 1;
-
 
273
        } else {
323
        } else {
274
            // limit overflow for unknown class
324
            // limit overflow for unknown class
275
            res = (int) Math.signum(n.doubleValue());
325
            res = (int) Math.signum(n.doubleValue());
276
        }
326
        }
277
        return res;
327
        return res;