diff options
author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2024-03-24 13:14:11 -0500 |
---|---|---|
committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2024-03-24 13:14:11 -0500 |
commit | 26291e672b0e683edc9d57710a9a9d96ca199c45 (patch) | |
tree | df88f3d3f110e50f38b8a2752d55df4a0c777677 /src/main/java/sevenUnitsGUI/StandardDisplayRules.java | |
parent | cc45a65c78c578eb404d8773b22e5b046917621f (diff) |
Format source code & set explicit UTF-8
Diffstat (limited to 'src/main/java/sevenUnitsGUI/StandardDisplayRules.java')
-rw-r--r-- | src/main/java/sevenUnitsGUI/StandardDisplayRules.java | 58 |
1 files changed, 30 insertions, 28 deletions
diff --git a/src/main/java/sevenUnitsGUI/StandardDisplayRules.java b/src/main/java/sevenUnitsGUI/StandardDisplayRules.java index cc69d31..d00263b 100644 --- a/src/main/java/sevenUnitsGUI/StandardDisplayRules.java +++ b/src/main/java/sevenUnitsGUI/StandardDisplayRules.java @@ -46,7 +46,7 @@ public final class StandardDisplayRules { * The number of places to round to. */ private final int decimalPlaces; - + /** * @param decimalPlaces * @since 2022-04-18 @@ -54,14 +54,14 @@ public final class StandardDisplayRules { private FixedDecimals(int decimalPlaces) { this.decimalPlaces = decimalPlaces; } - + @Override public String apply(UncertainDouble t) { final var toRound = new BigDecimal(t.value()); return toRound.setScale(this.decimalPlaces, RoundingMode.HALF_EVEN) .toPlainString(); } - + /** * @return the number of decimal places this rule rounds to * @since 2022-04-18 @@ -69,7 +69,7 @@ public final class StandardDisplayRules { public int decimalPlaces() { return this.decimalPlaces; } - + @Override public boolean equals(Object obj) { if (this == obj) @@ -81,18 +81,18 @@ public final class StandardDisplayRules { return false; return true; } - + @Override public int hashCode() { return 31 + this.decimalPlaces; } - + @Override public String toString() { return "Round to " + this.decimalPlaces + " decimal places"; } } - + /** * A rule that rounds to a fixed number of significant digits. * @@ -103,12 +103,12 @@ public final class StandardDisplayRules { implements Function<UncertainDouble, String> { public static final Pattern TO_STRING_PATTERN = Pattern .compile("Round to (\\d+) significant figures"); - + /** * The number of significant figures to round to. */ private final MathContext mathContext; - + /** * @param significantFigures * @since 2022-04-18 @@ -117,13 +117,13 @@ public final class StandardDisplayRules { this.mathContext = new MathContext(significantFigures, RoundingMode.HALF_EVEN); } - + @Override public String apply(UncertainDouble t) { final var toRound = new BigDecimal(t.value()); return toRound.round(this.mathContext).toString(); } - + @Override public boolean equals(Object obj) { if (this == obj) @@ -138,13 +138,13 @@ public final class StandardDisplayRules { return false; return true; } - + @Override public int hashCode() { return 127 + (this.mathContext == null ? 0 : this.mathContext.hashCode()); } - + /** * @return the number of significant figures this rule rounds to * @since 2022-04-18 @@ -152,14 +152,14 @@ public final class StandardDisplayRules { public int significantFigures() { return this.mathContext.getPrecision(); } - + @Override public String toString() { return "Round to " + this.mathContext.getPrecision() + " significant figures"; } } - + /** * A rounding rule that rounds based on UncertainDouble's toString method. * This means the output will have around as many significant figures as the @@ -170,25 +170,26 @@ public final class StandardDisplayRules { */ public static final class UncertaintyBased implements Function<UncertainDouble, String> { - private UncertaintyBased() {} - + private UncertaintyBased() { + } + @Override public String apply(UncertainDouble t) { return t.toString(false, RoundingMode.HALF_EVEN); } - + @Override public String toString() { return "Uncertainty-Based Rounding"; } } - + /** * For now, I want this to be a singleton. I might want to add a parameter * later, so I won't make it an enum. */ private static final UncertaintyBased UNCERTAINTY_BASED_ROUNDING_RULE = new UncertaintyBased(); - + /** * @param decimalPlaces decimal places to round to * @return a rounding rule that rounds to fixed number of decimal places @@ -198,7 +199,7 @@ public final class StandardDisplayRules { public static final FixedDecimals fixedDecimals(int decimalPlaces) { return new FixedDecimals(decimalPlaces); } - + /** * @param significantFigures significant figures to round to * @return a rounding rule that rounds to a fixed number of significant @@ -209,7 +210,7 @@ public final class StandardDisplayRules { public static final FixedPrecision fixedPrecision(int significantFigures) { return new FixedPrecision(significantFigures); } - + /** * Gets one of the standard rules from its string representation. * @@ -224,23 +225,23 @@ public final class StandardDisplayRules { String ruleToString) { if (UNCERTAINTY_BASED_ROUNDING_RULE.toString().equals(ruleToString)) return UNCERTAINTY_BASED_ROUNDING_RULE; - + // test if it is a fixed-places rule final var placesMatch = FixedDecimals.TO_STRING_PATTERN .matcher(ruleToString); if (placesMatch.matches()) return new FixedDecimals(Integer.valueOf(placesMatch.group(1))); - + // test if it is a fixed-sig-fig rule final var sigFigMatch = FixedPrecision.TO_STRING_PATTERN .matcher(ruleToString); if (sigFigMatch.matches()) return new FixedPrecision(Integer.valueOf(sigFigMatch.group(1))); - + throw new IllegalArgumentException( "Provided string does not match any given rules."); } - + /** * @return an UncertainDouble-based rounding rule * @since v0.4.0 @@ -249,6 +250,7 @@ public final class StandardDisplayRules { public static final UncertaintyBased uncertaintyBased() { return UNCERTAINTY_BASED_ROUNDING_RULE; } - - private StandardDisplayRules() {} + + private StandardDisplayRules() { + } } |