Skip to content

Commit

Permalink
[KYUUBI #6447] Use static regex Pattern instances in JavaUtils.timeSt…
Browse files Browse the repository at this point in the history
…ringAs and JavaUtils.byteStringAs

# 🔍 Description
## Issue References 🔗

This pull request fixes #6447

## Describe Your Solution 🔧
Use static regex Pattern instances in JavaUtils.timeStringAs and JavaUtils.byteStringAs

## Types of changes 🔖

- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Test Plan 🧪

#### Behavior Without This Pull Request ⚰️

#### Behavior With This Pull Request 🎉

#### Related Unit Tests

---

# Checklist 📝

- [ ] This patch was not authored or co-authored using [Generative Tooling](https://www.apache.org/legal/generative-tooling.html)

**Be nice. Be informative.**

Closes #6448 from lsm1/branch-kyuubi-6447.

Closes #6447

467066c [senmiaoliu] Use static regex Pattern instances in JavaUtils

Authored-by: senmiaoliu <senmiaoliu@trip.com>
Signed-off-by: Cheng Pan <chengpan@apache.org>
  • Loading branch information
lsm1 authored and pan3793 committed Jun 5, 2024
1 parent 6933a91 commit bb92128
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public class JavaUtils {
byteSuffixes.put("pb", ByteUnit.PiB);
}

private static final Pattern TIME_STRING_PATTERN = Pattern.compile("(-?[0-9]+)([a-z]+)?");

/**
* Convert a passed time string (e.g. 50s, 100ms, or 250us) to a time count in the given unit. The
* unit is also considered the default if the given string does not specify a unit.
Expand All @@ -66,7 +68,7 @@ public static long timeStringAs(String str, TimeUnit unit) {
String lower = str.toLowerCase(Locale.ROOT).trim();

try {
Matcher m = Pattern.compile("(-?[0-9]+)([a-z]+)?").matcher(lower);
Matcher m = TIME_STRING_PATTERN.matcher(lower);
if (!m.matches()) {
throw new NumberFormatException("Failed to parse time string: " + str);
}
Expand Down Expand Up @@ -107,6 +109,10 @@ public static long timeStringAsSec(String str) {
return timeStringAs(str, TimeUnit.SECONDS);
}

private static final Pattern BYTE_STRING_PATTERN = Pattern.compile("([0-9]+)([a-z]+)?");
private static final Pattern BYTE_STRING_FRACTION_PATTERN =
Pattern.compile("([0-9]+\\.[0-9]+)([a-z]+)?");

/**
* Convert a passed byte string (e.g. 50b, 100kb, or 250mb) to the given. If no suffix is
* provided, a direct conversion to the provided unit is attempted.
Expand All @@ -115,8 +121,8 @@ public static long byteStringAs(String str, ByteUnit unit) {
String lower = str.toLowerCase(Locale.ROOT).trim();

try {
Matcher m = Pattern.compile("([0-9]+)([a-z]+)?").matcher(lower);
Matcher fractionMatcher = Pattern.compile("([0-9]+\\.[0-9]+)([a-z]+)?").matcher(lower);
Matcher m = BYTE_STRING_PATTERN.matcher(lower);
Matcher fractionMatcher = BYTE_STRING_FRACTION_PATTERN.matcher(lower);

if (m.matches()) {
long val = Long.parseLong(m.group(1));
Expand Down

0 comments on commit bb92128

Please sign in to comment.