Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}