diff options
author | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-12-02 16:46:15 -0500 |
---|---|---|
committer | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-12-02 16:46:15 -0500 |
commit | 345fa45ae562e4699cc9c2e039dc82c7a66b0451 (patch) | |
tree | 970052482622edd7e7fa950dfcee66b165818a4f /src/main/java/sevenUnits/utils/UncertainDouble.java | |
parent | 54e481cdb7115b236bcddb86b785d88dcc757c39 (diff) | |
parent | 8ea77520ce58e948eeffc4c2e8c25c69e88ed00a (diff) |
Merge branch 'testing-upgrades' into develop
Diffstat (limited to 'src/main/java/sevenUnits/utils/UncertainDouble.java')
-rw-r--r-- | src/main/java/sevenUnits/utils/UncertainDouble.java | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/src/main/java/sevenUnits/utils/UncertainDouble.java b/src/main/java/sevenUnits/utils/UncertainDouble.java index 8fe4b31..fe41104 100644 --- a/src/main/java/sevenUnits/utils/UncertainDouble.java +++ b/src/main/java/sevenUnits/utils/UncertainDouble.java @@ -36,13 +36,14 @@ public final class UncertainDouble implements Comparable<UncertainDouble> { */ public static final UncertainDouble ZERO = UncertainDouble.of(0, 0); + static final String NUMBER_REGEX = "(\\d+(?:[\\.,]\\d+))"; + /** * A regular expression that can recognize toString forms */ - private static final Pattern TO_STRING = Pattern - .compile("([a-zA-Z_0-9\\.\\,]+)" // a number - // optional "± [number]" - + "(?:\\s*(?:±|\\+-)\\s*([a-zA-Z_0-9\\.\\,]+))?"); + static final Pattern TO_STRING = Pattern.compile(NUMBER_REGEX + // optional "± [number]" + + "(?:\\s*(?:±|\\+-)\\s*" + NUMBER_REGEX + ")?"); /** * Parses a string in the form of {@link UncertainDouble#toString(boolean)} @@ -60,10 +61,14 @@ public final class UncertainDouble implements Comparable<UncertainDouble> { Objects.requireNonNull(s, "s may not be null"); final Matcher matcher = TO_STRING.matcher(s); + if (!matcher.matches()) + throw new IllegalArgumentException( + "Could not parse stirng \"" + s + "\"."); + double value, uncertainty; try { value = Double.parseDouble(matcher.group(1)); - } catch (IllegalStateException | NumberFormatException e) { + } catch (final NumberFormatException e) { throw new IllegalArgumentException( "String " + s + " not in correct format."); } |