diff --git a/wrangler-api/src/main/java/io/cdap/wrangler/api/parser/ByteSize.java b/wrangler-api/src/main/java/io/cdap/wrangler/api/parser/ByteSize.java new file mode 100644 index 000000000..aa9a261ec --- /dev/null +++ b/wrangler-api/src/main/java/io/cdap/wrangler/api/parser/ByteSize.java @@ -0,0 +1,64 @@ +package io.cdap.wrangler.api.parser; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ByteSize implements Token { + private static final Pattern PATTERN = Pattern.compile("(\\d+(?:\\.\\d+)?)([KMGTP]?B)", Pattern.CASE_INSENSITIVE); + + private final long bytes; + + public ByteSize(String input) { + Matcher matcher = PATTERN.matcher(input.trim()); + if (!matcher.matches()) { + throw new IllegalArgumentException("Invalid byte size format: " + input); + } + + double value = Double.parseDouble(matcher.group(1)); + String unit = matcher.group(2).toUpperCase(); + + switch (unit) { + case "B": + bytes = (long) value; + break; + case "KB": + bytes = (long) (value * 1024); + break; + case "MB": + bytes = (long) (value * 1024 * 1024); + break; + case "GB": + bytes = (long) (value * 1024 * 1024 * 1024); + break; + case "TB": + bytes = (long) (value * 1024 * 1024 * 1024 * 1024); + break; + case "PB": + bytes = (long) (value * 1024 * 1024 * 1024 * 1024 * 1024); + break; + default: + throw new IllegalArgumentException("Unknown unit: " + unit); + } + } + + public long getBytes() { + return bytes; + } + + @Override + public Object value() { + return bytes; + } + + @Override + public TokenType type() { + return TokenType.BYTE_SIZE; + } + + @Override + public JsonElement toJson() { + return new JsonPrimitive(bytes); + } +} diff --git a/wrangler-api/src/main/java/io/cdap/wrangler/api/parser/TimeDuration.java b/wrangler-api/src/main/java/io/cdap/wrangler/api/parser/TimeDuration.java new file mode 100644 index 000000000..9162e5b1b --- /dev/null +++ b/wrangler-api/src/main/java/io/cdap/wrangler/api/parser/TimeDuration.java @@ -0,0 +1,62 @@ +package io.cdap.wrangler.api.parser; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class TimeDuration implements Token { + private static final Pattern PATTERN = Pattern.compile("(\\d+(?:\\.\\d+)?)(ms|s|m|h|d)", Pattern.CASE_INSENSITIVE); + + private final long milliseconds; + + public TimeDuration(String input) { + Matcher matcher = PATTERN.matcher(input.trim()); + if (!matcher.matches()) { + throw new IllegalArgumentException("Invalid time duration format: " + input); + } + + double value = Double.parseDouble(matcher.group(1)); + String unit = matcher.group(2).toLowerCase(); + + switch (unit) { + case "ms": + milliseconds = (long) value; + break; + case "s": + milliseconds = (long) (value * 1000); + break; + case "m": + milliseconds = (long) (value * 60 * 1000); + break; + case "h": + milliseconds = (long) (value * 60 * 60 * 1000); + break; + case "d": + milliseconds = (long) (value * 24 * 60 * 60 * 1000); + break; + default: + throw new IllegalArgumentException("Unknown time unit: " + unit); + } + } + + public long getMilliseconds() { + return milliseconds; + } + + @Override + public Object value() { + return milliseconds; + } + + @Override + public TokenType type() { + return TokenType.TIME_DURATION; + } + + @Override + public JsonElement toJson() { + return new JsonPrimitive(milliseconds); + } +} diff --git a/wrangler-api/src/main/java/io/cdap/wrangler/api/parser/TokenType.java b/wrangler-api/src/main/java/io/cdap/wrangler/api/parser/TokenType.java index 8c93b0e6a..6156a3ccf 100644 --- a/wrangler-api/src/main/java/io/cdap/wrangler/api/parser/TokenType.java +++ b/wrangler-api/src/main/java/io/cdap/wrangler/api/parser/TokenType.java @@ -152,5 +152,9 @@ public enum TokenType implements Serializable { * Represents the enumerated type for the object of type {@code String} with restrictions * on characters that can be present in a string. */ - IDENTIFIER + IDENTIFIER, + + BYTE_SIZE, + + TIME_DURATION, }