Skip to content
Merged
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
5 changes: 5 additions & 0 deletions api/src/main/java/jakarta/data/expression/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
*/
public interface Expression<T, V> {

/**
* The type of the expression.
*/
Class<? extends V> type();

/**
* <p>Obtains a {@link Restriction} that requires that this expression
* evaluate to a value that is equal to the specified value.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public interface NumericExpression<T, N extends Number & Comparable<N>>
* @return an expression for the function that computes the absolute value.
*/
default NumericExpression<T, N> abs() {
return NumericFunctionExpression.of(ABS, this);
return NumericFunctionExpression.of(ABS, type(), this);
}

/**
Expand All @@ -82,7 +82,7 @@ default NumericExpression<T, N> abs() {
* @return an expression for the function that computes negation of value.
*/
default NumericExpression<T, N> negated() {
return NumericFunctionExpression.of(NEG, this);
return NumericFunctionExpression.of(NEG, type(), this);
}

/**
Expand Down
12 changes: 11 additions & 1 deletion api/src/main/java/jakarta/data/expression/TextExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@
*/
public interface TextExpression<T> extends ComparableExpression<T, String> {

/**
* Returns {@code String.class} as type of the textual expression.
*
* @return {@code String.class}.
*/
@Override
default Class<String> type() {
return String.class;
}

/**
* <p>Represents the function to obtain the {@link String} value that is
* formed by prepending the specified prefix onto the beginning of the
Expand Down Expand Up @@ -209,7 +219,7 @@ default TextExpression<T> right(int length) {
* textual value.
*/
default NumericExpression<T, Integer> length() {
return NumericFunctionExpression.of(LENGTH, this);
return NumericFunctionExpression.of(LENGTH, Integer.class, this);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/jakarta/data/metamodel/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ default Class<T> declaringType() {
* was used to obtain the instance.
* @since 1.1
*/
default Class<?> attributeType() {
default Class<?> type() {
throw new UnsupportedOperationException(
Messages.get("011.unknown.attr.type", getClass().getName()));
}
Expand Down
8 changes: 8 additions & 0 deletions api/src/main/java/jakarta/data/metamodel/BasicAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
*/
public interface BasicAttribute<T, V> extends Attribute<T>, Expression<T, V> {

/**
* Obtain the Java class of the entity attribute.
*
* @return the type of the entity attribute.
*/
@Override
Class<V> type();

/**
* <p>Creates a static metamodel {@code BasicAttribute} representing the
* entity attribute with the specified name.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package jakarta.data.metamodel;

record BasicAttributeRecord<T, V>
(Class<T> declaringType, String name, Class<V> attributeType)
(Class<T> declaringType, String name, Class<V> type)
implements BasicAttribute<T, V> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package jakarta.data.metamodel;

record ComparableAttributeRecord<T, V extends Comparable<?>>
(Class<T> declaringType, String name, Class<V> attributeType)
(Class<T> declaringType, String name, Class<V> type)
implements ComparableAttribute<T, V> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface NavigableAttribute<T, U>
extends Attribute<T>, NavigableExpression<T, U> {

@Override
Class<U> attributeType();
Class<U> type();

/**
* <p>Creates a static metamodel {@code NavigableAttribute} representing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package jakarta.data.metamodel;

record NavigableAttributeRecord<T, U>
(Class<T> declaringType, String name, Class<U> attributeType)
(Class<T> declaringType, String name, Class<U> type)
implements NavigableAttribute<T, U> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package jakarta.data.metamodel;

record NumericAttributeRecord<T, V extends Number & Comparable<V>>
(Class<T> declaringType, String name, Class<V> attributeType)
(Class<T> declaringType, String name, Class<V> type)
implements NumericAttribute<T, V> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package jakarta.data.metamodel;

record SortableAttributeRecord<T>
(Class<T> declaringType, String name, Class<?> attributeType)
(Class<T> declaringType, String name, Class<?> type)
implements SortableAttribute<T> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.time.temporal.Temporal;

record TemporalAttributeRecord<T, V extends Temporal & Comparable<? extends Temporal>>
(Class<T> declaringType, String name, Class<V> attributeType)
(Class<T> declaringType, String name, Class<V> type)
implements TemporalAttribute<T, V> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ default Sort<T> ascIgnoreCase() {
* @since 1.1
*/
@Override
default Class<String> attributeType() {
default Class<String> type() {
return String.class;
}

Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/jakarta/data/restrict/Restrict.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static <T> Restriction<T> all(
*/
public static <T> Restriction<T> all(
List<? extends Restriction<? super T>> restrictions) {
return new CompositeRestrictionRecord<T>(
return new CompositeRestrictionRecord<>(
CompositeRestriction.Type.ALL,
List.copyOf(restrictions));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public interface CurrentDate<T> extends TemporalExpression<T, LocalDate> {

static <T> CurrentDate<T> now() {
return new CurrentDate<>() {
@Override
public Class<LocalDate> type() {
return LocalDate.class;
}

@Override
public String toString() {
return "LOCAL DATE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public interface CurrentDateTime<T> extends TemporalExpression<T, LocalDateTime>

static <T> CurrentDateTime<T> now() {
return new CurrentDateTime<>() {
@Override
public Class<LocalDateTime> type() {
return LocalDateTime.class;
}

@Override
public String toString() {
return "LOCAL DATETIME";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public interface CurrentTime<T> extends TemporalExpression<T, LocalTime> {

static <T> CurrentTime<T> now() {
return new CurrentTime<>() {
@Override
public Class<LocalTime> type() {
return LocalTime.class;
}

@Override
public String toString() {
return "LOCAL TIME";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ public interface NumericFunctionExpression<T, N extends Number & Comparable<N>>
String LENGTH = "length";

static <T, N extends Number & Comparable<N>> NumericFunctionExpression<T, N>
of(String name, TextExpression<? super T> expression) {
of(String name, Class<N> returnType, TextExpression<? super T> expression) {
if (expression == null) {
throw new NullPointerException(
Messages.get("001.arg.required", "expression"));
}

return new NumericFunctionExpressionRecord<>(name, List.of(expression));
return new NumericFunctionExpressionRecord<>(name, returnType, List.of(expression));
}

static <T, N extends Number & Comparable<N>> NumericFunctionExpression<T, N>
of(String name, NumericExpression<? super T, N> expression) {
of(String name, Class<? extends N> returnType, NumericExpression<? super T, N> expression) {
if (expression == null) {
throw new NullPointerException(
Messages.get("001.arg.required", "expression"));
}

return new NumericFunctionExpressionRecord<>(name, List.of(expression));
return new NumericFunctionExpressionRecord<>(name, returnType, List.of(expression));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

record NumericFunctionExpressionRecord<T, N extends Number & Comparable<N>>(
String name,
Class<? extends N> returnType,
List<ComparableExpression<? super T, ?>> arguments)
implements NumericFunctionExpression<T, N> {

Expand All @@ -32,6 +33,15 @@ record NumericFunctionExpressionRecord<T, N extends Number & Comparable<N>>(
throw new NullPointerException(
Messages.get("001.arg.required", "name"));
}
if (returnType == null) {
throw new NullPointerException(
Messages.get("001.arg.required", "returnType"));
}
}

@Override
public Class<? extends N> type() {
return returnType;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package jakarta.data.spi.expression.function;

import jakarta.data.expression.Expression;
import jakarta.data.expression.NumericExpression;
import jakarta.data.messages.Messages;
import jakarta.data.spi.expression.literal.NumericLiteral;
Expand All @@ -30,9 +29,9 @@ enum Operator {

Operator operator();

Expression<? super T, N> left();
NumericExpression<? super T, N> left();

Expression<? super T, N> right();
NumericExpression<? super T, N> right();

static <T, N extends Number & Comparable<N>> NumericOperatorExpression<T, N> of(
Operator operator,
Expand All @@ -43,7 +42,7 @@ static <T, N extends Number & Comparable<N>> NumericOperatorExpression<T, N> of(
Messages.get("001.arg.required", "left"));
}

return new NumericOperatorExpressionRecord<>(operator, NumericLiteral.of(left), right);
return new NumericOperatorExpressionRecord<>(operator, NumericLiteral.of(right.type(), left), right);
}

static <T, N extends Number & Comparable<N>> NumericOperatorExpression<T, N> of(
Expand All @@ -55,7 +54,7 @@ static <T, N extends Number & Comparable<N>> NumericOperatorExpression<T, N> of(
Messages.get("001.arg.required", "right"));
}

return new NumericOperatorExpressionRecord<>(operator, left, NumericLiteral.of(right));
return new NumericOperatorExpressionRecord<>(operator, left, NumericLiteral.of(left.type(), right));
}

static <T, N extends Number & Comparable<N>> NumericOperatorExpression<T, N> of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ record NumericOperatorExpressionRecord<T, N extends Number & Comparable<N>>
}
}

@Override
public Class<? extends N> type() {
return left.type();
}

/**
* Internal method that determines if a number is equal to 0.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ static <T> TextFunctionExpression<T> of(

return new TextFunctionExpressionRecord<>(
name,
List.of(left, NumericLiteral.of(literal)));
List.of(left, NumericLiteral.of(Integer.class, literal)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.UUID;

import jakarta.data.expression.ComparableExpression;
import jakarta.data.messages.Messages;

/**
* <p>A {@linkplain Literal literal} expression for a sortable value, such as a
Expand All @@ -44,9 +45,9 @@ public interface ComparableLiteral<V extends Comparable<?>>
* {@code ComparableLiteral} that represents the given value.</p>
*
* <p>The most specific subtype of {@code ComparableLiteral}, such as
* {@link NumericLiteral#of(Number) NumericLiteral},
* {@link NumericLiteral#of(Class, Number) NumericLiteral},
* {@link StringLiteral#of(String) StringLiteral}, or
* {@link TemporalLiteral#of(java.time.temporal.Temporal) TemporalLiteral},
* {@link TemporalLiteral#of(Class, java.time.temporal.Temporal) TemporalLiteral},
* should be used instead wherever possible.</p>
*
* @param <V> entity attribute type.
Expand All @@ -63,6 +64,7 @@ static <V extends Comparable<?>> ComparableLiteral<V> of(V value) {
// and
// TemporalExpression has V extends Temporal & Comparable<? extends Temporal>
return switch (value) {
case null -> throw new NullPointerException(Messages.get("001.arg.required", "value"));
case String s -> (ComparableLiteral<V>) StringLiteral.of(s);
case Integer i -> (ComparableLiteral<V>) NumericLiteral.of(i);
case Long l -> (ComparableLiteral<V>) NumericLiteral.of(l);
Expand All @@ -77,10 +79,31 @@ static <V extends Comparable<?>> ComparableLiteral<V> of(V value) {
case LocalDateTime d -> (ComparableLiteral<V>) TemporalLiteral.of(d);
case LocalTime t -> (ComparableLiteral<V>) TemporalLiteral.of(t);
case Year y -> (ComparableLiteral<V>) TemporalLiteral.of(y);
case null, default -> new ComparableLiteralRecord<>(value);
default -> new ComparableLiteralRecord<>((Class<? extends V>) value.getClass(), value);
};
}

/**
* Create a {@code ComparableLiteral} representing the given {@code boolean}.
*/
static ComparableLiteral<Boolean> of(boolean value) {
return new ComparableLiteralRecord<>(Boolean.class, value);
}

/**
* Create a {@code ComparableLiteral} representing the given {@code char}.
*/
static ComparableLiteral<Character> of(char value) {
return new ComparableLiteralRecord<>(Character.class, value);
}

/**
* Create a {@code ComparableLiteral} representing the given {@link UUID}.
*/
static ComparableLiteral<UUID> of(UUID value) {
return new ComparableLiteralRecord<>(UUID.class, value);
}

/**
* <p>Returns a {@code String} representing the literal value.</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import jakarta.data.messages.Messages;

record ComparableLiteralRecord<T, V extends Comparable<?>>(V value)
record ComparableLiteralRecord<V extends Comparable<?>>(Class<? extends V> type, V value)
implements ComparableLiteral<V> {

ComparableLiteralRecord {
Expand All @@ -28,7 +28,6 @@ record ComparableLiteralRecord<T, V extends Comparable<?>>(V value)
Messages.get("001.arg.required", "value"));
}
}

@Override
public String toString() {
return switch (value) {
Expand Down
Loading