From 6d7d172e2e706da44c2b30177a04648671aad69e Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Wed, 26 Aug 2020 12:37:12 -0500 Subject: Added the prefix repetition rule, and the two basic rules. --- .../converterGUI/DefaultPrefixRepetitionRule.java | 42 +++++++++++++++ .../converterGUI/UnitConverterGUI.java | 61 +++++++--------------- 2 files changed, 60 insertions(+), 43 deletions(-) create mode 100644 src/org/unitConverter/converterGUI/DefaultPrefixRepetitionRule.java (limited to 'src/org/unitConverter/converterGUI') 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> { + NO_REPETITION { + @Override + public boolean test(List prefixes) { + return prefixes.size() <= 1; + } + }, + NO_RESTRICTION { + @Override + public boolean test(List 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 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 @@ -346,45 +346,6 @@ final class UnitConverterGUI { return this.prefixNameComparator; } - /** - * @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); @@ -483,6 +445,15 @@ final class UnitConverterGUI { this.precision = precision; } + /** + * @param prefixRepetitionRule the prefixRepetitionRule to set + * @since 2020-08-26 + */ + public void setPrefixRepetitionRule( + Predicate> 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) -- cgit v1.2.3