diff options
author | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-03-13 15:55:35 -0500 |
---|---|---|
committer | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-03-13 15:55:35 -0500 |
commit | 184b7cc697ffc2dcbd49cfb3d0fd7b14bdac8803 (patch) | |
tree | f20fbc94b06b365a0195feed860a737a9e48d57e /src/org/unitConverter/converterGUI/UnitConverterGUI.java | |
parent | c5f7ad25645450fbb3ab539f76da189b5b8fcfe0 (diff) |
Upgraded to Java 11, and upgraded file code to the new I/O
Diffstat (limited to 'src/org/unitConverter/converterGUI/UnitConverterGUI.java')
-rw-r--r-- | src/org/unitConverter/converterGUI/UnitConverterGUI.java | 57 |
1 files changed, 28 insertions, 29 deletions
diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index 69f188b..6ddc4a0 100644 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -21,10 +21,7 @@ import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.event.KeyEvent; -import java.io.BufferedReader; import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; import java.io.IOException; import java.math.BigDecimal; import java.math.MathContext; @@ -95,6 +92,14 @@ final class UnitConverterGUI { private static class Presenter { /** The default place where settings are stored. */ private static final String DEFAULT_SETTINGS_FILEPATH = "settings.txt"; + /** The default place where units are stored. */ + private static final Path DEFAULT_UNITS_FILE = Path.of("unitsfile.txt"); + /** The default place where dimensions are stored. */ + private static final Path DEFAULT_DIMENSION_FILE = Path + .of("dimensionfile.txt"); + /** The default place where exceptions are stored. */ + private static final Path DEFAULT_EXCEPTIONS_FILE = Path + .of("metric_exceptions.txt"); /** * Adds default units and dimensions to a database. @@ -125,6 +130,15 @@ final class UnitConverterGUI { database.addDimension("TEMPERATURE", SI.Dimensions.TEMPERATURE); } + /** + * @return {@code line} with any comments removed. + * @since 2021-03-13 + */ + private static final String withoutComments(String line) { + final int index = line.indexOf('#'); + return index == -1 ? line : line.substring(index); + } + /** The presenter's associated view. */ private final View view; @@ -150,11 +164,11 @@ final class UnitConverterGUI { /** The prefix rule */ private DefaultPrefixRepetitionRule prefixRule = null; - // conditions for existence of From and To entries // used for one-way conversion private final MutablePredicate<String> fromExistenceCondition = new MutablePredicate<>( s -> true); + private final MutablePredicate<String> toExistenceCondition = new MutablePredicate<>( s -> true); @@ -182,27 +196,14 @@ final class UnitConverterGUI { DefaultPrefixRepetitionRule.NO_RESTRICTION); Presenter.addDefaults(this.database); - this.database.loadUnitsFile(new File("unitsfile.txt")); - this.database.loadDimensionFile(new File("dimensionfile.txt")); + this.database.loadUnitsFile(DEFAULT_UNITS_FILE); + this.database.loadDimensionFile(DEFAULT_DIMENSION_FILE); // 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()); - } - } + try { + this.metricExceptions = Files.readAllLines(DEFAULT_EXCEPTIONS_FILE) + .stream().map(Presenter::withoutComments) + .filter(s -> !s.isBlank()).collect(Collectors.toSet()); } catch (final IOException e) { throw new AssertionError("Loading of metric_exceptions.txt failed.", e); @@ -486,13 +487,11 @@ final class UnitConverterGUI { * @since 2021-02-17 */ public final void loadSettings() { - try (BufferedReader reader = Files - .newBufferedReader(this.getSettingsFile())) { - + try { // read file line by line final int lineNum = 0; - String line; - while ((line = reader.readLine()) != null) { + for (final String line : Files + .readAllLines(this.getSettingsFile())) { final int equalsIndex = line.indexOf('='); if (equalsIndex == -1) throw new IllegalStateException( @@ -1144,7 +1143,7 @@ final class UnitConverterGUI { try { final Path aboutFile = Path.of("src", "about.txt"); infoText = Files.readAllLines(aboutFile).stream() - .filter(s -> !s.trim().startsWith("#")) + .map(Presenter::withoutComments) .collect(Collectors.joining("\n")); } catch (final IOException e) { throw new AssertionError("I/O exception loading about.txt"); |