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
399 changes: 260 additions & 139 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion wrangler-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@
</dependency>

</dependencies>
</project>
</project>
147 changes: 147 additions & 0 deletions wrangler-api/src/main/java/io/cdap/wrangler/api/parser/ByteSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright © 2017-2019 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package io.cdap.wrangler.api.parser;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.cdap.wrangler.api.annotations.PublicEvolving;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Token class representing byte size values with units (e.g., "10KB", "5MB").
* Parses and stores byte sizes, providing methods to retrieve the value in
* bytes.
*/
@PublicEvolving
public class ByteSize implements Token {
private static final Pattern BYTE_SIZE_PATTERN = Pattern.compile("(\\d+)\\s*([kKmMgGtTpP]?[bB]?)");
private final String value;
private final long bytes;

/**
* Constructs a ByteSize token from a string representation.
* Accepts formats like "10KB", "5MB", "1GB", etc.
*
* @param value String representation of a byte size with unit
* @throws IllegalArgumentException if the string cannot be parsed as a byte
* size
*/
public ByteSize(String value) {
this.value = value;
this.bytes = parseBytes(value);
}

/**
* Parses a string representation of byte size into its byte value.
*
* @param sizeStr String representation of a byte size (e.g., "10KB")
* @return The size in bytes
* @throws IllegalArgumentException if the string cannot be parsed
*/
private long parseBytes(String sizeStr) {
Matcher matcher = BYTE_SIZE_PATTERN.matcher(sizeStr);
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid byte size format: " + sizeStr);
}

long size = Long.parseLong(matcher.group(1));
String unit = matcher.group(2).toUpperCase();

switch (unit) {
case "B":
case "":
return size;
case "KB":
case "K":
return size * 1024;
case "MB":
case "M":
return size * 1024 * 1024;
case "GB":
case "G":
return size * 1024 * 1024 * 1024;
case "TB":
case "T":
return size * 1024 * 1024 * 1024 * 1024;
case "PB":
case "P":
return size * 1024 * 1024 * 1024 * 1024 * 1024;
default:
throw new IllegalArgumentException("Unsupported byte size unit: " + unit);
}
}

/**
* Returns the original string representation of the byte size.
*/
@Override
public String value() {
return value;
}

/**
* Returns the size in bytes.
*
* @return The size in bytes
*/
public long getBytes() {
return bytes;
}

/**
* Returns the size in kilobytes.
*
* @return The size in kilobytes
*/
public double getKilobytes() {
return bytes / 1024.0;
}

/**
* Returns the size in megabytes.
*
* @return The size in megabytes
*/
public double getMegabytes() {
return bytes / (1024.0 * 1024.0);
}

/**
* Returns the size in gigabytes.
*
* @return The size in gigabytes
*/
public double getGigabytes() {
return bytes / (1024.0 * 1024.0 * 1024.0);
}

@Override
public TokenType type() {
return TokenType.BYTE_SIZE;
}

@Override
public JsonElement toJson() {
JsonObject object = new JsonObject();
object.addProperty("type", TokenType.BYTE_SIZE.name());
object.addProperty("value", value);
object.addProperty("bytes", bytes);
return object;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright © 2017-2019 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package io.cdap.wrangler.api.parser;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.cdap.wrangler.api.annotations.PublicEvolving;

import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Token class representing time duration values with units (e.g., "5s", "10m",
* "2h").
* Parses and stores time durations, providing methods to retrieve the value in
* various time units.
*/
@PublicEvolving
public class TimeDuration implements Token {
private static final Pattern TIME_PATTERN = Pattern.compile("(\\d+)\\s*([smhdwy]|mo)");
private final String value;
private final long milliseconds;

/**
* Constructs a TimeDuration token from a string representation.
* Accepts formats like "5s" (seconds), "10m" (minutes), "2h" (hours), etc.
*
* @param value String representation of a time duration with unit
* @throws IllegalArgumentException if the string cannot be parsed as a time
* duration
*/
public TimeDuration(String value) {
this.value = value;
this.milliseconds = parseMilliseconds(value);
}

/**
* Parses a string representation of time duration into milliseconds.
*
* @param durationStr String representation of a time duration (e.g., "5s")
* @return The duration in milliseconds
* @throws IllegalArgumentException if the string cannot be parsed
*/
private long parseMilliseconds(String durationStr) {
Matcher matcher = TIME_PATTERN.matcher(durationStr);
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid time duration format: " + durationStr);
}

long amount = Long.parseLong(matcher.group(1));
String unit = matcher.group(2).toLowerCase();

switch (unit) {
case "s":
return TimeUnit.SECONDS.toMillis(amount);
case "m":
return TimeUnit.MINUTES.toMillis(amount);
case "h":
return TimeUnit.HOURS.toMillis(amount);
case "d":
return TimeUnit.DAYS.toMillis(amount);
case "w":
return TimeUnit.DAYS.toMillis(amount * 7);
case "mo":
// Approximate a month as 30 days
return TimeUnit.DAYS.toMillis(amount * 30);
case "y":
// Approximate a year as 365 days
return TimeUnit.DAYS.toMillis(amount * 365);
default:
throw new IllegalArgumentException("Unsupported time unit: " + unit);
}
}

/**
* Returns the original string representation of the time duration.
*/
@Override
public String value() {
return value;
}

/**
* Returns the duration in milliseconds.
*
* @return The duration in milliseconds
*/
public long getMilliseconds() {
return milliseconds;
}

/**
* Returns the duration in seconds.
*
* @return The duration in seconds
*/
public double getSeconds() {
return milliseconds / 1000.0;
}

/**
* Returns the duration in minutes.
*
* @return The duration in minutes
*/
public double getMinutes() {
return milliseconds / (1000.0 * 60);
}

/**
* Returns the duration in hours.
*
* @return The duration in hours
*/
public double getHours() {
return milliseconds / (1000.0 * 60 * 60);
}

/**
* Returns the duration in days.
*
* @return The duration in days
*/
public double getDays() {
return milliseconds / (1000.0 * 60 * 60 * 24);
}

@Override
public TokenType type() {
return TokenType.TIME_DURATION;
}

@Override
public JsonElement toJson() {
JsonObject object = new JsonObject();
object.addProperty("type", TokenType.TIME_DURATION.name());
object.addProperty("value", value);
object.addProperty("milliseconds", milliseconds);
return object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* of this interface.</p>
*/
@PublicEvolving

public interface Token extends Serializable {
/**
* Returns the {@code value} of the object wrapped by the
Expand All @@ -57,3 +58,4 @@ public interface Token extends Serializable {
*/
JsonElement toJson();
}
// [blank line here]
Loading