-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathNumberUtils.java
More file actions
115 lines (102 loc) · 4.1 KB
/
NumberUtils.java
File metadata and controls
115 lines (102 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2016 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.util;
import java.math.BigDecimal;
import java.math.BigInteger;
/**
* Useful methods for working with {@link Number} objects.
*
* @author Curtis Rueden
* @author Barry DeZonia
*/
public final class NumberUtils {
private NumberUtils() {
// prevent instantiation of utility class
}
/**
* Converts the given object to a {@link Number} of the specified type, or
* null if the types are incompatible.
*/
public static Number toNumber(final Object value, final Class<?> type) {
final Object num = ConversionUtils.convert(value, type);
return num == null ? null : ConversionUtils.cast(num, Number.class);
}
public static BigDecimal asBigDecimal(final Number n) {
// Using .doubleValue on a long or BigInteger would cause loss of accuracy
if(BigInteger.class.isInstance(n)){
return new BigDecimal((BigInteger) n);
}
else if(Long.class.isInstance(n)){
return new BigDecimal(n.longValue());
}
return new BigDecimal(n.doubleValue());
}
public static BigInteger asBigInteger(final Number n) {
return BigInteger.valueOf(n.longValue());
}
public static Number getMinimumNumber(final Class<?> type) {
if (ClassUtils.isByte(type)) return Byte.MIN_VALUE;
if (ClassUtils.isShort(type)) return Short.MIN_VALUE;
if (ClassUtils.isInteger(type)) return Integer.MIN_VALUE;
if (ClassUtils.isLong(type)) return Long.MIN_VALUE;
if (ClassUtils.isFloat(type)) return -Float.MAX_VALUE;
if (ClassUtils.isDouble(type)) return -Double.MAX_VALUE;
return null;
}
public static Number getMaximumNumber(final Class<?> type) {
if (ClassUtils.isByte(type)) return Byte.MAX_VALUE;
if (ClassUtils.isShort(type)) return Short.MAX_VALUE;
if (ClassUtils.isInteger(type)) return Integer.MAX_VALUE;
if (ClassUtils.isLong(type)) return Long.MAX_VALUE;
if (ClassUtils.isFloat(type)) return Float.MAX_VALUE;
if (ClassUtils.isDouble(type)) return Double.MAX_VALUE;
return null;
}
public static Number getDefaultValue(final Number min, final Number max,
final Class<?> type)
{
if (min != null) return min;
if (max != null) return max;
return toNumber("0", type);
}
public static Number clampToRange(final Class<?> type, final Number value,
final Number min, final Number max)
{
if (value == null) return getDefaultValue(min, max, type);
if (Comparable.class.isAssignableFrom(type)) {
@SuppressWarnings("unchecked")
final Comparable<Number> cValue = (Comparable<Number>) value;
if (min != null && cValue.compareTo(min) < 0) return min;
if (max != null && cValue.compareTo(max) > 0) return max;
}
return value;
}
}