From e7c6cf33670548f1b1650278114530b2abcbaae9 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Wed, 6 Jul 2022 16:23:09 -0500 Subject: Added the ability to make search rules --- src/main/java/sevenUnitsGUI/Presenter.java | 37 ++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'src/main/java/sevenUnitsGUI/Presenter.java') 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; @@ -194,6 +195,14 @@ public final class Presenter { */ private Predicate> 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> 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 @@ -274,6 +283,26 @@ public final class Presenter { .filter(isFullBase).count()); } + /** + * 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> applySearchRule( + Map.Entry e) { + final Unit u = e.getValue(); + if (u instanceof LinearUnit) { + final String name = e.getKey(); + final Map.Entry 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())); } } -- cgit v1.2.3