Skip to content

Commit

Permalink
Replace prettyPrint with pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Thihup committed Aug 18, 2021
1 parent 59d0d79 commit b49c9e7
Show file tree
Hide file tree
Showing 34 changed files with 52 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@ public Object getValue(ELContext context) {
var rightValue = right.getValue(context);
return Operation.ADDITION.calculate(leftValue, rightValue, context);
}

@Override
public String prettyPrint() {
return "%s + %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/AndNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,4 @@ public Object getValue(ELContext context) {
return false;
return context.convertToType(right.getValue(context), boolean.class);
}

@Override
public String prettyPrint() {
return "%s && %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/AssignNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,4 @@ private Object getValue(ELContext context, Object node) {
return expressionNode.getValue(context);
return null;
}

@Override
public String prettyPrint() {
return "%s = %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
8 changes: 0 additions & 8 deletions io.joel/src/main/java/io/joel/impl/node/BooleanNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,4 @@ public String toString() {
public Class<?> getType(ELContext context) {
return Boolean.class;
}

@Override
public String prettyPrint() {
return switch (this) {
case TRUE -> "true";
case FALSE -> "false";
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ private static <E extends Throwable> void sneakyThrow(Throwable e) throws E {
throw (E) e;
}

@Override
public String prettyPrint() {
return "%s(%s)".formatted(callee.prettyPrint(), arguments.stream().map(ExpressionNode::prettyPrint).collect(Collectors.joining(",")));
}

public ExpressionNode callee() {
return callee;
}
Expand Down
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/ConcatNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ public Class<?> getType(ELContext context) {
public Object getValue(ELContext context) {
return (String) context.convertToType(left.getValue(context), String.class) + context.convertToType(right.getValue(context), String.class);
}

@Override
public String prettyPrint() {
return "%s += %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ public Class<?> getType(ELContext context) {
public Object getValue(ELContext context) {
return node.getValue(context);
}

@Override
public String prettyPrint() {
return "#{%s}".formatted(node.prettyPrint());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@ public Object getValue(ELContext context) {
var rightValue = right.getValue(context);
return Operation.DIVISION.calculate(leftValue, rightValue, context);
}

@Override
public String prettyPrint() {
return "%s / %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ public Class<?> getType(ELContext context) {
public Object getValue(ELContext context) {
return node.getValue(context);
}

@Override
public String prettyPrint() {
return "${%s}".formatted(node.prettyPrint());
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/EqualNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,4 @@ public Object getValue(ELContext context) {
}
return leftValue.equals(rightValue);
}

@Override
public String prettyPrint() {
return "%s == %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
45 changes: 42 additions & 3 deletions io.joel/src/main/java/io/joel/impl/node/ExpressionNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.el.ELContext;

import java.io.Serializable;
import java.util.stream.Collectors;

public sealed interface ExpressionNode extends Serializable
permits CallExpressionNode,
Expand All @@ -25,6 +26,47 @@ public sealed interface ExpressionNode extends Serializable
UnaryNotNode,
ObjectNode {

static String prettyPrint(ExpressionNode node) {
return switch (node) {
case ObjectNode n -> n.value().toString();
case BooleanNode n -> switch (n) {
case TRUE -> "true";
case FALSE -> "false";
};
case NullNode ignored -> "null";
case IdentifierNode n -> n.value();
case StringNode n -> "'%s'".formatted(n.value());
case NumberNode n -> n.value().toString();
case SetNode n -> n.values().stream().map(ExpressionNode::prettyPrint).collect(Collectors.joining(",", "{", "}"));
case ListNode n -> n.values().stream().map(ExpressionNode::prettyPrint).collect(Collectors.joining(",", "[", "]"));
case CallExpressionNode n -> "%s(%s)".formatted(prettyPrint(n.callee()), n.arguments().stream().map(ExpressionNode::prettyPrint).collect(Collectors.joining(",")));
case DeferredExpressionNode n -> "#{%s}".formatted(prettyPrint(n));
case AddExpressionNode n -> "%s + %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
case AssignNode n -> "%s = %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
case ConcatNode n -> "%s += %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
case DivExpressionNode n -> "%s / %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
case ModExpressionNode n -> "%s %% %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
case MulExpressionNode n -> "%s * %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
case SubExpressionNode n -> "%s - %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
case DynamicExpressionNode n -> "${%s}".formatted(prettyPrint(n));
case LambdaNode n -> "(%s) -> %s".formatted(String.join(",", n.parameters()), prettyPrint(n.expression()));
case MemberNode n -> "%s.%s".formatted(prettyPrint(n.object()), prettyPrint(n.property()));
case SemicolonNode n -> "%s ; %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
case TernaryNode n -> "%s ? %s : %s".formatted(prettyPrint(n.condition()), prettyPrint(n.trueExpression()), prettyPrint(n.falseExpression()));
case UnaryEmptyNode n -> "empty %s".formatted(prettyPrint(n.node()));
case UnaryMinusNode n -> "- %s".formatted(prettyPrint(n.node()));
case UnaryNotNode n -> "! %s".formatted(prettyPrint(n.node()));
case AndNode n -> "%s && %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
case EqualNode n -> "%s == %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
case GreaterEqualNode n -> "%s >= %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
case GreaterThanNode n -> "%s > %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
case LessEqualNode n -> "%s <= %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
case LessThanNode n -> "%s < %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
case NotEqualNode n -> "%s != %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
case OrNode n -> "%s || %s".formatted(prettyPrint(n.left()), prettyPrint(n.right()));
};
}

default Object getValue(ELContext context) {
throw new UnsupportedOperationException(this.toString());
}
Expand All @@ -33,7 +75,4 @@ default Class<?> getType(ELContext context) {
throw new UnsupportedOperationException(this.toString());
}

default String prettyPrint() {
throw new UnsupportedOperationException();
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/GreaterEqualNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,4 @@ public Object getValue(ELContext context) {
}
throw new ELException("Cannot compare values: %s >= %s".formatted(leftValue, rightValue));
}

@Override
public String prettyPrint() {
return "%s >= %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/GreaterThanNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,4 @@ public Object getValue(ELContext context) {
}
throw new ELException("Cannot compare values: %s > %s".formatted(leftValue, rightValue));
}

@Override
public String prettyPrint() {
return "%s > %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
4 changes: 0 additions & 4 deletions io.joel/src/main/java/io/joel/impl/node/IdentifierNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,4 @@ public Object getValue(ELContext context) {
throw new PropertyNotFoundException("Property %s not found".formatted(value));
}

@Override
public String prettyPrint() {
return value;
}
}
7 changes: 1 addition & 6 deletions io.joel/src/main/java/io/joel/impl/node/LambdaNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
public record LambdaNode(List<String> parameters, ExpressionNode expression) implements ExpressionNode {
@Override
public Object getValue(ELContext context) {
return new LambdaExpression(parameters, new JoelValueExpression(expression.prettyPrint(), expression, Object.class));
}

@Override
public String prettyPrint() {
return "(%s) -> %s".formatted(String.join(",", parameters), expression.prettyPrint());
return new LambdaExpression(parameters, new JoelValueExpression(ExpressionNode.prettyPrint(expression), expression, Object.class));
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/LessEqualNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,4 @@ public Object getValue(ELContext context) {
}
throw new ELException("Cannot compare values: %s <= %s".formatted(leftValue, rightValue));
}

@Override
public String prettyPrint() {
return "%s <= %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/LessThanNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,4 @@ public Object getValue(ELContext context) {
}
throw new ELException("Cannot compare values: %s < %s".formatted(leftValue, rightValue));
}

@Override
public String prettyPrint() {
return "%s < %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
4 changes: 0 additions & 4 deletions io.joel/src/main/java/io/joel/impl/node/ListNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,4 @@ public Object getValue(ELContext context) {
.collect(Collectors.toList());
}

@Override
public String prettyPrint() {
return values.stream().map(ExpressionNode::prettyPrint).collect(Collectors.joining(",", "[", "]"));
}
}
7 changes: 1 addition & 6 deletions io.joel/src/main/java/io/joel/impl/node/MemberNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,11 @@ public Object getValue(ELContext context) {
return context.getELResolver().getValue(context, new ELClass(aClass), property instanceof IdentifierNode identifier ? identifier.value() : property.getValue(context));
}
}
throw new PropertyNotFoundException(prettyPrint(), rootCause);
throw new PropertyNotFoundException(ExpressionNode.prettyPrint(this), rootCause);
}
}

public ValueReference valueReference(ELContext context) {
return new ValueReference(object.getValue(context), property instanceof IdentifierNode node ? node.value() : property.getValue(context));
}

@Override
public String prettyPrint() {
return "%s.%s".formatted(object.prettyPrint(), property.prettyPrint());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@ public Object getValue(ELContext context) {
var rightValue = right.getValue(context);
return Operation.MODULO.calculate(leftValue, rightValue, context);
}

@Override
public String prettyPrint() {
return "%s %% %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@ public Object getValue(ELContext context) {
var rightValue = right.getValue(context);
return Operation.MULTIPLICATION.calculate(leftValue, rightValue, context);
}

@Override
public String prettyPrint() {
return "%s * %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/NotEqualNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,4 @@ public record NotEqualNode(ExpressionNode left, ExpressionNode right) implements
public Object getValue(ELContext context) {
return !(Boolean) new EqualNode(left, right).getValue(context);
}

@Override
public String prettyPrint() {
return "%s != %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/NullNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ public Class<?> getType(ELContext context) {
return null;
}

@Override
public String prettyPrint() {
return "null";
}

@Override
public String toString() {
return "NullNode[" + super.toString() + "]";
Expand Down
4 changes: 0 additions & 4 deletions io.joel/src/main/java/io/joel/impl/node/NumberNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,4 @@ public Object getValue(ELContext context) {
return value;
}

@Override
public String prettyPrint() {
return value.toString();
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/OrNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,4 @@ public Object getValue(ELContext context) {
return true;
return context.convertToType(right.getValue(context), boolean.class);
}

@Override
public String prettyPrint() {
return "%s || %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/SemicolonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,4 @@ public Object getValue(ELContext context) {
left.getValue(context);
return right.getValue(context);
}

@Override
public String prettyPrint() {
return "%s; %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
4 changes: 0 additions & 4 deletions io.joel/src/main/java/io/joel/impl/node/SetNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,4 @@ public Object getValue(ELContext context) {
.collect(Collectors.toSet());
}

@Override
public String prettyPrint() {
return values.stream().map(ExpressionNode::prettyPrint).collect(Collectors.joining(",", "{", "}"));
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/StringNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ public Class<?> getType(ELContext context) {
public Object getValue(ELContext context) {
return value;
}

@Override
public String prettyPrint() {
return "'%s'".formatted(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@ public Object getValue(ELContext context) {
var rightValue = right.getValue(context);
return Operation.SUBTRACTION.calculate(leftValue, rightValue, context);
}

@Override
public String prettyPrint() {
return "%s - %s".formatted(left.prettyPrint(), right.prettyPrint());
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/TernaryNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,4 @@ public Object getValue(ELContext context) {
}
return falseExpression.getValue(context);
}

@Override
public String prettyPrint() {
return "%s ? %s : %s".formatted(condition.prettyPrint(), trueExpression.prettyPrint(), falseExpression.prettyPrint());
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/UnaryEmptyNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,4 @@ public Object getValue(ELContext context) {
return true;
return value instanceof Collection<?> newValue && newValue.isEmpty();
}

@Override
public String prettyPrint() {
return "empty %s".formatted(node.prettyPrint());
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/UnaryMinusNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,4 @@ public Object getValue(ELContext context) {
}
throw new ELException("Cannot apply the '-' operator to %s".formatted(value));
}

@Override
public String prettyPrint() {
return "-%s".formatted(node.prettyPrint());
}
}
5 changes: 0 additions & 5 deletions io.joel/src/main/java/io/joel/impl/node/UnaryNotNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ public Class<?> getType(ELContext context) {
public Object getValue(ELContext context) {
return !(boolean) context.convertToType(node.getValue(context), boolean.class);
}

@Override
public String prettyPrint() {
return "! %s".formatted(node.prettyPrint());
}
}
Loading

0 comments on commit b49c9e7

Please sign in to comment.