Skip to content

Always return same instance of CurrentDate, CurrentTime, or CurrentDateTime#1467

Open
njr-11 wants to merge 3 commits into
jakartaee:mainfrom
njr-11:instances-of-currentdate-should-be-equal
Open

Always return same instance of CurrentDate, CurrentTime, or CurrentDateTime#1467
njr-11 wants to merge 3 commits into
jakartaee:mainfrom
njr-11:instances-of-currentdate-should-be-equal

Conversation

@njr-11
Copy link
Copy Markdown
Member

@njr-11 njr-11 commented May 28, 2026

Currently we are creating a new instance of CurrentDate (and likewise for CurrentTime/CurrentDateTime) every time the TemporalExpression.localDate or CurrentDate.now method is invoked, such that CurrentDate.now() is unequal to CurrentDate.now() even though both represent the same datetime expression, LOCAL DATE.

@njr-11 njr-11 added this to the 1.1 milestone May 28, 2026
}

// 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.


@Test
void testCurrentDateEqualsCurrentDate() {
assertEquals(CurrentDate.now(),
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.

I would use the assertj on it.

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.

That's fine, I switched it for you.

@njr-11
Copy link
Copy Markdown
Member Author

njr-11 commented May 29, 2026

I did think of one improvement to add: making the constructor private to enforce that only the single static instance can ever be obtained. This is covered in commit 0a0b0f0

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 ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants