Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

negated restrictions #905

Merged
merged 6 commits into from
Dec 18, 2024
Merged

Conversation

njr-11
Copy link
Contributor

@njr-11 njr-11 commented Dec 5, 2024

If I remember the discussion correctly, the 3 ideas for how to do negation of restrictions were:

  • restriction.negate()
  • Restrict.not(restriction)
  • Restrict.notAll(restrictions) / Restrict.notAny(restrictions)

notAll/notAny seemed the most awkward to me, and I realized that the first two are basically the same thing, and it would be trivial to have both ways, where Restrict.not(restriction) is a convenience method that invokes restriction.negate().

@njr-11 njr-11 added the enhancement New feature or request label Dec 5, 2024
@njr-11 njr-11 added this to the 1.1 milestone Dec 5, 2024
@@ -62,6 +65,23 @@ void shouldCreateBasicRestrictionWithNullValue() {
});
}

@Test
void shouldNegateLTERestriction() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I should have a test scenario case when we negate twice, I mean negate a Restriction that is already not

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean, not remove this one, but include a second one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I should have a test scenario case when we negate twice, I mean negate a Restriction that is already not

Good idea. I added tests for negating a negated restriction here:
f5d3bb9

@@ -71,6 +73,35 @@ void shouldFailIfEmptyRestrictions() {
.hasMessage("Cannot create a composite restriction without any restrictions to combine.");
}

@Test
void shouldNegateCompositeRestriction() {
Copy link
Contributor

Choose a reason for hiding this comment

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

The same here, the negate, negate scenario.

Comment on lines 42 to 43
boolean newNegation = isNegated;
Operator newComparison;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is incredibly anal, I know, but can we avoid the use of non-final instance vars here, please?

Also: remind we what version of Java we're targeting? Can we still not use switch expressions? I thought we could now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is incredibly anal, I know, but can we avoid the use of non-final instance vars here, please?

The non-final variable went away after I moved the negation detail to Operator, so this is covered now.

Also: remind we what version of Java we're targeting? Can we still not use switch expressions? I thought we could now.

Jakarta EE 11 targets Java SE 21 and Java SE 17.
Jakarta EE 12 will add Java SE 25 (and I assume drop 17 but not really sure).

It would make sense for Data 1.1 to aim for compatibility with all 3 of these Java levels. I don't know of any concerns with switch statements for these levels.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, so we can use switch expressions then, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I accepted your suggestion to use the more concise -> instead of : and followed that up with a commit to add ; at the end which was also needed. This is also nice because allowed us to get past the code checking police without needing to add the useless default throw exception. It is actually better without the default because if someone adds a new enumerated value to Operator at some point, this method will not compile until they also add the implementation for negating it.

Comment on lines 57 to 59
default:
newComparison = comparison;
newNegation = !isNegated;
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually I think it would make more sense to just add NOT_IN and NOT_LIKE operators (they exist in JDQL), and move all this logic to a negated() method on Operator.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. That makes it possible for all operations to be negated and then makes sense for negation to move to Operator as you suggested. I added this in commit c480490

@otaviojava otaviojava merged commit 06ceafe into jakartaee:main Dec 18, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants