summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnitsGUI/Presenter.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/sevenUnitsGUI/Presenter.java')
-rw-r--r--src/main/java/sevenUnitsGUI/Presenter.java37
1 files changed, 33 insertions, 4 deletions
diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java
index 4feea44..f630a77 100644
--- a/src/main/java/sevenUnitsGUI/Presenter.java
+++ b/src/main/java/sevenUnitsGUI/Presenter.java
@@ -32,6 +32,7 @@ import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
import sevenUnits.ProgramInfo;
import sevenUnits.unit.BaseDimension;
@@ -195,6 +196,14 @@ public final class Presenter {
private Predicate<List<UnitPrefix>> prefixRepetitionRule = DefaultPrefixRepetitionRule.NO_RESTRICTION;
/**
+ * A rule that accepts a prefixless name-unit pair and returns a map mapping
+ * names to prefixed versions of that unit (including the unit itself) that
+ * should be searchable.
+ */
+ private final Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>> searchRule = e -> Map
+ .ofEntries(e);
+
+ /**
* The set of units that is considered neither metric nor nonmetric for the
* purposes of the metric-imperial one-way conversion. These units are
* included in both From and To, even if One Way Conversion is enabled.
@@ -275,6 +284,26 @@ public final class Presenter {
}
/**
+ * Applies a search rule to an entry in a name-unit map.
+ *
+ * @param e entry
+ * @return stream of entries, ready for flat-mapping
+ * @since 2022-07-06
+ */
+ private final Stream<Map.Entry<String, Unit>> applySearchRule(
+ Map.Entry<String, Unit> e) {
+ final Unit u = e.getValue();
+ if (u instanceof LinearUnit) {
+ final String name = e.getKey();
+ final Map.Entry<String, LinearUnit> linearEntry = Map.entry(name,
+ (LinearUnit) u);
+ return this.searchRule.apply(linearEntry).entrySet().stream().map(
+ entry -> Map.entry(entry.getKey(), (Unit) entry.getValue()));
+ } else
+ return Stream.of(e);
+ }
+
+ /**
* Converts from the view's input expression to its output expression.
* Displays an error message if any of the required fields are invalid.
*
@@ -761,10 +790,10 @@ public final class Presenter {
}
// set unit names
- ucview.setFromUnitNames(
- fromUnits.map(Map.Entry::getKey).collect(Collectors.toSet()));
- ucview.setToUnitNames(
- toUnits.map(Map.Entry::getKey).collect(Collectors.toSet()));
+ ucview.setFromUnitNames(fromUnits.flatMap(this::applySearchRule)
+ .map(Map.Entry::getKey).collect(Collectors.toSet()));
+ ucview.setToUnitNames(toUnits.flatMap(this::applySearchRule)
+ .map(Map.Entry::getKey).collect(Collectors.toSet()));
}
}