mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-21 04:48:44 +00:00
changed parameter for slowmode command to be a duration string instead of just seconds
added exception to duration parsing, in case there is a unknown duration indicator
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package dev.sheldan.abstracto.core.utils;
|
||||
|
||||
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -11,9 +13,11 @@ public class ParseUtils {
|
||||
public static Duration parseDuration(String textToParseFrom) {
|
||||
Matcher matcher = ParseUtils.messageRegex.matcher(textToParseFrom);
|
||||
Duration start = Duration.ZERO;
|
||||
String rest = textToParseFrom;
|
||||
while(matcher.find()) {
|
||||
String unit = matcher.group("unit");
|
||||
String number = matcher.group("number");
|
||||
rest = rest.replace(matcher.group(0), "");
|
||||
long parsed = Long.parseLong(number);
|
||||
switch (unit) {
|
||||
case "w": start = start.plus(Duration.ofDays(parsed * 7)); break;
|
||||
@@ -21,8 +25,12 @@ public class ParseUtils {
|
||||
case "h": start = start.plus(Duration.ofHours(parsed)); break;
|
||||
case "m": start = start.plus(Duration.ofMinutes(parsed)); break;
|
||||
case "s": start = start.plus(Duration.ofSeconds(parsed)); break;
|
||||
default: throw new AbstractoRunTimeException(String.format("Invalid time format %s", unit));
|
||||
}
|
||||
}
|
||||
if(!rest.equals("")) {
|
||||
throw new AbstractoRunTimeException(String.format("Invalid time format found: %s", rest));
|
||||
}
|
||||
return start;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.sheldan.abstracto.core.utils;
|
||||
|
||||
|
||||
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
@@ -41,4 +42,9 @@ public class ParseUtilsTest {
|
||||
Duration duration = ParseUtils.parseDuration("70s");
|
||||
assertEquals(Duration.ofMinutes(1).plus(Duration.ofSeconds(10)), duration);
|
||||
}
|
||||
|
||||
@Test(expected = AbstractoRunTimeException.class)
|
||||
public void invalidTimeFormat() {
|
||||
ParseUtils.parseDuration("1k");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user