summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnitsGUI/StandardDisplayRules.java
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2024-03-24 13:25:22 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2024-03-24 13:25:22 -0500
commited53492243ecad8d975401a97f5b634328ad2c71 (patch)
tree8a744f46320710355a02c9b2c371602ce69aefec /src/main/java/sevenUnitsGUI/StandardDisplayRules.java
parentc878761f737c90fc3fa1caedd48e2ee01637108f (diff)
parent91d51c3c49c4c0877483220ac0f12db4efab8f60 (diff)
Release version 0.5.0 (merge into stable)
Diffstat (limited to 'src/main/java/sevenUnitsGUI/StandardDisplayRules.java')
-rw-r--r--src/main/java/sevenUnitsGUI/StandardDisplayRules.java58
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() {
+ }
}