Skip to content

Commit

Permalink
refactor to
Browse files Browse the repository at this point in the history
  • Loading branch information
mchades committed Nov 15, 2023
1 parent 6f3c668 commit 4b807be
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ public enum Strategy {
RANGE,
EVEN;

public static Strategy fromString(String strategy) {
try {
return valueOf(strategy.toUpperCase());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(
"Invalid distribution strategy: "
+ strategy
+ ". Valid values are: "
+ Arrays.toString(Strategy.values()));
public static Strategy getByName(String name) {
for (Strategy strategy : Strategy.values()) {
if (strategy.name().equalsIgnoreCase(name)) {
return strategy;
}
}
throw new IllegalArgumentException(
"Invalid distribution strategy: "
+ name
+ ". Valid values are: "
+ Arrays.toString(Strategy.values()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public interface Partitioning extends Transform {
Partitioning[] EMPTY_PARTITIONING = new Partitioning[0];

Strategy strategy();

// columns of table
void validate(ColumnDTO[] columns) throws IllegalArgumentException;

Expand All @@ -38,16 +39,17 @@ enum Strategy {
RANGE,
FUNCTION;

public static Strategy fromString(String strategy) {
try {
return Strategy.valueOf(strategy.toUpperCase());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(
"Invalid partitioning strategy: "
+ strategy
+ ". Valid values are: "
+ Arrays.toString(Strategy.values()));
public static Strategy getByName(String name) {
for (Strategy strategy : Strategy.values()) {
if (strategy.name().equalsIgnoreCase(name)) {
return strategy;
}
}
throw new IllegalArgumentException(
"Invalid partitioning strategy: "
+ name
+ ". Valid values are: "
+ Arrays.toString(Strategy.values()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
*/
package com.datastrato.gravitino.json;

import static com.datastrato.gravitino.dto.rel.expressions.FunctionArg.ArgType.FUNCTION;

import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.Namespace;
import com.datastrato.gravitino.dto.rel.DistributionDTO;
Expand Down Expand Up @@ -485,7 +483,7 @@ public Partitioning deserialize(JsonParser p, DeserializationContext ctxt) throw
Preconditions.checkArgument(
node.has(STRATEGY), "Cannot parse partitioning from missing strategy: %s", node);
String strategy = getString(STRATEGY, node);
switch (Partitioning.Strategy.fromString(strategy)) {
switch (Partitioning.Strategy.getByName(strategy)) {
case IDENTITY:
return IdentityPartitioningDTO.of(getStringList(FIELD_NAME, node).toArray(new String[0]));
case YEAR:
Expand Down Expand Up @@ -590,7 +588,7 @@ public DistributionDTO deserialize(JsonParser p, DeserializationContext ctxt)
DistributionDTO.Builder builder = new DistributionDTO.Builder();
if (node.has(STRATEGY)) {
String strategy = getString(STRATEGY, node);
builder.withStrategy(Strategy.fromString(strategy));
builder.withStrategy(Strategy.getByName(strategy));
}
builder.withNumber(getInt(NUMBER, node));
List<FunctionArg> args = Lists.newArrayList();
Expand Down

0 comments on commit 4b807be

Please sign in to comment.