Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,26 @@ public interface CurrentDate<T> extends TemporalExpression<T, LocalDate> {
*
* @return an expression representing the current date.
*/
@SuppressWarnings("unchecked")
static <T> CurrentDate<T> now() {
return new CurrentDate<>() {
@Override
public Class<LocalDate> type() {
return LocalDate.class;
}

@Override
public String toString() {
return "LOCAL DATE";
}
};
return (CurrentDate<T>) CurrentDateInstance.instance;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure I'm in the minority here, but honestly I kinda prefer an unnecessary instantiation to an unchecked cast. Yes, I get that it's perfectly sound here due to type argument erasure but even so ...

}
}

// Internal implementation of single instance obtained from CurrentDate.now()
class CurrentDateInstance implements CurrentDate<Object> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it private?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - for two reasons. Java does not allow private on top level classes. Even if it did allow that, the CurrentDate class needs access to it, so the default access level of package-private is what we want.

This comment was marked as resolved.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it inner, static, private then

Java does not allow private inner classes on interfaces. That does not compile.

I understand CS might not be used here, but there also is https://checkstyle.org/checks/design/onetoplevelclass.html#Description to consider.

This project does use checkstyle, but the OneTopLevelClass check is not enabled. I looked over that check (thanks for posting the direct link) and it does seem like a nice guideline to follow in a lot of cases but I do not see how it would add value here. Given that the CurrentDateInstance is only used by CurrentDate (which is why it would have made sense as a private static inner class if Java allowed that on interfaces) and there is no value in any other classes using it, it seems appropriate to isolate it to the same file as CurrentDate in this case.

This comment was marked as resolved.

This comment was marked as resolved.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this will not prevent using it by other classes (in the package I mean), will it?

I agree, it doesn't prevent other classes in the package from using it. Mostly, it just keeps it out of their way and isolates it to the only place that cares about it.

static final CurrentDate<?> instance = new CurrentDateInstance();

private CurrentDateInstance() {
}

@Override
public Class<LocalDate> type() {
return LocalDate.class;
}

@Override
public String toString() {
return "LOCAL DATE";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,27 @@ public interface CurrentDateTime<T> extends TemporalExpression<T, LocalDateTime>
*
* @return an expression representing the current date and time.
*/
@SuppressWarnings("unchecked")
static <T> CurrentDateTime<T> now() {
return new CurrentDateTime<>() {
@Override
public Class<LocalDateTime> type() {
return LocalDateTime.class;
}

@Override
public String toString() {
return "LOCAL DATETIME";
}
};
return (CurrentDateTime<T>) CurrentDateTimeInstance.instance;
}
}

// Internal implementation of single instance obtained from CurrentDateTime.now()
class CurrentDateTimeInstance implements CurrentDateTime<Object> {
static final CurrentDateTime<?> instance = new CurrentDateTimeInstance();

private CurrentDateTimeInstance() {
}

@Override
public Class<LocalDateTime> type() {
return LocalDateTime.class;
}

@Override
public String toString() {
return "LOCAL DATETIME";
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,26 @@ public interface CurrentTime<T> extends TemporalExpression<T, LocalTime> {
*
* @return an expression representing the current time.
*/
@SuppressWarnings("unchecked")
static <T> CurrentTime<T> now() {
return new CurrentTime<>() {
@Override
public Class<LocalTime> type() {
return LocalTime.class;
}

@Override
public String toString() {
return "LOCAL TIME";
}
};
return (CurrentTime<T>) CurrentTimeInstance.instance;
}
}

// Internal implementation of single instance obtained from CurrentTime.now()
class CurrentTimeInstance implements CurrentTime<Object> {
static final CurrentTime<?> instance = new CurrentTimeInstance();

private CurrentTimeInstance() {
}

@Override
public Class<LocalTime> type() {
return LocalTime.class;
}

@Override
public String toString() {
return "LOCAL TIME";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2025 Contributors to the Eclipse Foundation
* Copyright (c) 2025,2026 Contributors to the Eclipse Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,13 +17,17 @@
*/
package jakarta.data.expression;

import static org.assertj.core.api.Assertions.assertThat;

import jakarta.data.constraint.Between;
import jakarta.data.constraint.LessThan;
import jakarta.data.mock.entity.Book;
import jakarta.data.mock.entity._Book;
import jakarta.data.restrict.BasicRestriction;
import jakarta.data.restrict.Restriction;
import jakarta.data.spi.expression.function.CurrentDate;
import jakarta.data.spi.expression.function.CurrentDateTime;
import jakarta.data.spi.expression.function.CurrentTime;
import jakarta.data.spi.expression.literal.TemporalLiteral;

import java.time.LocalDate;
Expand Down Expand Up @@ -98,4 +102,22 @@ void shouldRestrictLocalDateBetween() {
.isEqualTo(LocalDate.of(2025, Month.APRIL, 30));
});
}

@Test
void testCurrentDateEqualsCurrentDate() {
assertThat(CurrentDate.now())
.isSameAs(CurrentDate.now());
}

@Test
void testCurrentDateTimeEqualsCurrentDateTime() {
assertThat(CurrentDateTime.now())
.isSameAs(CurrentDateTime.now());
}

@Test
void testCurrentTimeEqualsCurrentTime() {
assertThat(CurrentTime.now())
.isSameAs(CurrentTime.now());
}
}