• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

AuthMe / ConfigMe / 6022426951

30 Aug 2023 08:26AM UTC coverage: 97.279% (+3.7%) from 93.592%
6022426951

push

github

ljacqu
Draft: collection property types (3)

528 of 546 branches covered (0.0%)

45 of 45 new or added lines in 12 files covered. (100.0%)

1466 of 1507 relevant lines covered (97.28%)

4.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.44
/src/main/java/ch/jalu/configme/properties/types/NumberType.java
1
package ch.jalu.configme.properties.types;
2

3
import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder;
4
import ch.jalu.configme.utils.Utils;
5
import ch.jalu.typeresolver.TypeInfo;
6
import ch.jalu.typeresolver.numbers.StandardNumberType;
7
import ch.jalu.typeresolver.numbers.ValueRangeComparison;
8
import ch.jalu.typeresolver.primitives.PrimitiveType;
9
import org.jetbrains.annotations.NotNull;
10
import org.jetbrains.annotations.Nullable;
11

12
import java.math.BigDecimal;
13
import java.math.BigInteger;
14

15
/**
16
 * Property type and mapper leaf type for numerical values.
17
 *
18
 * @param <N> the number type
19
 */
20
public class NumberType<N extends Number> extends PropertyAndLeafType<N> {
21

22
    /** Byte number type. */
23
    public static final NumberType<Byte> BYTE = new NumberType<>(StandardNumberType.TYPE_BYTE);
5✔
24
    /** Short number type. */
25
    public static final NumberType<Short> SHORT = new NumberType<>(StandardNumberType.TYPE_SHORT);
5✔
26
    /** Integer number type. */
27
    public static final NumberType<Integer> INTEGER = new NumberType<>(StandardNumberType.TYPE_INTEGER);
5✔
28
    /** Long number type. */
29
    public static final NumberType<Long> LONG = new NumberType<>(StandardNumberType.TYPE_LONG);
5✔
30
    /** Float number type. */
31
    public static final NumberType<Float> FLOAT = new NumberType<>(StandardNumberType.TYPE_FLOAT);
5✔
32
    /** Double number type. */
33
    public static final NumberType<Double> DOUBLE = new NumberType<>(StandardNumberType.TYPE_DOUBLE);
5✔
34
    /** BigInteger number type. */
35
    public static final NumberType<BigInteger> BIG_INTEGER = new NumberType<>(StandardNumberType.TYPE_BIG_INTEGER);
5✔
36
    /** BigDecimal number type. */
37
    public static final NumberType<BigDecimal> BIG_DECIMAL = new NumberType<>(StandardNumberType.TYPE_BIG_DECIMAL);
5✔
38

39
    /** Value after which scientific notation (like "1E+130") might be used when exporting BigDecimal values. */
40
    private static final BigDecimal BIG_DECIMAL_SCIENTIFIC_THRESHOLD = new BigDecimal("1E100");
6✔
41

42
    private final ch.jalu.typeresolver.numbers.NumberType<N> numberType;
43

44
    /**
45
     * Constructor.
46
     *
47
     * @param type jalu typresolver NumberType implementation to base the object's behavior on
48
     */
49
    protected NumberType(@NotNull ch.jalu.typeresolver.numbers.NumberType<N> type) {
50
        super(type.getType());
4✔
51
        this.numberType = type;
3✔
52
    }
1✔
53

54
    @Override
55
    public @Nullable N convert(@Nullable Object object, @NotNull ConvertErrorRecorder errorRecorder) {
56
        if (object instanceof String) {
3✔
57
            Number value = convertToNumberIfPossible((String) object);
5✔
58
            return value == null ? null : convertToType(value, errorRecorder);
9✔
59
        } else if (object instanceof Number) {
3✔
60
            return convertToType((Number) object, errorRecorder);
6✔
61
        }
62
        return null;
2✔
63
    }
64

65
    @Override
66
    public @NotNull Object toExportValue(@NotNull N value) {
67
        if (value instanceof BigDecimal) {
3✔
68
            BigDecimal bigDecimal = (BigDecimal) value;
3✔
69
            return bigDecimal.abs().compareTo(BIG_DECIMAL_SCIENTIFIC_THRESHOLD) >= 0
7✔
70
                ? bigDecimal.toString()
3✔
71
                : bigDecimal.toPlainString();
1✔
72
        } else if (value instanceof BigInteger) {
3✔
73
            return value.toString();
3✔
74
        }
75
        return value;
2✔
76
    }
77

78
    @Override
79
    public boolean canConvertToType(@NotNull TypeInfo typeInformation) {
80
        Class<?> requestedClass = PrimitiveType.toReferenceType(typeInformation.toClass());
4✔
81
        return requestedClass != null && requestedClass.isAssignableFrom(numberType.getType());
12✔
82
    }
83

84
    /**
85
     * @return the NumberType instance (from Jalu typeresolver) this object uses to convert to its number class
86
     */
87
    protected final @NotNull ch.jalu.typeresolver.numbers.NumberType<N> getTypeResolverNumberType() {
88
        return numberType;
3✔
89
    }
90

91
    /**
92
     * Converts the number to this instance's type. If the number is out of this type's bounds, an error is added to
93
     * the error recorder and the value that is the closest to the given number is returned.
94
     *
95
     * @param number the number to convert
96
     * @param errorRecorder error recorder to add errors to
97
     * @return the converted number (or closest possible value)
98
     */
99
    protected @Nullable N convertToType(@NotNull Number number, @NotNull ConvertErrorRecorder errorRecorder) {
100
        ValueRangeComparison comparison = numberType.compareToValueRange(number);
5✔
101
        if (comparison == ValueRangeComparison.WITHIN_RANGE) {
3✔
102
            return numberType.convertUnsafe(number);
6✔
103
        }
104

105
        errorRecorder.setHasError("Value cannot be represented in type (" + comparison + ")");
12✔
106
        return numberType.convertToBounds(number);
6✔
107
    }
108

109
    /**
110
     * Converts the given String to a number of appropriate type, if possible. Otherwise, null is returned.
111
     *
112
     * @param value the value to potentially convert
113
     * @return the string converted as number, null if not possible
114
     */
115
    protected @Nullable Number convertToNumberIfPossible(@NotNull String value) {
116
        try {
117
            return new BigDecimal(value);
5✔
118
        } catch (NumberFormatException ignore) {
1✔
119
            // nothing to do
120
        }
121
        return null;
2✔
122
    }
123

124
    @Override
125
    public @NotNull String toString() {
126
        return "NumberTypeHandler[" + getType().getSimpleName() + "]";
13✔
127
    }
128

129
    public ArrayPropertyType<N> arrayType() {
130
        return new ArrayPropertyType<>(this, size -> Utils.createArrayForReferenceType(getType(), size));
×
131
    }
132
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc