-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberError.java
More file actions
127 lines (112 loc) · 4.16 KB
/
NumberError.java
File metadata and controls
127 lines (112 loc) · 4.16 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
116
117
118
119
120
121
122
123
124
125
126
127
package preciseValues.errorModel;
import java.util.Objects;
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* TODO: finish this javadoc
*/
public class NumberError {
public static final NumberError NON_NULL_ERROR = new NumberError();
private static final BigDecimal
UNDEFINED_ERROR_VALUE = new BigDecimal(0),
NON_NULL_AVERAGE_VALUE = new BigDecimal(0);
private final ErrorType errorType;
private final BigDecimal errorValue;
/**
* Creates a blank NumberError object.
* (Intended for cases where the error is not defined)
*/
public NumberError() {
this(null, null);
}
/**
* Creates a new NumberError object.
*
* @param errorType Type of the error.
* @param errorValue Value of the error.
*/
public NumberError(@Nullable ErrorType errorType, @Nullable BigDecimal errorValue) {
this.errorType = Objects.requireNonNullElse(errorType, ErrorType.UNDEFINED);
this.errorValue = Objects.requireNonNullElse(errorValue, UNDEFINED_ERROR_VALUE);
}
/**
* Gets the type of the error.
* (This method is intended for use in output)
*
* @return Error type.
*/
@SuppressWarnings("unused")
public final @NotNull ErrorType getErrorType() {
return errorType;
}
/**
* Gets the absolute value component of the error.
* (This method is intended for use in output)
*
* @return Error value.
*/
@SuppressWarnings("unused")
public final @NotNull BigDecimal getErrorValue() {
return errorValue;
}
/**
* TODO: finish this javadoc
*
* @param errorType Type of error to return.
* @param averageValue Average value for reference.
*
* @return Error of the desired type.
*/
public final @NotNull NumberError getError(
@Nullable ErrorType errorType,
@Nullable BigDecimal averageValue) {
return switch (Objects.requireNonNullElse(errorType, ErrorType.UNDEFINED)) {
case ABSOLUTE -> getAbsoluteError(averageValue);
case RELATIVE -> getRelativeError(averageValue);
default -> NON_NULL_ERROR;
};
}
private @NotNull NumberError getAbsoluteError(@Nullable BigDecimal averageValue) {
return new NumberError(ErrorType.ABSOLUTE, getAbsoluteErrorValue(averageValue));
}
private @NotNull BigDecimal getAbsoluteErrorValue(@Nullable BigDecimal averageValue) {
return switch (errorType) {
case ABSOLUTE -> errorValue;
case RELATIVE -> getAbsoluteValueFromRelativeValue(
errorValue,
Objects.requireNonNullElse(averageValue, NON_NULL_AVERAGE_VALUE));
default -> UNDEFINED_ERROR_VALUE;
};
}
private static @NotNull BigDecimal getAbsoluteValueFromRelativeValue(
@NotNull BigDecimal errorValue,
@NotNull BigDecimal averageValue) {
return averageValue.multiply(errorValue);
}
private @NotNull NumberError getRelativeError(@Nullable BigDecimal averageValue) {
return new NumberError(ErrorType.RELATIVE, getRelativeErrorValue(averageValue));
}
private @NotNull BigDecimal getRelativeErrorValue(@Nullable BigDecimal averageValue) {
return switch (errorType) {
case ABSOLUTE -> getRelativeValueFromAbsoluteValue(
errorValue,
Objects.requireNonNullElse(averageValue, NON_NULL_AVERAGE_VALUE));
case RELATIVE -> errorValue;
default -> UNDEFINED_ERROR_VALUE;
};
}
private static @NotNull BigDecimal getRelativeValueFromAbsoluteValue(
@NotNull BigDecimal errorValue,
@NotNull BigDecimal averageValue) {
BigDecimal result;
try {
result = errorValue.divide(averageValue, RoundingMode.HALF_UP);
} catch (ArithmeticException e) {
System.err.println("BigDecimal - trying to divide by zero or insufficient scaling.");
result = UNDEFINED_ERROR_VALUE;
}
return result;
}
}