diff options
author | Adrien Hopkins <ahopk127@my.yorku.ca> | 2022-07-06 16:53:44 -0500 |
---|---|---|
committer | Adrien Hopkins <ahopk127@my.yorku.ca> | 2022-07-06 16:53:44 -0500 |
commit | 60027af631f071aa12f7d2afb77179464dc69d0b (patch) | |
tree | 192210035e0d6c1bc8cb9f291f9bedef23e3ff88 /src | |
parent | e7c6cf33670548f1b1650278114530b2abcbaae9 (diff) |
Added some standard prefix search rules
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/sevenUnitsGUI/PrefixSearchRule.java | 172 | ||||
-rw-r--r-- | src/main/java/sevenUnitsGUI/Presenter.java | 5 | ||||
-rw-r--r-- | src/main/resources/unitsfile.txt | 6 |
3 files changed, 174 insertions, 9 deletions
diff --git a/src/main/java/sevenUnitsGUI/PrefixSearchRule.java b/src/main/java/sevenUnitsGUI/PrefixSearchRule.java new file mode 100644 index 0000000..2928082 --- /dev/null +++ b/src/main/java/sevenUnitsGUI/PrefixSearchRule.java @@ -0,0 +1,172 @@ +/** + * Copyright (C) 2022 Adrien Hopkins + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +package sevenUnitsGUI; + +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.function.Function; +import java.util.function.Predicate; + +import sevenUnits.unit.LinearUnit; +import sevenUnits.unit.Metric; +import sevenUnits.unit.UnitPrefix; + +/** + * A search rule that applies a certain set of prefixes to a unit. It always + * includes the original unit in the output map. + * + * @since 2022-07-06 + */ +public final class PrefixSearchRule implements + Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>> { + /** + * A rule that does not add any prefixed versions of units. + */ + public static final PrefixSearchRule NO_PREFIXES = getUniversalRule( + Set.of()); + + /** + * A rule that gives every unit a common set of prefixes. + */ + public static final PrefixSearchRule COMMON_PREFIXES = getCoherentOnlyRule( + Set.of(Metric.MILLI, Metric.KILO)); + + /** + * A rule that gives every unit all metric prefixes. + */ + public static final PrefixSearchRule ALL_METRIC_PREFIXES = getCoherentOnlyRule( + Metric.ALL_PREFIXES); + + /** + * Gets a rule that applies the provided prefixes to coherent units only (as + * defined by {@link LinearUnit#isCoherent}), except the kilogram + * (specifically, units named "kilogram"). + * + * @param prefixes prefixes to apply + * @return prefix rule + * @since 2022-07-06 + */ + public static final PrefixSearchRule getCoherentOnlyRule( + Set<UnitPrefix> prefixes) { + return new PrefixSearchRule(prefixes, + u -> u.isCoherent() && !u.getName().equals("kilogram")); + } + + /** + * Gets a rule that applies the provided prefixes to all units. + * + * @param prefixes prefixes to apply + * @return prefix rule + * @since 2022-07-06 + */ + public static final PrefixSearchRule getUniversalRule( + Set<UnitPrefix> prefixes) { + return new PrefixSearchRule(prefixes, u -> true); + } + + /** + * The set of prefixes that will be applied to the unit. + */ + private final Set<UnitPrefix> prefixes; + + /** + * Determines which units are given prefixes. + */ + private final Predicate<LinearUnit> allowUnit; + + /** + * @param prefixes + * @param metricOnly + * @since 2022-07-06 + */ + public PrefixSearchRule(Set<UnitPrefix> prefixes, + Predicate<LinearUnit> allowUnit) { + this.prefixes = Collections.unmodifiableSet(new HashSet<>(prefixes)); + this.allowUnit = allowUnit; + } + + @Override + public Map<String, LinearUnit> apply(Entry<String, LinearUnit> t) { + final Map<String, LinearUnit> outputUnits = new HashMap<>(); + final String originalName = t.getKey(); + final LinearUnit originalUnit = t.getValue(); + outputUnits.put(originalName, originalUnit); + if (this.allowUnit.test(originalUnit)) { + for (final UnitPrefix prefix : this.prefixes) { + outputUnits.put(prefix.getName() + originalName, + originalUnit.withPrefix(prefix)); + } + } + return outputUnits; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!(obj instanceof PrefixSearchRule)) + return false; + final PrefixSearchRule other = (PrefixSearchRule) obj; + if (this.allowUnit == null) { + if (other.allowUnit != null) + return false; + } else if (!this.allowUnit.equals(other.allowUnit)) + return false; + if (this.prefixes == null) { + if (other.prefixes != null) + return false; + } else if (!this.prefixes.equals(other.prefixes)) + return false; + return true; + } + + /** + * @return the allowUnit + * @since 2022-07-06 + */ + public Predicate<LinearUnit> getAllowUnit() { + return this.allowUnit; + } + + /** + * @return the prefixes that are applied by this rule + * @since 2022-07-06 + */ + public Set<UnitPrefix> getPrefixes() { + return this.prefixes; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + (this.allowUnit == null ? 0 : this.allowUnit.hashCode()); + result = prime * result + + (this.prefixes == null ? 0 : this.prefixes.hashCode()); + return result; + } + + @Override + public String toString() { + return "Apply the following prefixes: " + this.prefixes; + } +} diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java index f630a77..24a6c10 100644 --- a/src/main/java/sevenUnitsGUI/Presenter.java +++ b/src/main/java/sevenUnitsGUI/Presenter.java @@ -200,8 +200,7 @@ public final class Presenter { * 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); + private final Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>> searchRule = PrefixSearchRule.COMMON_PREFIXES; /** * The set of units that is considered neither metric nor nonmetric for the @@ -596,7 +595,7 @@ public final class Presenter { * @return true iff the One-Way Conversion feature is available (views that * show units as a list will have metric units removed from the From * unit list and imperial/USC units removed from the To unit list) - * + * * @since 2022-03-30 */ public boolean oneWayConversionEnabled() { diff --git a/src/main/resources/unitsfile.txt b/src/main/resources/unitsfile.txt index 340e8ea..2a5abdd 100644 --- a/src/main/resources/unitsfile.txt +++ b/src/main/resources/unitsfile.txt @@ -253,18 +253,12 @@ Wh W h # Extra units to show in the dimension-based converter km km -kilometre km cm cm centimetre cm mm mm -millimetre mm mg mg -milligram mg mL mL ml ml -millilitre mL -kJ kJ -kilojoule kJ MJ MJ megajoule MJ kWh kWh |