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 | |
parent | c5f7ad25645450fbb3ab539f76da189b5b8fcfe0 (diff) |
Upgraded to Java 11, and upgraded file code to the new I/O
-rw-r--r-- | .classpath | 6 | ||||
-rw-r--r-- | .settings/org.eclipse.jdt.core.prefs | 11 | ||||
-rw-r--r-- | src/org/unitConverter/converterGUI/UnitConverterGUI.java | 57 | ||||
-rw-r--r-- | src/org/unitConverter/unit/UnitDatabase.java | 27 |
4 files changed, 45 insertions, 56 deletions
@@ -1,10 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <classpath> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> - <attributes> - <attribute name="maven.pomderived" value="true"/> - </attributes> - </classpathentry> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"/> <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/> <classpathentry kind="src" output="target/classes" path="src"> <attributes> diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 2dd1838..01cf56c 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,15 +1,16 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.compliance=11 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning org.eclipse.jdt.core.compiler.processAnnotations=disabled -org.eclipse.jdt.core.compiler.release=disabled -org.eclipse.jdt.core.compiler.source=1.8 +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=11 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"); diff --git a/src/org/unitConverter/unit/UnitDatabase.java b/src/org/unitConverter/unit/UnitDatabase.java index 9ca9617..000acf5 100644 --- a/src/org/unitConverter/unit/UnitDatabase.java +++ b/src/org/unitConverter/unit/UnitDatabase.java @@ -16,12 +16,11 @@ */ package org.unitConverter.unit; -import java.io.BufferedReader; -import java.io.File; import java.io.FileNotFoundException; -import java.io.FileReader; import java.io.IOException; import java.math.BigDecimal; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.AbstractSet; import java.util.ArrayList; import java.util.Arrays; @@ -1866,15 +1865,12 @@ public final class UnitDatabase { * @since 2019-01-13 * @since v0.1.0 */ - public void loadDimensionFile(final File file) { + public void loadDimensionFile(final Path file) { Objects.requireNonNull(file, "file must not be null."); - try (FileReader fileReader = new FileReader(file); - BufferedReader reader = new BufferedReader(fileReader)) { - // while the reader has lines to read, read a line, then parse it, then - // add it + try { long lineCounter = 0; - while (reader.ready()) { - this.addDimensionFromLine(reader.readLine(), ++lineCounter); + for (final String line : Files.readAllLines(file)) { + this.addDimensionFromLine(line, ++lineCounter); } } catch (final FileNotFoundException e) { throw new IllegalArgumentException("Could not find file " + file, e); @@ -1908,15 +1904,12 @@ public final class UnitDatabase { * @since 2019-01-13 * @since v0.1.0 */ - public void loadUnitsFile(final File file) { + public void loadUnitsFile(final Path file) { Objects.requireNonNull(file, "file must not be null."); - try (FileReader fileReader = new FileReader(file); - BufferedReader reader = new BufferedReader(fileReader)) { - // while the reader has lines to read, read a line, then parse it, then - // add it + try { long lineCounter = 0; - while (reader.ready()) { - this.addUnitOrPrefixFromLine(reader.readLine(), ++lineCounter); + for (final String line : Files.readAllLines(file)) { + this.addUnitOrPrefixFromLine(line, ++lineCounter); } } catch (final FileNotFoundException e) { throw new IllegalArgumentException("Could not find file " + file, e); |