summaryrefslogtreecommitdiff
path: root/src/org/unitConverter/converterGUI
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2020-08-27 07:06:35 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2020-08-27 07:06:35 -0500
commit0245594222bfa0bd9a47d8326ed323c7356ac27c (patch)
tree73134bb44ace529cdfa37f745ab2e4cf128c44ca /src/org/unitConverter/converterGUI
parent6d7d172e2e706da44c2b30177a04648671aad69e (diff)
Added Complex Repetition.
Diffstat (limited to 'src/org/unitConverter/converterGUI')
-rw-r--r--src/org/unitConverter/converterGUI/DefaultPrefixRepetitionRule.java57
-rw-r--r--src/org/unitConverter/converterGUI/UnitConverterGUI.java6
2 files changed, 59 insertions, 4 deletions
diff --git a/src/org/unitConverter/converterGUI/DefaultPrefixRepetitionRule.java b/src/org/unitConverter/converterGUI/DefaultPrefixRepetitionRule.java
index 34d8467..bdc3a2e 100644
--- a/src/org/unitConverter/converterGUI/DefaultPrefixRepetitionRule.java
+++ b/src/org/unitConverter/converterGUI/DefaultPrefixRepetitionRule.java
@@ -6,6 +6,7 @@ package org.unitConverter.converterGUI;
import java.util.List;
import java.util.function.Predicate;
+import org.unitConverter.unit.SI;
import org.unitConverter.unit.UnitPrefix;
/**
@@ -35,8 +36,60 @@ enum DefaultPrefixRepetitionRule implements Predicate<List<UnitPrefix>> {
COMPLEX_REPETITION {
@Override
public boolean test(List<UnitPrefix> prefixes) {
- // TODO method stub
- return false;
+ // determine whether we are magnifying or reducing
+ final boolean magnifying;
+ if (prefixes.isEmpty())
+ return true;
+ else if (prefixes.get(0).getMultiplier() > 1) {
+ magnifying = true;
+ } else {
+ magnifying = false;
+ }
+
+ // if the first prefix is non-metric (including binary prefixes),
+ // assume we are using non-metric prefixes
+ // non-metric prefixes are allowed, but can't be repeated.
+ if (!SI.DECIMAL_PREFIXES.contains(prefixes.get(0)))
+ return NO_REPETITION.test(prefixes);
+
+ int part = 0; // 0=yotta/yoctos, 1=kilo-zetta/milli-zepto,
+ // 2=deka,hecto,deci,centi
+
+ for (final UnitPrefix prefix : prefixes) {
+ // check that the current prefix is metric and appropriately
+ // magnifying/reducing
+ if (!SI.DECIMAL_PREFIXES.contains(prefix))
+ return false;
+ if (magnifying != prefix.getMultiplier() > 1)
+ return false;
+
+ // check if the current prefix is correct
+ // since part is set *after* this check, part designates the state
+ // of the *previous* prefix
+ switch (part) {
+ case 0:
+ // do nothing, any prefix is valid after a yotta
+ break;
+ case 1:
+ // after a kilo-zetta, only deka/hecto are valid
+ if (SI.THOUSAND_PREFIXES.contains(prefix))
+ return false;
+ break;
+ case 2:
+ // deka/hecto must be the last prefix, so this is always invalid
+ return false;
+ }
+
+ // set part
+ if (SI.YOTTA.equals(prefix) || SI.YOCTO.equals(prefix)) {
+ part = 0;
+ } else if (SI.THOUSAND_PREFIXES.contains(prefix)) {
+ part = 1;
+ } else {
+ part = 2;
+ }
+ }
+ return true;
}
};
}
diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java
index eff0c47..5fe4ee5 100644
--- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java
+++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java
@@ -1024,8 +1024,10 @@ final class UnitConverterGUI {
.build());
final JRadioButton customRepetition = new JRadioButton(
- "Custom Repetition Rule");
- customRepetition.setEnabled(false);
+ "Complex Repetition");
+ customRepetition.addActionListener(
+ e -> this.presenter.setPrefixRepetitionRule(
+ DefaultPrefixRepetitionRule.COMPLEX_REPETITION));
prefixRuleButtons.add(customRepetition);
prefixRepetitionPanel.add(customRepetition,
new GridBagBuilder(0, 2)