package com.xfestudio.xfeservermanager.core.trigger;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.UUID;
/** A transport-safe, strongly typed expression tree. */
public record TriggerExpression(
UUID nodeId,
Kind kind,
String valueType,
Object literal,
String name,
List<TriggerExpression> arguments) {
public TriggerExpression {
nodeId = nodeId == null ? UUID.randomUUID() : nodeId;
Objects.requireNonNull(kind, "kind");
valueType = valueType == null || valueType.isBlank()
? "any" : valueType.strip().toLowerCase(Locale.ROOT);
if (!valueType.equals("any")) valueType = TriggerValueTypes.normalizeVariableType(valueType);
name = name == null ? "" : name.strip();
arguments = List.copyOf(arguments == null ? List.of() : arguments);
if (arguments.stream().anyMatch(Objects::isNull)) {
throw new IllegalArgumentException("expression argument must not be null");
}
if (kind.requiresName() && name.isBlank()) {
throw new IllegalArgumentException(kind.name().toLowerCase(Locale.ROOT) + " expression requires a name");
}
}
public static TriggerExpression literal(String type, Object value) {
return new TriggerExpression(UUID.randomUUID(), Kind.LITERAL, type, value, "", List.of());
}
public static TriggerExpression reference(String type, String name) {
return new TriggerExpression(UUID.randomUUID(), Kind.REFERENCE, type, null, name, List.of());
}
public enum Kind {
LITERAL,
REFERENCE,
UNARY,
BINARY,
FUNCTION,
INDEX,
COALESCE,
CONVERT;
boolean requiresName() {
return this == REFERENCE || this == UNARY || this == BINARY
|| this == FUNCTION || this == CONVERT;
}
}
}
package com.xfestudio.xfeservermanager.core.trigger;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.UUID;
/** A transport-safe, strongly typed expression tree. */
public record TriggerExpression(
UUID nodeId,
Kind kind,
String valueType,
Object literal,
String name,
List<TriggerExpression> arguments) {
public TriggerExpression {
nodeId = nodeId == null ? UUID.randomUUID() : nodeId;
Objects.requireNonNull(kind, "kind");
valueType = valueType == null || valueType.isBlank()
? "any" : valueType.strip().toLowerCase(Locale.ROOT);
if (!valueType.equals("any")) valueType = TriggerValueTypes.normalizeVariableType(valueType);
name = name == null ? "" : name.strip();
arguments = List.copyOf(arguments == null ? List.of() : arguments);
if (arguments.stream().anyMatch(Objects::isNull)) {
throw new IllegalArgumentException("expression argument must not be null");
}
if (kind.requiresName() && name.isBlank()) {
throw new IllegalArgumentException(kind.name().toLowerCase(Locale.ROOT) + " expression requires a name");
}
}
public static TriggerExpression literal(String type, Object value) {
return new TriggerExpression(UUID.randomUUID(), Kind.LITERAL, type, value, "", List.of());
}
public static TriggerExpression reference(String type, String name) {
return new TriggerExpression(UUID.randomUUID(), Kind.REFERENCE, type, null, name, List.of());
}
public enum Kind {
LITERAL,
REFERENCE,
UNARY,
BINARY,
FUNCTION,
INDEX,
COALESCE,
CONVERT;
boolean requiresName() {
return this == REFERENCE || this == UNARY || this == BINARY
|| this == FUNCTION || this == CONVERT;
}
}
}