diff options
-rw-r--r-- | metric_exceptions.txt | 19 | ||||
-rw-r--r-- | src/org/unitConverter/converterGUI/UnitConverterGUI.java | 35 | ||||
-rw-r--r-- | unitsfile.txt | 5 |
3 files changed, 56 insertions, 3 deletions
diff --git a/metric_exceptions.txt b/metric_exceptions.txt new file mode 100644 index 0000000..73748c0 --- /dev/null +++ b/metric_exceptions.txt @@ -0,0 +1,19 @@ +# This is a list of exceptions for the one-way conversion mode +# Units in this list will be included in both From: and To: +# regardless of whether or not one-way conversion is enabled. + +tempC +tempCelsius +s +second +min +minute +h +hour +d +day +wk +week +gregorianmonth +gregorianyear +km/h
\ No newline at end of file diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index 75ab16d..38a9de1 100644 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -21,7 +21,10 @@ import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.event.KeyEvent; +import java.io.BufferedReader; import java.io.File; +import java.io.FileReader; +import java.io.IOException; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; @@ -128,6 +131,9 @@ final class UnitConverterGUI { /** The names of all of the dimensions */ private final List<String> dimensionNames; + /** Unit names that are ignored by the metric-only/imperial-only filter */ + private final Set<String> metricExceptions; + private final Comparator<String> prefixNameComparator; // conditions for existence of From and To entries @@ -164,6 +170,29 @@ final class UnitConverterGUI { this.database.loadUnitsFile(new File("unitsfile.txt")); this.database.loadDimensionFile(new File("dimensionfile.txt")); + // load metric exceptions + final File exceptions = new File("metric_exceptions.txt"); + this.metricExceptions = new HashSet<>(); + try (FileReader fileReader = new FileReader(exceptions); + BufferedReader reader = new BufferedReader(fileReader)) { + while (reader.ready()) { + String line = reader.readLine(); + + // # can be used for comments + if (line.contains("#")) { + line = line.substring(line.indexOf("#")); + } + + // don't read black lines + if (!line.isBlank()) { + this.metricExceptions.add(line.strip()); + } + } + } catch (final IOException e) { + throw new AssertionError("Loading of metric_exceptions.txt failed.", + e); + } + // a comparator that can be used to compare prefix names // any name that does not exist is less than a name that does. // otherwise, they are compared by value @@ -464,9 +493,11 @@ final class UnitConverterGUI { public final void setOneWay(boolean oneWay) { if (oneWay) { this.fromExistenceCondition.setPredicate( - unitName -> !this.database.getUnit(unitName).isMetric()); + unitName -> this.metricExceptions.contains(unitName) + || !this.database.getUnit(unitName).isMetric()); this.toExistenceCondition.setPredicate( - unitName -> this.database.getUnit(unitName).isMetric()); + unitName -> this.metricExceptions.contains(unitName) + || this.database.getUnit(unitName).isMetric()); } else { this.fromExistenceCondition.setPredicate(unitName -> true); this.toExistenceCondition.setPredicate(unitName -> true); diff --git a/unitsfile.txt b/unitsfile.txt index a067d14..eafe885 100644 --- a/unitsfile.txt +++ b/unitsfile.txt @@ -157,8 +157,11 @@ hour 3600 second h hour day 86400 second d day +week 7 day +wk week julianyear 365.25 day gregorianyear 365.2425 day +gregorianmonth gregorianyear / 12 # Other non-SI "metric" units litre 0.001 m^3 @@ -180,7 +183,7 @@ waterdensity kilogram / litre # Imperial length units foot 0.3048 m ft foot -inch 1 / 12 foot +inch foot / 12 in inch yard 3 foot yd yard |