2424import java .math .BigDecimal ;
2525import java .math .BigInteger ;
2626import java .math .MathContext ;
27+ import java .util .Objects ;
2728
2829import static java .util .Objects .requireNonNull ;
2930
@@ -54,7 +55,7 @@ public abstract class Decimal<T extends Decimal<T>> {
5455 this .bigDecimal = requireNonNull (bigDecimal , "bigDecimal" );
5556 if (bigDecimal .compareTo (BigDecimal .ZERO ) != 0 && this .signum != bigDecimal .signum ()) {
5657 throw new IllegalArgumentException ("Signum value not consistent with big decimal value, was: "
57- + signum + ", expected: " + bigDecimal .signum ());
58+ + signum + ", expected: " + bigDecimal .signum ());
5859 }
5960 }
6061
@@ -68,7 +69,7 @@ public abstract class Decimal<T extends Decimal<T>> {
6869 public final BigDecimal toBigDecimal () {
6970 if (type != DecimalType .FINITE ) {
7071 throw new DecimalInconvertibleException (
71- "Value " + toString () + " cannot be converted to a BigDecimal" , type , signum );
72+ "Value " + this + " cannot be converted to a BigDecimal" , type , signum );
7273 }
7374 return bigDecimal ;
7475 }
@@ -86,21 +87,12 @@ public final BigDecimal toBigDecimal() {
8687 * @return this decimal converted to a {@code double}
8788 */
8889 public final double doubleValue () {
89- switch (type ) {
90- case FINITE :
91- return bigDecimal .doubleValue ();
92-
93- case INFINITY :
94- return signum == Signum .NEGATIVE ? Double .NEGATIVE_INFINITY : Double .POSITIVE_INFINITY ;
95-
96- case NAN :
97- case SIGNALING_NAN :
90+ return switch (type ) {
91+ case FINITE -> bigDecimal .doubleValue ();
92+ case INFINITY -> signum == Signum .NEGATIVE ? Double .NEGATIVE_INFINITY : Double .POSITIVE_INFINITY ;
9893 // No differentiation between positive/negative and signaling/normal
99- return Double .NaN ;
100-
101- default :
102- throw new IllegalStateException ("Unsupported DecimalType " + type );
103- }
94+ case NAN , SIGNALING_NAN -> Double .NaN ;
95+ };
10496 }
10597
10698 /**
@@ -174,7 +166,7 @@ final int signum() {
174166 */
175167 final boolean isEquivalentToZero () {
176168 return type == DecimalType .FINITE
177- && BigDecimal .ZERO .compareTo (bigDecimal ) == 0 ;
169+ && BigDecimal .ZERO .compareTo (bigDecimal ) == 0 ;
178170 }
179171
180172 /**
@@ -202,37 +194,29 @@ final T negate() {
202194
203195 @ Override
204196 public final String toString () {
205- switch (type ) {
206- case FINITE :
207- if (signum == Signum .NEGATIVE && isEquivalentToZero ()) {
208- return "-" + bigDecimal .toString ();
197+ return switch (type ) {
198+ case FINITE -> {
199+ if (signum == Signum .NEGATIVE && isEquivalentToZero ()) {
200+ yield "-" + bigDecimal .toString ();
201+ }
202+ yield bigDecimal .toString ();
209203 }
210- return bigDecimal .toString ();
211-
212- case INFINITY :
213- return signum == Signum .NEGATIVE ? "-Infinity" : "+Infinity" ;
214-
215- case NAN :
216- return signum == Signum .NEGATIVE ? "-NaN" : "+NaN" ;
217-
218- case SIGNALING_NAN :
219- return signum == Signum .NEGATIVE ? "-sNaN" : "+sNaN" ;
220-
221- default :
222- throw new IllegalStateException ("Unsupported DecimalType " + type );
223- }
204+ case INFINITY -> signum == Signum .NEGATIVE ? "-Infinity" : "+Infinity" ;
205+ case NAN -> signum == Signum .NEGATIVE ? "-NaN" : "+NaN" ;
206+ case SIGNALING_NAN -> signum == Signum .NEGATIVE ? "-sNaN" : "+sNaN" ;
207+ };
224208 }
225209
226210 @ Override
227211 public final boolean equals (Object o ) {
228212 if (this == o ) return true ;
229213 if (o == null || getClass () != o .getClass ()) return false ;
230214
231- Decimal decimal = (Decimal ) o ;
215+ Decimal <?> decimal = (Decimal <?> ) o ;
232216
233217 if (signum != decimal .signum ) return false ;
234218 if (type != decimal .type ) return false ;
235- return bigDecimal != null ? bigDecimal .equals (decimal . bigDecimal ) : decimal .bigDecimal == null ;
219+ return Objects .equals (bigDecimal , decimal .bigDecimal ) ;
236220 }
237221
238222 @ Override
@@ -336,10 +320,11 @@ final T valueOf(BigInteger value, OverflowHandling overflowHandling) {
336320 /**
337321 * Creates a decimal from {@code value}, rejecting values that would lose precision due to rounding.
338322 *
339- * @param value Big integer value to convert
323+ * @param value
324+ * Big integer value to convert
325+ * @return Decimal equivalent
340326 * @throws DecimalOverflowException
341327 * If the value is out of range.
342- * @return Decimal equivalent
343328 * @see #valueOf(BigInteger, OverflowHandling)
344329 */
345330 final T valueOfExact (BigInteger value ) {
@@ -353,7 +338,8 @@ final T valueOfExact(BigInteger value) {
353338 * {@code Double.NaN} is mapped to positive NaN, the infinities to their equivalent +/- infinity.
354339 * </p>
355340 * <p>
356- * For normal, finite, values, this is equivalent to {@code valueOf(BigDecimal.valueOf(value), overflowHandling)}.
341+ * For normal, finite, values, this is equivalent to
342+ * {@code valueOf(BigDecimal.valueOf(value), overflowHandling)}.
357343 * </p>
358344 *
359345 * @param value
@@ -379,8 +365,8 @@ final T valueOf(double value, OverflowHandling overflowHandling) {
379365 /**
380366 * Converts a decimal to this type.
381367 * <p>
382- * For normal, finite, decimals, this behaves like {@code valueOf(decimal.toBigDecimal(), overflowHandling)}, see
383- * {@link #valueOf(BigDecimal, OverflowHandling)}.
368+ * For normal, finite, decimals, this behaves like {@code valueOf(decimal.toBigDecimal(), overflowHandling)},
369+ * see {@link #valueOf(BigDecimal, OverflowHandling)}.
384370 * </p>
385371 *
386372 * @param decimal
@@ -429,73 +415,48 @@ final T valueOf(String value, OverflowHandling overflowHandling) {
429415 checkChar = value .charAt (1 );
430416 }
431417 if (checkChar == 'i' || checkChar == 'I'
432- || checkChar == 'n' || checkChar == 'N'
433- || checkChar == 's' || checkChar == 'S' ) {
418+ || checkChar == 'n' || checkChar == 'N'
419+ || checkChar == 's' || checkChar == 'S' ) {
434420 return valueOfSpecial (value );
435421 }
436422 }
437423 BigDecimal bdValue = new BigDecimal (value , getMathContext ());
438424 T decimalValue = valueOf (bdValue , overflowHandling );
439425 if (decimalValue .isEquivalentToZero ()
440- && value .charAt (0 ) == '-'
441- && bdValue .signum () != Signum .NEGATIVE ) {
426+ && value .charAt (0 ) == '-'
427+ && bdValue .signum () != Signum .NEGATIVE ) {
442428 return decimalValue .negate ();
443429 }
444430 return decimalValue ;
445431 }
446432
447433 private T valueOfSpecial (String special ) {
448- switch (special .toLowerCase ()) {
449- case "inf" :
450- case "infinity" :
451- case "+inf" :
452- case "+infinity" :
453- return getSpecialConstant (Signum .POSITIVE , DecimalType .INFINITY );
454-
455- case "-inf" :
456- case "-infinity" :
457- return getSpecialConstant (Signum .NEGATIVE , DecimalType .INFINITY );
458-
459- case "nan" :
460- case "+nan" :
461- return getSpecialConstant (Signum .POSITIVE , DecimalType .NAN );
462-
463- case "-nan" :
464- return getSpecialConstant (Signum .NEGATIVE , DecimalType .NAN );
465-
466- case "snan" :
467- case "+snan" :
468- return getSpecialConstant (Signum .POSITIVE , DecimalType .SIGNALING_NAN );
469-
470- case "-snan" :
471- return getSpecialConstant (Signum .NEGATIVE , DecimalType .SIGNALING_NAN );
472-
473- default :
474- throw new NumberFormatException ("Invalid value " + special );
475- }
434+ return switch (special .toLowerCase ()) {
435+ case "inf" , "infinity" , "+inf" , "+infinity" ->
436+ getSpecialConstant (Signum .POSITIVE , DecimalType .INFINITY );
437+ case "-inf" , "-infinity" -> getSpecialConstant (Signum .NEGATIVE , DecimalType .INFINITY );
438+ case "nan" , "+nan" -> getSpecialConstant (Signum .POSITIVE , DecimalType .NAN );
439+ case "-nan" -> getSpecialConstant (Signum .NEGATIVE , DecimalType .NAN );
440+ case "snan" , "+snan" -> getSpecialConstant (Signum .POSITIVE , DecimalType .SIGNALING_NAN );
441+ case "-snan" -> getSpecialConstant (Signum .NEGATIVE , DecimalType .SIGNALING_NAN );
442+ default -> throw new NumberFormatException ("Invalid value " + special );
443+ };
476444 }
477445
478446 @ Override
479447 public final T getSpecialConstant (int signum , DecimalType decimalType ) {
480- switch (decimalType ) {
481- case INFINITY :
482- return signum == Signum .NEGATIVE
448+ return switch (decimalType ) {
449+ case INFINITY -> signum == Signum .NEGATIVE
483450 ? negativeInfinity
484451 : positiveInfinity ;
485-
486- case NAN :
487- return signum == Signum .NEGATIVE
452+ case NAN -> signum == Signum .NEGATIVE
488453 ? negativeNan
489454 : positiveNan ;
490-
491- case SIGNALING_NAN :
492- return signum == Signum .NEGATIVE
455+ case SIGNALING_NAN -> signum == Signum .NEGATIVE
493456 ? negativeSignalingNaN
494457 : positiveSignalingNaN ;
495-
496- default :
497- throw new AssertionError ("Invalid special value for decimalType " + decimalType );
498- }
458+ default -> throw new AssertionError ("Invalid special value for decimalType " + decimalType );
459+ };
499460 }
500461
501462 }
0 commit comments