Skip to content

Assignment_part_1 #990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
100 changes: 100 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,100 @@
package io.cdap.wrangler.api.parser;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

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

/**
* Represents a byte size token parsed from a string (e.g., "10KB", "1.5MB").
* The value is stored in canonical units (bytes).
*/
public class ByteSize implements Token {
// The original token string.
private final String token;
// Canonical value stored in bytes.
private final long bytes;
// Token type.
private final TokenType tokenType = TokenType.BYTE_SIZE;

// Conversion constants (using 1024-based conversions)
private static final long KILOBYTE = 1024;
private static final long MEGABYTE = KILOBYTE * 1024;
private static final long GIGABYTE = MEGABYTE * 1024;
private static final long TERABYTE = GIGABYTE * 1024;

// Pattern to capture the numeric and unit portions (e.g., "10KB" or "1.5MB").
private static final Pattern PATTERN = Pattern.compile("([0-9]+(?:\\.[0-9]+)?)([a-zA-Z]+)");

/**
* Constructs a ByteSize token by parsing the input string.
*
* @param token the string representation (e.g., "10KB", "1.5MB")
* @throws IllegalArgumentException if the token is null, empty, or its format is invalid.
*/
public ByteSize(String token) {
if (token == null || token.trim().isEmpty()) {
throw new IllegalArgumentException("Token cannot be null or empty");
}
this.token = token;
Matcher matcher = PATTERN.matcher(token);
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid byte size token format: " + token);
}

String numberStr = matcher.group(1);
String unitStr = matcher.group(2).toUpperCase(); // Normalize unit to upper-case

double value = Double.parseDouble(numberStr);
long multiplier;

// Determine multiplier based on the unit.
if ("B".equals(unitStr)) {
multiplier = 1;
} else if ("KB".equals(unitStr)) {
multiplier = KILOBYTE;
} else if ("MB".equals(unitStr)) {
multiplier = MEGABYTE;
} else if ("GB".equals(unitStr)) {
multiplier = GIGABYTE;
} else if ("TB".equals(unitStr)) {
multiplier = TERABYTE;
} else {
throw new IllegalArgumentException("Unsupported byte size unit: " + unitStr);
}

// Calculate canonical value in bytes.
this.bytes = (long) Math.round(value * multiplier);
}

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

@Override
public Object value() {
// Return the canonical value.
return bytes;
}

@Override
public TokenType type() {
return tokenType;
}

@Override
public JsonElement toJson() {
// Construct a JSON representation of this token.
JsonObject obj = new JsonObject();
obj.addProperty("token", token);
obj.addProperty("type", tokenType.name());
obj.addProperty("bytes", bytes);
return obj;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package io.cdap.wrangler.api.parser;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

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

/**
* Represents a time duration token parsed from a string (e.g., "150ms", "2.1s").
* The value is stored in canonical units (milliseconds).
*/
public class TimeDuration implements Token {
// The original token string.
private final String token;
// Canonical value stored in milliseconds.
private final long milliseconds;
// Token type.
private final TokenType tokenType = TokenType.TIME_DURATION;

// Constant: 1 second = 1000 milliseconds.
private static final long SECOND_IN_MS = 1000;

// Pattern to capture the numeric and unit portions (e.g., "150ms" or "2.1s").
private static final Pattern PATTERN = Pattern.compile("([0-9]+(?:\\.[0-9]+)?)([a-zA-Z]+)");

/**
* Constructs a TimeDuration token by parsing the input string.
*
* @param token the string representation (e.g., "150ms", "2.1s")
* @throws IllegalArgumentException if the token is null, empty, or its format is invalid.
*/
public TimeDuration(String token) {
if (token == null || token.trim().isEmpty()) {
throw new IllegalArgumentException("Token cannot be null or empty");
}
this.token = token;
Matcher matcher = PATTERN.matcher(token);
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid time duration token format: " + token);
}

String numberStr = matcher.group(1);
// Normalize the unit to lower-case for easier comparison.
String unitStr = matcher.group(2).toLowerCase();

double value = Double.parseDouble(numberStr);
long multiplier;

// Determine multiplier based on the unit.
if ("ms".equals(unitStr)) {
multiplier = 1;
} else if ("s".equals(unitStr)) {
multiplier = SECOND_IN_MS;
} else {
throw new IllegalArgumentException("Unsupported time duration unit: " + unitStr);
}

// Calculate canonical value in milliseconds.
this.milliseconds = (long) Math.round(value * multiplier);
}

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

@Override
public Object value() {
// Return the canonical value.
return milliseconds;
}

@Override
public TokenType type() {
return tokenType;
}

@Override
public JsonElement toJson() {
// Construct a JSON representation of this token.
JsonObject obj = new JsonObject();
obj.addProperty("token", token);
obj.addProperty("type", tokenType.name());
obj.addProperty("milliseconds", milliseconds);
return obj;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,19 @@ 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,

/**
* Represents the enumerated type for a byte size token.
* This type is associated with tokens representing byte sizes, such as "10KB" or "1.5MB".
*/
BYTE_SIZE,

/**
* Represents the enumerated type for a time duration token.
* This type is associated with tokens representing time durations, such as "150ms" or "2.1s".
*/
TIME_DURATION


}
3 changes: 3 additions & 0 deletions wrangler-core/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.compile.nullAnalysis.mode": "automatic"
}
100 changes: 53 additions & 47 deletions wrangler-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -313,52 +313,58 @@


<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr4-maven-plugin.version}</version>
<configuration>
<visitor>true</visitor>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>true</doCheck>
<doUpdate>false</doUpdate>
<revisionOnScmFailure>false</revisionOnScmFailure>
<format>{0,date,yyyy-MM-dd-HH:mm:ss}_{1}</format>
<items>
<item>timestamp</item>
<item>${user.name}</item>
</items>
</configuration>
</plugin>
</plugins>
</build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>

<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr4-maven-plugin.version}</version>
<configuration>
<visitor>true</visitor>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>true</doCheck>
<doUpdate>false</doUpdate>
<revisionOnScmFailure>false</revisionOnScmFailure>
<format>{0,date,yyyy-MM-dd-HH:mm:ss}_{1}</format>
<items>
<item>timestamp</item>
<item>${user.name}</item>
</items>
</configuration>
</plugin>
</plugins>
</build>


</project>
Loading