summaryrefslogtreecommitdiff
path: root/src/org/unitConverter/converterGUI
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/unitConverter/converterGUI')
-rw-r--r--src/org/unitConverter/converterGUI/DefaultPrefixRepetitionRule.java42
-rw-r--r--src/org/unitConverter/converterGUI/UnitConverterGUI.java61
2 files changed, 60 insertions, 43 deletions
diff --git a/src/org/unitConverter/converterGUI/DefaultPrefixRepetitionRule.java b/src/org/unitConverter/converterGUI/DefaultPrefixRepetitionRule.java
new file mode 100644
index 0000000..34d8467
--- /dev/null
+++ b/src/org/unitConverter/converterGUI/DefaultPrefixRepetitionRule.java
@@ -0,0 +1,42 @@
+/**
+ * @since 2020-08-26
+ */
+package org.unitConverter.converterGUI;
+
+import java.util.List;
+import java.util.function.Predicate;
+
+import org.unitConverter.unit.UnitPrefix;
+
+/**
+ * A rule that specifies whether prefix repetition is allowed
+ *
+ * @since 2020-08-26
+ */
+enum DefaultPrefixRepetitionRule implements Predicate<List<UnitPrefix>> {
+ NO_REPETITION {
+ @Override
+ public boolean test(List<UnitPrefix> prefixes) {
+ return prefixes.size() <= 1;
+ }
+ },
+ NO_RESTRICTION {
+ @Override
+ public boolean test(List<UnitPrefix> prefixes) {
+ return true;
+ }
+ },
+ /**
+ * You are allowed to have any number of Yotta/Yocto followed by possibly one
+ * Kilo-Zetta/Milli-Zepto followed by possibly one Deca/Hecto. Same for
+ * reducing prefixes, don't mix magnifying and reducing. Non-metric
+ * (including binary) prefixes can't be repeated.
+ */
+ COMPLEX_REPETITION {
+ @Override
+ public boolean test(List<UnitPrefix> prefixes) {
+ // TODO method stub
+ return false;
+ }
+ };
+}
diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java
index f7c3479..eff0c47 100644
--- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java
+++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java
@@ -149,7 +149,8 @@ final class UnitConverterGUI {
this.view = view;
// load initial units
- this.database = new UnitDatabase();
+ this.database = new UnitDatabase(
+ DefaultPrefixRepetitionRule.NO_RESTRICTION);
Presenter.addDefaults(this.database);
this.database.loadUnitsFile(new File("unitsfile.txt"));
@@ -308,7 +309,6 @@ final class UnitConverterGUI {
this.view.setExpressionConverterOutputText((useSlash ? "1 / " : "")
+ String.format("%s = %s", fromUnitString,
this.getRoundedString(converted, false)));
- final String toString = this.getRoundedString(converted, false);
return;
} else {
// convert to UnitValue
@@ -347,45 +347,6 @@ final class UnitConverterGUI {
}
/**
- * @param value value to round
- * @return string of that value rounded to {@code significantDigits}
- * significant digits.
- * @since 2019-04-14
- * @since v0.2.0
- */
- private final String getRoundedString(final BigDecimal value) {
- // round value based on rounding type
- final BigDecimal roundedValue;
- switch (this.roundingType) {
- case DECIMAL_PLACES:
- roundedValue = value.setScale(this.precision,
- RoundingMode.HALF_EVEN);
- break;
- case SCIENTIFIC:
- throw new UnsupportedOperationException("Not yet implemented.");
- case SIGNIFICANT_DIGITS:
- roundedValue = value.round(new MathContext(this.precision));
- break;
- default:
- throw new AssertionError("Invalid switch condition.");
- }
-
- String output = roundedValue.toString();
-
- // remove trailing zeroes
- if (output.contains(".")) {
- while (output.endsWith("0")) {
- output = output.substring(0, output.length() - 1);
- }
- if (output.endsWith(".")) {
- output = output.substring(0, output.length() - 1);
- }
- }
-
- return output;
- }
-
- /**
* Like {@link LinearUnitValue#toString(boolean)}, but obeys this unit
* converter's rounding settings.
*
@@ -414,6 +375,7 @@ final class UnitConverterGUI {
final BigDecimal unrounded = new BigDecimal(value.getValue());
final BigDecimal rounded;
int precision = this.precision;
+
switch (this.roundingType) {
case DECIMAL_PLACES:
rounded = unrounded.setScale(precision, RoundingMode.HALF_EVEN);
@@ -484,6 +446,15 @@ final class UnitConverterGUI {
}
/**
+ * @param prefixRepetitionRule the prefixRepetitionRule to set
+ * @since 2020-08-26
+ */
+ public void setPrefixRepetitionRule(
+ Predicate<List<UnitPrefix>> prefixRepetitionRule) {
+ this.database.setPrefixRepetitionRule(prefixRepetitionRule);
+ }
+
+ /**
* @param roundingType the roundingType to set
* @since 2020-07-16
*/
@@ -1031,7 +1002,9 @@ final class UnitConverterGUI {
final JRadioButton noRepetition = new JRadioButton(
"No Repetition");
- noRepetition.setEnabled(false);
+ noRepetition.addActionListener(
+ e -> this.presenter.setPrefixRepetitionRule(
+ DefaultPrefixRepetitionRule.NO_REPETITION));
prefixRuleButtons.add(noRepetition);
prefixRepetitionPanel.add(noRepetition,
new GridBagBuilder(0, 0)
@@ -1041,7 +1014,9 @@ final class UnitConverterGUI {
final JRadioButton noRestriction = new JRadioButton(
"No Restriction");
noRestriction.setSelected(true);
- noRestriction.setEnabled(false);
+ noRestriction.addActionListener(
+ e -> this.presenter.setPrefixRepetitionRule(
+ DefaultPrefixRepetitionRule.NO_RESTRICTION));
prefixRuleButtons.add(noRestriction);
prefixRepetitionPanel.add(noRestriction,
new GridBagBuilder(0, 1)