summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnitsGUI/Presenter.java
diff options
context:
space:
mode:
authorAdrien Hopkins <ahopk127@my.yorku.ca>2022-07-08 10:31:06 -0500
committerAdrien Hopkins <ahopk127@my.yorku.ca>2022-07-08 10:31:06 -0500
commitb80d16bfd2dca34733e5cb37283df93d10805512 (patch)
treedc90cf748a4d754e56ca16845e4b4358dd9bd931 /src/main/java/sevenUnitsGUI/Presenter.java
parent768468b303627c215046cd10cf9e8ba62151ac65 (diff)
parenteba6fb3778b07b10ee321310991d375202f4c9d2 (diff)
Merge branch 'feature-search-settings' into develop
Diffstat (limited to 'src/main/java/sevenUnitsGUI/Presenter.java')
-rw-r--r--src/main/java/sevenUnitsGUI/Presenter.java77
1 files changed, 73 insertions, 4 deletions
diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java
index 4feea44..a5c4d48 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,13 @@ 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 Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>> searchRule = PrefixSearchRule.NO_PREFIXES;
+
+ /**
* 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 +283,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.
*
@@ -490,6 +518,23 @@ public final class Presenter {
}
/**
+ * @return the rule that determines which units are prefixed
+ * @since 2022-07-08
+ */
+ public Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>> getSearchRule() {
+ return this.searchRule;
+ }
+
+ /**
+ * @return a search rule that shows all single prefixes
+ * @since 2022-07-08
+ */
+ public Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>> getUniversalSearchRule() {
+ return PrefixSearchRule.getCoherentOnlyRule(
+ new HashSet<>(this.database.prefixMap(true).values()));
+ }
+
+ /**
* @return the view associated with this presenter
* @since 2022-04-19
*/
@@ -551,6 +596,16 @@ public final class Presenter {
case "include_duplicates":
this.showDuplicates = Boolean.valueOf(value);
break;
+ case "search_prefix_rule":
+ if (PrefixSearchRule.NO_PREFIXES.toString().equals(value)) {
+ this.searchRule = PrefixSearchRule.NO_PREFIXES;
+ } else if (PrefixSearchRule.COMMON_PREFIXES.toString()
+ .equals(value)) {
+ this.searchRule = PrefixSearchRule.COMMON_PREFIXES;
+ } else {
+ this.searchRule = this.getUniversalSearchRule();
+ }
+ break;
default:
System.err.printf("Warning: unrecognized setting \"%s\".%n",
param);
@@ -628,6 +683,8 @@ public final class Presenter {
String.format("one_way=%s\n", this.oneWayConversionEnabled));
writer.write(
String.format("include_duplicates=%s\n", this.showDuplicates));
+ writer.write(
+ String.format("search_prefix_rule=%s\n", this.searchRule));
} catch (final IOException e) {
e.printStackTrace();
this.view.showErrorMessage("I/O Error",
@@ -680,6 +737,18 @@ public final class Presenter {
}
/**
+ * @param searchRule 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.
+ * @since 2022-07-08
+ */
+ public void setSearchRule(
+ Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>> searchRule) {
+ this.searchRule = searchRule;
+ }
+
+ /**
* @param showDuplicateUnits whether or not duplicate units should be shown
* @since 2022-03-30
*/
@@ -761,10 +830,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()));
}
}