diff options
author | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-03-27 18:00:14 -0500 |
---|---|---|
committer | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-03-27 18:00:14 -0500 |
commit | 3e6fee9561a00e5d9958c5685336c9d68c8629e1 (patch) | |
tree | 61ce842d1e982f41c32a87adfcabe2b2a4c1bde2 /src/main/java/org/unitConverter/unit | |
parent | c767cfda66e22f6e6b2f87b3a4ded850352e909d (diff) |
Used resources instead of Paths to make the generated jar work
Diffstat (limited to 'src/main/java/org/unitConverter/unit')
-rw-r--r-- | src/main/java/org/unitConverter/unit/UnitDatabase.java | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/main/java/org/unitConverter/unit/UnitDatabase.java b/src/main/java/org/unitConverter/unit/UnitDatabase.java index 000acf5..6322fef 100644 --- a/src/main/java/org/unitConverter/unit/UnitDatabase.java +++ b/src/main/java/org/unitConverter/unit/UnitDatabase.java @@ -18,6 +18,7 @@ package org.unitConverter.unit; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.math.BigDecimal; import java.nio.file.Files; import java.nio.file.Path; @@ -34,6 +35,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Scanner; import java.util.Set; import java.util.function.BiFunction; import java.util.function.Function; @@ -1880,6 +1882,22 @@ public final class UnitDatabase { } /** + * Adds all dimensions from a {@code InputStream}. Otherwise, works like + * {@link #loadDimensionFile}. + * + * @param stream stream to load from + * @since 2021-03-27 + */ + public void loadDimensionsFromStream(final InputStream stream) { + try (final Scanner scanner = new Scanner(stream)) { + long lineCounter = 0; + while (scanner.hasNextLine()) { + this.addDimensionFromLine(scanner.nextLine(), ++lineCounter); + } + } + } + + /** * Adds all units from a file, using data from the database to parse them. * <p> * Each line in the file should consist of a name and an expression (parsed @@ -1919,6 +1937,22 @@ public final class UnitDatabase { } /** + * Adds all units from a {@code InputStream}. Otherwise, works like + * {@link #loadUnitsFile}. + * + * @param stream stream to load from + * @since 2021-03-27 + */ + public void loadUnitsFromStream(InputStream stream) { + try (final Scanner scanner = new Scanner(stream)) { + long lineCounter = 0; + while (scanner.hasNextLine()) { + this.addUnitOrPrefixFromLine(scanner.nextLine(), ++lineCounter); + } + } + } + + /** * @return a map mapping prefix names to prefixes * @since 2019-04-13 * @since v0.2.0 |