diff options
author | Kevin <92656833+kevinthegreat1@users.noreply.github.com> | 2024-05-23 22:19:13 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-23 22:19:13 -0400 |
commit | b6da8de30929b4722244062e948dca42a810a230 (patch) | |
tree | f5ea29a18d285f29397d5e2a5bbaac06194eb38e /src/main/java/de/hysky/skyblocker/utils/RegexUtils.java | |
parent | 610c64758fc8d0b8bea0145c33881b60c0747bd7 (diff) | |
parent | 1f6798df58af5eec4dfe954ab413c5a92d0fee63 (diff) | |
download | Skyblocker-b6da8de30929b4722244062e948dca42a810a230.tar.gz Skyblocker-b6da8de30929b4722244062e948dca42a810a230.tar.bz2 Skyblocker-b6da8de30929b4722244062e948dca42a810a230.zip |
Merge pull request #683 from Emirlol/chocolate-factory-helper
Add chocolate factory helper
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils/RegexUtils.java')
-rw-r--r-- | src/main/java/de/hysky/skyblocker/utils/RegexUtils.java | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/RegexUtils.java b/src/main/java/de/hysky/skyblocker/utils/RegexUtils.java new file mode 100644 index 00000000..5b91a80b --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/utils/RegexUtils.java @@ -0,0 +1,55 @@ +package de.hysky.skyblocker.utils; + +import java.util.OptionalDouble; +import java.util.OptionalInt; +import java.util.OptionalLong; +import java.util.regex.Matcher; + +public class RegexUtils { + /** + * @return An OptionalLong of the first group in the matcher, or an empty OptionalLong if the matcher doesn't find anything. + */ + public static OptionalLong getLongFromMatcher(Matcher matcher) { + return getLongFromMatcher(matcher, matcher.hasMatch() ? matcher.end() : 0); + } + + /** + * @return An OptionalLong of the first group in the matcher, or an empty OptionalLong if the matcher doesn't find anything. + */ + public static OptionalLong getLongFromMatcher(Matcher matcher, int startingIndex) { + if (!matcher.find(startingIndex)) return OptionalLong.empty(); + return OptionalLong.of(Long.parseLong(matcher.group(1).replace(",", ""))); + } + + /** + * @return An OptionalInt of the first group in the matcher, or an empty OptionalInt if the matcher doesn't find anything. + */ + public static OptionalInt getIntFromMatcher(Matcher matcher) { + return getIntFromMatcher(matcher, matcher.hasMatch() ? matcher.end() : 0); + } + + /** + * @return An OptionalInt of the first group in the matcher, or an empty OptionalInt if the matcher doesn't find anything. + */ + public static OptionalInt getIntFromMatcher(Matcher matcher, int startingIndex) { + if (!matcher.find(startingIndex)) return OptionalInt.empty(); + return OptionalInt.of(Integer.parseInt(matcher.group(1).replace(",", ""))); + } + + /** + * @return An OptionalDouble of the first group in the matcher, or an empty OptionalDouble if the matcher doesn't find anything. + * @implNote Assumes the decimal separator is `.` + */ + public static OptionalDouble getDoubleFromMatcher(Matcher matcher) { + return getDoubleFromMatcher(matcher, matcher.hasMatch() ? matcher.end() : 0); + } + + /** + * @return An OptionalDouble of the first group in the matcher, or an empty OptionalDouble if the matcher doesn't find anything. + * @implNote Assumes the decimal separator is `.` + */ + public static OptionalDouble getDoubleFromMatcher(Matcher matcher, int startingIndex) { + if (!matcher.find(startingIndex)) return OptionalDouble.empty(); + return OptionalDouble.of(Double.parseDouble(matcher.group(1).replace(",", ""))); + } +} |