From 83c127ffa2c82d7390b851880df564e17ec3e464 Mon Sep 17 00:00:00 2001 From: Nathan Rauh Date: Thu, 5 Dec 2024 16:54:36 -0600 Subject: [PATCH 1/6] Issue #829 negated restrictions --- .../jakarta/data/BasicRestrictionRecord.java | 29 ++++++++++++++++ .../data/CompositeRestrictionRecord.java | 5 +++ api/src/main/java/jakarta/data/Restrict.java | 7 ++++ .../main/java/jakarta/data/Restriction.java | 2 ++ .../java/jakarta/data/TextRestriction.java | 5 ++- .../jakarta/data/TextRestrictionRecord.java | 33 ++++++++++++++++++- 6 files changed, 79 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/jakarta/data/BasicRestrictionRecord.java b/api/src/main/java/jakarta/data/BasicRestrictionRecord.java index e58ca18f..f426a8c5 100644 --- a/api/src/main/java/jakarta/data/BasicRestrictionRecord.java +++ b/api/src/main/java/jakarta/data/BasicRestrictionRecord.java @@ -36,4 +36,33 @@ record BasicRestrictionRecord( BasicRestrictionRecord(String field, Operator comparison, Object value) { this(field, false, comparison, value); } + + @Override + public Restriction negate() { + boolean newNegation = isNegated; + Operator newComparison; + switch (comparison) { + case GREATER_THAN: + newComparison = Operator.LESS_THAN_EQUAL; + break; + case GREATER_THAN_EQUAL: + newComparison = Operator.LESS_THAN; + break; + case LESS_THAN: + newComparison = Operator.GREATER_THAN_EQUAL; + break; + case LESS_THAN_EQUAL: + newComparison = Operator.GREATER_THAN; + break; + default: + newComparison = comparison; + newNegation = !isNegated; + } + + return new BasicRestrictionRecord<>( + field, + newNegation, + newComparison, + value); + } } diff --git a/api/src/main/java/jakarta/data/CompositeRestrictionRecord.java b/api/src/main/java/jakarta/data/CompositeRestrictionRecord.java index a8cd5e0f..32a0e416 100644 --- a/api/src/main/java/jakarta/data/CompositeRestrictionRecord.java +++ b/api/src/main/java/jakarta/data/CompositeRestrictionRecord.java @@ -38,4 +38,9 @@ record CompositeRestrictionRecord( CompositeRestrictionRecord(Type type, List> restrictions) { this(type, restrictions, false); } + + @Override + public Restriction negate() { + return new CompositeRestrictionRecord<>(type, restrictions, !isNegated); + } } \ No newline at end of file diff --git a/api/src/main/java/jakarta/data/Restrict.java b/api/src/main/java/jakarta/data/Restrict.java index 96b24d8e..1ad9ead3 100644 --- a/api/src/main/java/jakarta/data/Restrict.java +++ b/api/src/main/java/jakarta/data/Restrict.java @@ -18,6 +18,7 @@ package jakarta.data; import java.util.List; +import java.util.Objects; import java.util.Set; // TODO document @@ -132,6 +133,12 @@ public static TextRestriction like(String pattern, return new TextRestrictionRecord<>(field, Operator.LIKE, ESCAPED, p); } + // convenience method for those who would prefer to avoid .negate() + public static Restriction not(Restriction restriction) { + Objects.requireNonNull(restriction, "Restriction must not be null"); + return restriction.negate(); + } + public static Restriction notEqualTo(Object value, String field) { return new BasicRestrictionRecord<>(field, NOT, Operator.EQUAL, value); } diff --git a/api/src/main/java/jakarta/data/Restriction.java b/api/src/main/java/jakarta/data/Restriction.java index 0548fb4f..6bd1eeac 100644 --- a/api/src/main/java/jakarta/data/Restriction.java +++ b/api/src/main/java/jakarta/data/Restriction.java @@ -19,4 +19,6 @@ public interface Restriction { boolean isNegated(); + + Restriction negate(); } diff --git a/api/src/main/java/jakarta/data/TextRestriction.java b/api/src/main/java/jakarta/data/TextRestriction.java index b6c9850c..d1a44fa8 100644 --- a/api/src/main/java/jakarta/data/TextRestriction.java +++ b/api/src/main/java/jakarta/data/TextRestriction.java @@ -18,7 +18,7 @@ package jakarta.data; public interface TextRestriction extends BasicRestriction { - Restriction ignoreCase(); + TextRestriction ignoreCase(); // TODO can mention in the JavaDoc that a value of true will be ignored // if the database is not not capable of case sensitive comparisons @@ -26,6 +26,9 @@ public interface TextRestriction extends BasicRestriction { boolean isEscaped(); + @Override + TextRestriction negate(); + @Override String value(); } diff --git a/api/src/main/java/jakarta/data/TextRestrictionRecord.java b/api/src/main/java/jakarta/data/TextRestrictionRecord.java index 831fec41..87cbe884 100644 --- a/api/src/main/java/jakarta/data/TextRestrictionRecord.java +++ b/api/src/main/java/jakarta/data/TextRestrictionRecord.java @@ -52,7 +52,38 @@ record TextRestrictionRecord( } @Override - public Restriction ignoreCase() { + public TextRestriction ignoreCase() { return new TextRestrictionRecord<>(field, isNegated, comparison, false, isEscaped, value); } + + @Override + public TextRestriction negate() { + boolean newNegation = isNegated; + Operator newComparison; + switch (comparison) { + case GREATER_THAN: + newComparison = Operator.LESS_THAN_EQUAL; + break; + case GREATER_THAN_EQUAL: + newComparison = Operator.LESS_THAN; + break; + case LESS_THAN: + newComparison = Operator.GREATER_THAN_EQUAL; + break; + case LESS_THAN_EQUAL: + newComparison = Operator.GREATER_THAN; + break; + default: + newComparison = comparison; + newNegation = !isNegated; + } + + return new TextRestrictionRecord<>( + field, + newNegation, + newComparison, + isCaseSensitive, + isEscaped, + value); + } } From 5240da69593cacf24a4fc0c1ef1509c22c4151d6 Mon Sep 17 00:00:00 2001 From: Nathan Rauh Date: Thu, 5 Dec 2024 16:55:38 -0600 Subject: [PATCH 2/6] Issue #829 unit tests for negated restrictions --- .../data/BasicRestrictionRecordTest.java | 20 ++++++++ .../data/CompositeRestrictionRecordTest.java | 33 ++++++++++++- .../data/TextRestrictionRecordTest.java | 49 +++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) diff --git a/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java b/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java index c46bfb0d..6c540e46 100644 --- a/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java +++ b/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java @@ -24,6 +24,9 @@ class BasicRestrictionRecordTest { + // A mock entity class for tests + static class Book { + } @Test void shouldCreateBasicRestrictionWithDefaultNegation() { @@ -62,6 +65,23 @@ void shouldCreateBasicRestrictionWithNullValue() { }); } + @Test + void shouldNegateLTERestriction() { + Restriction numChaptersLTE10 = Restrict.lessThanEqual(10, "numChapters"); + BasicRestriction numChaptersLTE10Basic = (BasicRestriction) numChaptersLTE10; + BasicRestriction numChaptersGT10Basic = (BasicRestriction) numChaptersLTE10Basic.negate(); + + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(numChaptersLTE10Basic.comparison()).isEqualTo(Operator.LESS_THAN_EQUAL); + soft.assertThat(numChaptersLTE10Basic.value()).isEqualTo(10); + soft.assertThat(numChaptersLTE10Basic.isNegated()).isEqualTo(false); + + soft.assertThat(numChaptersGT10Basic.comparison()).isEqualTo(Operator.GREATER_THAN); + soft.assertThat(numChaptersGT10Basic.value()).isEqualTo(10); + soft.assertThat(numChaptersGT10Basic.isNegated()).isEqualTo(false); + }); + } + @Test void shouldSupportNegatedRestrictionUsingDefaultConstructor() { BasicRestrictionRecord restriction = new BasicRestrictionRecord<>("author", Operator.EQUAL, "Unknown"); diff --git a/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java b/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java index 79764f64..b80c182c 100644 --- a/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java +++ b/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java @@ -27,7 +27,9 @@ class CompositeRestrictionRecordTest { - + // A mock entity class for tests + static class Person { + } @Test void shouldCreateCompositeRestrictionWithDefaultNegation() { @@ -71,6 +73,35 @@ void shouldFailIfEmptyRestrictions() { .hasMessage("Cannot create a composite restriction without any restrictions to combine."); } + @Test + void shouldNegateCompositeRestriction() { + Restriction ageLessThan50 = Restrict.lessThan(50, "age"); + Restriction nameStartsWithDuke = Restrict.startsWith("Duke ", "name"); + Restriction all = Restrict.all(ageLessThan50, nameStartsWithDuke); + Restriction allNegated = all.negate(); + Restriction notAll = Restrict.not(all); + + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(all.isNegated()).isEqualTo(false); + soft.assertThat(((CompositeRestriction) all).restrictions() + .get(0).isNegated()).isEqualTo(false); + soft.assertThat(((CompositeRestriction) all).restrictions() + .get(1).isNegated()).isEqualTo(false); + + soft.assertThat(allNegated.isNegated()).isEqualTo(true); + soft.assertThat(((CompositeRestriction) allNegated).restrictions() + .get(0).isNegated()).isEqualTo(false); + soft.assertThat(((CompositeRestriction) allNegated).restrictions() + .get(1).isNegated()).isEqualTo(false); + + soft.assertThat(notAll.isNegated()).isEqualTo(true); + soft.assertThat(((CompositeRestriction) notAll).restrictions() + .get(0).isNegated()).isEqualTo(false); + soft.assertThat(((CompositeRestriction) notAll).restrictions() + .get(1).isNegated()).isEqualTo(false); + }); + } + @Test void shouldPreserveRestrictionsOrder() { Restriction restriction1 = new BasicRestrictionRecord<>("title", Operator.EQUAL, "Java Guide"); diff --git a/api/src/test/java/jakarta/data/TextRestrictionRecordTest.java b/api/src/test/java/jakarta/data/TextRestrictionRecordTest.java index 5b7dcb2a..3dcc5369 100644 --- a/api/src/test/java/jakarta/data/TextRestrictionRecordTest.java +++ b/api/src/test/java/jakarta/data/TextRestrictionRecordTest.java @@ -20,6 +20,8 @@ import org.assertj.core.api.SoftAssertions; import org.junit.jupiter.api.Test; +import jakarta.data.BasicRestrictionRecordTest.Book; + import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; @@ -103,6 +105,53 @@ void shouldCreateTextRestrictionWithEscapedValue() { }); } + @Test + void shouldNegateLikeRestriction() { + TextRestriction likeJakartaEE = Restrict.like("%Jakarta EE%", "title"); + TextRestriction notLikeJakartaEE = likeJakartaEE.negate(); + TextRestriction anyCaseNotLikeJakartaEE = likeJakartaEE.ignoreCase().negate(); + TextRestriction notLikeJakartaEEAnyCase = likeJakartaEE.negate().ignoreCase(); + + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(likeJakartaEE.isNegated()).isFalse(); + soft.assertThat(likeJakartaEE.comparison()).isEqualTo(Operator.LIKE); + soft.assertThat(likeJakartaEE.isCaseSensitive()).isTrue(); + + soft.assertThat(notLikeJakartaEE.isNegated()).isTrue(); + soft.assertThat(notLikeJakartaEE.comparison()).isEqualTo(Operator.LIKE); + soft.assertThat(notLikeJakartaEE.isCaseSensitive()).isTrue(); + + soft.assertThat(anyCaseNotLikeJakartaEE.isNegated()).isTrue(); + soft.assertThat(anyCaseNotLikeJakartaEE.comparison()).isEqualTo(Operator.LIKE); + soft.assertThat(anyCaseNotLikeJakartaEE.isCaseSensitive()).isFalse(); + + soft.assertThat(notLikeJakartaEEAnyCase.isNegated()).isTrue(); + soft.assertThat(notLikeJakartaEEAnyCase.comparison()).isEqualTo(Operator.LIKE); + soft.assertThat(notLikeJakartaEEAnyCase.isCaseSensitive()).isFalse(); + }); + } + + @Test + void shouldNegateNegatedRestriction() { + TextRestriction endsWithJakartaEE = Restrict.endsWith("Jakarta EE", "title"); + TextRestriction notEndsWithJakartaEE = endsWithJakartaEE.negate(); + TextRestriction notNotEndsWithJakartaEE = notEndsWithJakartaEE.negate(); + + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(endsWithJakartaEE.isNegated()).isFalse(); + soft.assertThat(endsWithJakartaEE.comparison()).isEqualTo(Operator.LIKE); + soft.assertThat(endsWithJakartaEE.value()).isEqualTo("%Jakarta EE"); + + soft.assertThat(notEndsWithJakartaEE.isNegated()).isTrue(); + soft.assertThat(notEndsWithJakartaEE.comparison()).isEqualTo(Operator.LIKE); + soft.assertThat(notEndsWithJakartaEE.value()).isEqualTo("%Jakarta EE"); + + soft.assertThat(notNotEndsWithJakartaEE.isNegated()).isFalse(); + soft.assertThat(notNotEndsWithJakartaEE.comparison()).isEqualTo(Operator.LIKE); + soft.assertThat(notNotEndsWithJakartaEE.value()).isEqualTo("%Jakarta EE"); + }); + } + @Test void shouldSupportNegationForTextRestriction() { TextRestrictionRecord restriction = new TextRestrictionRecord<>( From f5d3bb91143dd534ab0cda7089cf80b906a9dbc7 Mon Sep 17 00:00:00 2001 From: Nathan Rauh Date: Fri, 6 Dec 2024 08:47:44 -0600 Subject: [PATCH 3/6] Tests for double negation --- .../data/BasicRestrictionRecordTest.java | 35 +++++++++++++++++++ .../data/CompositeRestrictionRecordTest.java | 29 +++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java b/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java index 6c540e46..ee41345e 100644 --- a/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java +++ b/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java @@ -82,6 +82,41 @@ void shouldNegateLTERestriction() { }); } + @Test + void shouldNegateNegatedRestriction() { + Restriction titleRestriction = + Restrict.equalTo("A Developer's Guide to Jakarta Data", "title"); + BasicRestriction titleRestrictionBasic = + (BasicRestriction) titleRestriction; + BasicRestriction negatedTitleRestrictionBasic = + (BasicRestriction) titleRestriction.negate(); + BasicRestriction negatedNegatedTitleRestrictionBasic = + (BasicRestriction) negatedTitleRestrictionBasic.negate(); + + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(titleRestrictionBasic.comparison()) + .isEqualTo(Operator.EQUAL); + soft.assertThat(titleRestrictionBasic.value()) + .isEqualTo("A Developer's Guide to Jakarta Data"); + soft.assertThat(titleRestrictionBasic.isNegated()) + .isEqualTo(false); + + soft.assertThat(negatedTitleRestrictionBasic.comparison()) + .isEqualTo(Operator.EQUAL); + soft.assertThat(negatedTitleRestrictionBasic.value()) + .isEqualTo("A Developer's Guide to Jakarta Data"); + soft.assertThat(negatedTitleRestrictionBasic.isNegated()) + .isEqualTo(true); + + soft.assertThat(negatedNegatedTitleRestrictionBasic.comparison()) + .isEqualTo(Operator.EQUAL); + soft.assertThat(negatedNegatedTitleRestrictionBasic.value()) + .isEqualTo("A Developer's Guide to Jakarta Data"); + soft.assertThat(negatedNegatedTitleRestrictionBasic.isNegated()) + .isEqualTo(false); + }); + } + @Test void shouldSupportNegatedRestrictionUsingDefaultConstructor() { BasicRestrictionRecord restriction = new BasicRestrictionRecord<>("author", Operator.EQUAL, "Unknown"); diff --git a/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java b/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java index b80c182c..c4acf781 100644 --- a/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java +++ b/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java @@ -102,6 +102,35 @@ void shouldNegateCompositeRestriction() { }); } + @Test + void shouldNegateNegatedCompositeRestriction() { + Restriction ageBetween20and30 = Restrict.between(20, 30, "age"); + Restriction nameContainsDuke = Restrict.contains("Duke", "name"); + Restriction any = Restrict.any(ageBetween20and30, nameContainsDuke); + Restriction anyNegated = any.negate(); + Restriction anyNotNegated = Restrict.not(anyNegated); + + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(any.isNegated()).isEqualTo(false); + soft.assertThat(((CompositeRestriction) any).restrictions() + .get(0).isNegated()).isEqualTo(false); + soft.assertThat(((CompositeRestriction) any).restrictions() + .get(1).isNegated()).isEqualTo(false); + + soft.assertThat(anyNegated.isNegated()).isEqualTo(true); + soft.assertThat(((CompositeRestriction) anyNegated).restrictions() + .get(0).isNegated()).isEqualTo(false); + soft.assertThat(((CompositeRestriction) anyNegated).restrictions() + .get(1).isNegated()).isEqualTo(false); + + soft.assertThat(anyNotNegated.isNegated()).isEqualTo(false); + soft.assertThat(((CompositeRestriction) anyNotNegated).restrictions() + .get(0).isNegated()).isEqualTo(false); + soft.assertThat(((CompositeRestriction) anyNotNegated).restrictions() + .get(1).isNegated()).isEqualTo(false); + }); + } + @Test void shouldPreserveRestrictionsOrder() { Restriction restriction1 = new BasicRestrictionRecord<>("title", Operator.EQUAL, "Java Guide"); From c480490190e05b90954166d48eb66d2d970280aa Mon Sep 17 00:00:00 2001 From: Nathan Rauh Date: Mon, 9 Dec 2024 09:36:29 -0600 Subject: [PATCH 4/6] Address review comments from Gavin --- .../java/jakarta/data/BasicRestriction.java | 3 ++ .../jakarta/data/BasicRestrictionRecord.java | 30 +------------- .../jakarta/data/CompositeRestriction.java | 5 +++ .../data/CompositeRestrictionRecord.java | 2 +- api/src/main/java/jakarta/data/Operator.java | 37 ++++++++++++++++- api/src/main/java/jakarta/data/Restrict.java | 17 ++++---- .../main/java/jakarta/data/Restriction.java | 2 - .../jakarta/data/TextRestrictionRecord.java | 37 ++--------------- .../data/BasicRestrictionRecordTest.java | 26 ++++-------- .../data/CompositeRestrictionRecordTest.java | 40 +++++-------------- .../test/java/jakarta/data/RestrictTest.java | 16 ++------ .../data/TextRestrictionRecordTest.java | 30 ++++---------- .../jakarta/data/metamodel/AttributeTest.java | 12 ++---- .../data/metamodel/SortableAttributeTest.java | 6 --- .../data/metamodel/TextAttributeTest.java | 16 ++------ 15 files changed, 96 insertions(+), 183 deletions(-) diff --git a/api/src/main/java/jakarta/data/BasicRestriction.java b/api/src/main/java/jakarta/data/BasicRestriction.java index f7c3b167..0cc17338 100644 --- a/api/src/main/java/jakarta/data/BasicRestriction.java +++ b/api/src/main/java/jakarta/data/BasicRestriction.java @@ -22,5 +22,8 @@ public interface BasicRestriction extends Restriction { String field(); + @Override + BasicRestriction negate(); + Object value(); } diff --git a/api/src/main/java/jakarta/data/BasicRestrictionRecord.java b/api/src/main/java/jakarta/data/BasicRestrictionRecord.java index f426a8c5..dfedc845 100644 --- a/api/src/main/java/jakarta/data/BasicRestrictionRecord.java +++ b/api/src/main/java/jakarta/data/BasicRestrictionRecord.java @@ -25,7 +25,6 @@ record BasicRestrictionRecord( String field, - boolean isNegated, Operator comparison, Object value) implements BasicRestriction { @@ -33,36 +32,11 @@ record BasicRestrictionRecord( Objects.requireNonNull(field, "Field must not be null"); } - BasicRestrictionRecord(String field, Operator comparison, Object value) { - this(field, false, comparison, value); - } - @Override - public Restriction negate() { - boolean newNegation = isNegated; - Operator newComparison; - switch (comparison) { - case GREATER_THAN: - newComparison = Operator.LESS_THAN_EQUAL; - break; - case GREATER_THAN_EQUAL: - newComparison = Operator.LESS_THAN; - break; - case LESS_THAN: - newComparison = Operator.GREATER_THAN_EQUAL; - break; - case LESS_THAN_EQUAL: - newComparison = Operator.GREATER_THAN; - break; - default: - newComparison = comparison; - newNegation = !isNegated; - } - + public BasicRestriction negate() { return new BasicRestrictionRecord<>( field, - newNegation, - newComparison, + comparison.negate(), value); } } diff --git a/api/src/main/java/jakarta/data/CompositeRestriction.java b/api/src/main/java/jakarta/data/CompositeRestriction.java index 1eeffbf9..5c5a8ab6 100644 --- a/api/src/main/java/jakarta/data/CompositeRestriction.java +++ b/api/src/main/java/jakarta/data/CompositeRestriction.java @@ -20,6 +20,11 @@ import java.util.List; public interface CompositeRestriction extends Restriction { + boolean isNegated(); + + @Override + CompositeRestriction negate(); + List> restrictions(); Type type(); diff --git a/api/src/main/java/jakarta/data/CompositeRestrictionRecord.java b/api/src/main/java/jakarta/data/CompositeRestrictionRecord.java index 32a0e416..40b8d612 100644 --- a/api/src/main/java/jakarta/data/CompositeRestrictionRecord.java +++ b/api/src/main/java/jakarta/data/CompositeRestrictionRecord.java @@ -40,7 +40,7 @@ record CompositeRestrictionRecord( } @Override - public Restriction negate() { + public CompositeRestriction negate() { return new CompositeRestrictionRecord<>(type, restrictions, !isNegated); } } \ No newline at end of file diff --git a/api/src/main/java/jakarta/data/Operator.java b/api/src/main/java/jakarta/data/Operator.java index a8d448c8..36b46aa5 100644 --- a/api/src/main/java/jakarta/data/Operator.java +++ b/api/src/main/java/jakarta/data/Operator.java @@ -24,5 +24,40 @@ public enum Operator { IN, LESS_THAN, LESS_THAN_EQUAL, - LIKE + LIKE, + NOT_EQUAL, + NOT_IN, + NOT_LIKE; + + /** + * Returns the operator that is the negation of this operator. + * + * @return the operator that is the negation of this operator. + */ + Operator negate() { + switch (this) { + case EQUAL: + return NOT_EQUAL; + case GREATER_THAN: + return LESS_THAN_EQUAL; + case GREATER_THAN_EQUAL: + return LESS_THAN; + case IN: + return NOT_IN; + case LESS_THAN: + return GREATER_THAN_EQUAL; + case LESS_THAN_EQUAL: + return GREATER_THAN; + case LIKE: + return NOT_LIKE; + case NOT_EQUAL: + return EQUAL; + case NOT_IN: + return IN; + case NOT_LIKE: + return LIKE; + default: // should be unreachable + throw new IllegalStateException(name()); + } + } } diff --git a/api/src/main/java/jakarta/data/Restrict.java b/api/src/main/java/jakarta/data/Restrict.java index 1ad9ead3..c9e402ad 100644 --- a/api/src/main/java/jakarta/data/Restrict.java +++ b/api/src/main/java/jakarta/data/Restrict.java @@ -32,7 +32,6 @@ public class Restrict { // used internally for more readable code private static final boolean ESCAPED = true; - private static final boolean NOT = true; private static final char STRING_WILDCARD = '%'; @@ -140,29 +139,29 @@ public static Restriction not(Restriction restriction) { } public static Restriction notEqualTo(Object value, String field) { - return new BasicRestrictionRecord<>(field, NOT, Operator.EQUAL, value); + return new BasicRestrictionRecord<>(field, Operator.NOT_EQUAL, value); } public static TextRestriction notEqualTo(String value, String field) { - return new TextRestrictionRecord<>(field, NOT, Operator.EQUAL, value); + return new TextRestrictionRecord<>(field, Operator.NOT_EQUAL, value); } public static TextRestriction notContains(String substring, String field) { String pattern = toLikeEscaped(CHAR_WILDCARD, STRING_WILDCARD, true, substring, true); - return new TextRestrictionRecord<>(field, NOT, Operator.LIKE, ESCAPED, pattern); + return new TextRestrictionRecord<>(field, Operator.NOT_LIKE, ESCAPED, pattern); } public static TextRestriction notEndsWith(String suffix, String field) { String pattern = toLikeEscaped(CHAR_WILDCARD, STRING_WILDCARD, true, suffix, false); - return new TextRestrictionRecord<>(field, NOT, Operator.LIKE, ESCAPED, pattern); + return new TextRestrictionRecord<>(field, Operator.NOT_LIKE, ESCAPED, pattern); } public static Restriction notIn(Set values, String field) { - return new BasicRestrictionRecord<>(field, NOT, Operator.IN, values); + return new BasicRestrictionRecord<>(field, Operator.NOT_IN, values); } public static TextRestriction notLike(String pattern, String field) { - return new TextRestrictionRecord<>(field, NOT, Operator.LIKE, pattern); + return new TextRestrictionRecord<>(field, Operator.NOT_LIKE, pattern); } public static TextRestriction notLike(String pattern, @@ -170,12 +169,12 @@ public static TextRestriction notLike(String pattern, char stringWildcard, String field) { String p = toLikeEscaped(charWildcard, stringWildcard, false, pattern, false); - return new TextRestrictionRecord<>(field, NOT, Operator.LIKE, ESCAPED, p); + return new TextRestrictionRecord<>(field, Operator.NOT_LIKE, ESCAPED, p); } public static TextRestriction notStartsWith(String prefix, String field) { String pattern = toLikeEscaped(CHAR_WILDCARD, STRING_WILDCARD, false, prefix, true); - return new TextRestrictionRecord<>(field, NOT, Operator.LIKE, ESCAPED, pattern); + return new TextRestrictionRecord<>(field, Operator.NOT_LIKE, ESCAPED, pattern); } public static TextRestriction startsWith(String prefix, String field) { diff --git a/api/src/main/java/jakarta/data/Restriction.java b/api/src/main/java/jakarta/data/Restriction.java index 6bd1eeac..91504c71 100644 --- a/api/src/main/java/jakarta/data/Restriction.java +++ b/api/src/main/java/jakarta/data/Restriction.java @@ -18,7 +18,5 @@ package jakarta.data; public interface Restriction { - boolean isNegated(); - Restriction negate(); } diff --git a/api/src/main/java/jakarta/data/TextRestrictionRecord.java b/api/src/main/java/jakarta/data/TextRestrictionRecord.java index 87cbe884..c957860e 100644 --- a/api/src/main/java/jakarta/data/TextRestrictionRecord.java +++ b/api/src/main/java/jakarta/data/TextRestrictionRecord.java @@ -25,7 +25,6 @@ record TextRestrictionRecord( String field, - boolean isNegated, Operator comparison, boolean isCaseSensitive, boolean isEscaped, @@ -35,53 +34,25 @@ record TextRestrictionRecord( Objects.requireNonNull(field, "Field must not be null"); } - TextRestrictionRecord(String field, boolean negated, Operator comparison, boolean escaped, String value) { - this(field, negated, comparison, true, escaped, value); - } - - TextRestrictionRecord(String field, boolean negated, Operator comparison, String value) { - this(field, negated, comparison, true, false, value); - } - TextRestrictionRecord(String field, Operator comparison, boolean escaped, String value) { - this(field, false, comparison, true, escaped, value); + this(field, comparison, true, escaped, value); } TextRestrictionRecord(String field, Operator comparison, String value) { - this(field, false, comparison, true, false, value); + this(field, comparison, true, false, value); } @Override public TextRestriction ignoreCase() { - return new TextRestrictionRecord<>(field, isNegated, comparison, false, isEscaped, value); + return new TextRestrictionRecord<>(field, comparison, false, isEscaped, value); } @Override public TextRestriction negate() { - boolean newNegation = isNegated; - Operator newComparison; - switch (comparison) { - case GREATER_THAN: - newComparison = Operator.LESS_THAN_EQUAL; - break; - case GREATER_THAN_EQUAL: - newComparison = Operator.LESS_THAN; - break; - case LESS_THAN: - newComparison = Operator.GREATER_THAN_EQUAL; - break; - case LESS_THAN_EQUAL: - newComparison = Operator.GREATER_THAN; - break; - default: - newComparison = comparison; - newNegation = !isNegated; - } return new TextRestrictionRecord<>( field, - newNegation, - newComparison, + comparison.negate(), isCaseSensitive, isEscaped, value); diff --git a/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java b/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java index ee41345e..e0c3c2c1 100644 --- a/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java +++ b/api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java @@ -34,7 +34,6 @@ void shouldCreateBasicRestrictionWithDefaultNegation() { SoftAssertions.assertSoftly(soft -> { soft.assertThat(restriction.field()).isEqualTo("title"); - soft.assertThat(restriction.isNegated()).isFalse(); soft.assertThat(restriction.comparison()).isEqualTo(Operator.EQUAL); soft.assertThat(restriction.value()).isEqualTo("Java Guide"); }); @@ -42,12 +41,13 @@ void shouldCreateBasicRestrictionWithDefaultNegation() { @Test void shouldCreateBasicRestrictionWithExplicitNegation() { - BasicRestrictionRecord restriction = new BasicRestrictionRecord<>("title", true, Operator.EQUAL, "Java Guide"); + BasicRestriction restriction = + (BasicRestriction) Restrict.equalTo("Java Guide", "title") + .negate(); SoftAssertions.assertSoftly(soft -> { soft.assertThat(restriction.field()).isEqualTo("title"); - soft.assertThat(restriction.isNegated()).isTrue(); - soft.assertThat(restriction.comparison()).isEqualTo(Operator.EQUAL); + soft.assertThat(restriction.comparison()).isEqualTo(Operator.NOT_EQUAL); soft.assertThat(restriction.value()).isEqualTo("Java Guide"); }); } @@ -59,7 +59,6 @@ void shouldCreateBasicRestrictionWithNullValue() { SoftAssertions.assertSoftly(soft -> { soft.assertThat(restriction.field()).isEqualTo("title"); - soft.assertThat(restriction.isNegated()).isFalse(); soft.assertThat(restriction.comparison()).isEqualTo(Operator.EQUAL); soft.assertThat(restriction.value()).isNull(); }); @@ -74,11 +73,9 @@ void shouldNegateLTERestriction() { SoftAssertions.assertSoftly(soft -> { soft.assertThat(numChaptersLTE10Basic.comparison()).isEqualTo(Operator.LESS_THAN_EQUAL); soft.assertThat(numChaptersLTE10Basic.value()).isEqualTo(10); - soft.assertThat(numChaptersLTE10Basic.isNegated()).isEqualTo(false); soft.assertThat(numChaptersGT10Basic.comparison()).isEqualTo(Operator.GREATER_THAN); soft.assertThat(numChaptersGT10Basic.value()).isEqualTo(10); - soft.assertThat(numChaptersGT10Basic.isNegated()).isEqualTo(false); }); } @@ -98,34 +95,27 @@ void shouldNegateNegatedRestriction() { .isEqualTo(Operator.EQUAL); soft.assertThat(titleRestrictionBasic.value()) .isEqualTo("A Developer's Guide to Jakarta Data"); - soft.assertThat(titleRestrictionBasic.isNegated()) - .isEqualTo(false); soft.assertThat(negatedTitleRestrictionBasic.comparison()) - .isEqualTo(Operator.EQUAL); + .isEqualTo(Operator.NOT_EQUAL); soft.assertThat(negatedTitleRestrictionBasic.value()) .isEqualTo("A Developer's Guide to Jakarta Data"); - soft.assertThat(negatedTitleRestrictionBasic.isNegated()) - .isEqualTo(true); soft.assertThat(negatedNegatedTitleRestrictionBasic.comparison()) .isEqualTo(Operator.EQUAL); soft.assertThat(negatedNegatedTitleRestrictionBasic.value()) .isEqualTo("A Developer's Guide to Jakarta Data"); - soft.assertThat(negatedNegatedTitleRestrictionBasic.isNegated()) - .isEqualTo(false); }); } @Test void shouldSupportNegatedRestrictionUsingDefaultConstructor() { - BasicRestrictionRecord restriction = new BasicRestrictionRecord<>("author", Operator.EQUAL, "Unknown"); - BasicRestrictionRecord negatedRestriction = new BasicRestrictionRecord<>(restriction.field(), true, restriction.comparison(), restriction.value()); + BasicRestriction negatedRestriction = + (BasicRestriction) Restrict.notEqualTo((Object) "Unknown", "author"); SoftAssertions.assertSoftly(soft -> { soft.assertThat(negatedRestriction.field()).isEqualTo("author"); - soft.assertThat(negatedRestriction.isNegated()).isTrue(); - soft.assertThat(negatedRestriction.comparison()).isEqualTo(Operator.EQUAL); + soft.assertThat(negatedRestriction.comparison()).isEqualTo(Operator.NOT_EQUAL); soft.assertThat(negatedRestriction.value()).isEqualTo("Unknown"); }); } diff --git a/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java b/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java index c4acf781..67e3f9df 100644 --- a/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java +++ b/api/src/test/java/jakarta/data/CompositeRestrictionRecordTest.java @@ -77,28 +77,18 @@ void shouldFailIfEmptyRestrictions() { void shouldNegateCompositeRestriction() { Restriction ageLessThan50 = Restrict.lessThan(50, "age"); Restriction nameStartsWithDuke = Restrict.startsWith("Duke ", "name"); - Restriction all = Restrict.all(ageLessThan50, nameStartsWithDuke); - Restriction allNegated = all.negate(); - Restriction notAll = Restrict.not(all); + CompositeRestriction all = + (CompositeRestriction) Restrict.all(ageLessThan50, nameStartsWithDuke); + CompositeRestriction allNegated = all.negate(); + CompositeRestriction notAll = + (CompositeRestriction) Restrict.not(all); SoftAssertions.assertSoftly(soft -> { soft.assertThat(all.isNegated()).isEqualTo(false); - soft.assertThat(((CompositeRestriction) all).restrictions() - .get(0).isNegated()).isEqualTo(false); - soft.assertThat(((CompositeRestriction) all).restrictions() - .get(1).isNegated()).isEqualTo(false); soft.assertThat(allNegated.isNegated()).isEqualTo(true); - soft.assertThat(((CompositeRestriction) allNegated).restrictions() - .get(0).isNegated()).isEqualTo(false); - soft.assertThat(((CompositeRestriction) allNegated).restrictions() - .get(1).isNegated()).isEqualTo(false); soft.assertThat(notAll.isNegated()).isEqualTo(true); - soft.assertThat(((CompositeRestriction) notAll).restrictions() - .get(0).isNegated()).isEqualTo(false); - soft.assertThat(((CompositeRestriction) notAll).restrictions() - .get(1).isNegated()).isEqualTo(false); }); } @@ -106,28 +96,18 @@ void shouldNegateCompositeRestriction() { void shouldNegateNegatedCompositeRestriction() { Restriction ageBetween20and30 = Restrict.between(20, 30, "age"); Restriction nameContainsDuke = Restrict.contains("Duke", "name"); - Restriction any = Restrict.any(ageBetween20and30, nameContainsDuke); - Restriction anyNegated = any.negate(); - Restriction anyNotNegated = Restrict.not(anyNegated); + CompositeRestriction any = + (CompositeRestriction) Restrict.any(ageBetween20and30, nameContainsDuke); + CompositeRestriction anyNegated = any.negate(); + CompositeRestriction anyNotNegated = + (CompositeRestriction) Restrict.not(anyNegated); SoftAssertions.assertSoftly(soft -> { soft.assertThat(any.isNegated()).isEqualTo(false); - soft.assertThat(((CompositeRestriction) any).restrictions() - .get(0).isNegated()).isEqualTo(false); - soft.assertThat(((CompositeRestriction) any).restrictions() - .get(1).isNegated()).isEqualTo(false); soft.assertThat(anyNegated.isNegated()).isEqualTo(true); - soft.assertThat(((CompositeRestriction) anyNegated).restrictions() - .get(0).isNegated()).isEqualTo(false); - soft.assertThat(((CompositeRestriction) anyNegated).restrictions() - .get(1).isNegated()).isEqualTo(false); soft.assertThat(anyNotNegated.isNegated()).isEqualTo(false); - soft.assertThat(((CompositeRestriction) anyNotNegated).restrictions() - .get(0).isNegated()).isEqualTo(false); - soft.assertThat(((CompositeRestriction) anyNotNegated).restrictions() - .get(1).isNegated()).isEqualTo(false); }); } diff --git a/api/src/test/java/jakarta/data/RestrictTest.java b/api/src/test/java/jakarta/data/RestrictTest.java index d2914e08..04113be8 100644 --- a/api/src/test/java/jakarta/data/RestrictTest.java +++ b/api/src/test/java/jakarta/data/RestrictTest.java @@ -37,7 +37,6 @@ void shouldCreateEqualToRestriction() { soft.assertThat(basic.field()).isEqualTo("field"); soft.assertThat(basic.comparison()).isEqualTo(Operator.EQUAL); soft.assertThat(basic.value()).isEqualTo("value"); - soft.assertThat(basic.isNegated()).isFalse(); }); } @@ -50,9 +49,8 @@ void shouldCreateNotEqualToRestriction() { TextRestrictionRecord basic = (TextRestrictionRecord) restriction; SoftAssertions.assertSoftly(soft -> { soft.assertThat(basic.field()).isEqualTo("field"); - soft.assertThat(basic.comparison()).isEqualTo(Operator.EQUAL); + soft.assertThat(basic.comparison()).isEqualTo(Operator.NOT_EQUAL); soft.assertThat(basic.value()).isEqualTo("value"); - soft.assertThat(basic.isNegated()).isTrue(); }); } @@ -81,7 +79,6 @@ void shouldCreateContainsRestriction() { soft.assertThat(restriction.field()).isEqualTo("field"); soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); soft.assertThat(restriction.value()).isEqualTo("%substring%"); - soft.assertThat(restriction.isNegated()).isFalse(); }); } @@ -91,9 +88,8 @@ void shouldCreateNegatedContainsRestriction() { SoftAssertions.assertSoftly(soft -> { soft.assertThat(restriction.field()).isEqualTo("field"); - soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); + soft.assertThat(restriction.comparison()).isEqualTo(Operator.NOT_LIKE); soft.assertThat(restriction.value()).isEqualTo("%substring%"); - soft.assertThat(restriction.isNegated()).isTrue(); }); } @@ -105,7 +101,6 @@ void shouldCreateStartsWithRestriction() { soft.assertThat(restriction.field()).isEqualTo("field"); soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); soft.assertThat(restriction.value()).isEqualTo("prefix%"); - soft.assertThat(restriction.isNegated()).isFalse(); }); } @@ -115,9 +110,8 @@ void shouldCreateNegatedStartsWithRestriction() { SoftAssertions.assertSoftly(soft -> { soft.assertThat(restriction.field()).isEqualTo("field"); - soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); + soft.assertThat(restriction.comparison()).isEqualTo(Operator.NOT_LIKE); soft.assertThat(restriction.value()).isEqualTo("prefix%"); - soft.assertThat(restriction.isNegated()).isTrue(); }); } @@ -129,7 +123,6 @@ void shouldCreateEndsWithRestriction() { soft.assertThat(restriction.field()).isEqualTo("field"); soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); soft.assertThat(restriction.value()).isEqualTo("%suffix"); - soft.assertThat(restriction.isNegated()).isFalse(); }); } @@ -139,9 +132,8 @@ void shouldCreateNegatedEndsWithRestriction() { SoftAssertions.assertSoftly(soft -> { soft.assertThat(restriction.field()).isEqualTo("field"); - soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); + soft.assertThat(restriction.comparison()).isEqualTo(Operator.NOT_LIKE); soft.assertThat(restriction.value()).isEqualTo("%suffix"); - soft.assertThat(restriction.isNegated()).isTrue(); }); } diff --git a/api/src/test/java/jakarta/data/TextRestrictionRecordTest.java b/api/src/test/java/jakarta/data/TextRestrictionRecordTest.java index 3dcc5369..14b7f2b2 100644 --- a/api/src/test/java/jakarta/data/TextRestrictionRecordTest.java +++ b/api/src/test/java/jakarta/data/TextRestrictionRecordTest.java @@ -37,7 +37,6 @@ void shouldCreateTextRestrictionWithDefaultValues() { SoftAssertions.assertSoftly(soft -> { soft.assertThat(restriction.field()).isEqualTo("title"); - soft.assertThat(restriction.isNegated()).isFalse(); soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); soft.assertThat(restriction.value()).isEqualTo("%Java%"); soft.assertThat(restriction.isCaseSensitive()).isTrue(); @@ -49,15 +48,13 @@ void shouldCreateTextRestrictionWithDefaultValues() { void shouldCreateTextRestrictionWithExplicitNegation() { TextRestrictionRecord restriction = new TextRestrictionRecord<>( "title", - true, - Operator.LIKE, + Operator.NOT_LIKE, "%Java%" ); SoftAssertions.assertSoftly(soft -> { soft.assertThat(restriction.field()).isEqualTo("title"); - soft.assertThat(restriction.isNegated()).isTrue(); - soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); + soft.assertThat(restriction.comparison()).isEqualTo(Operator.NOT_LIKE); soft.assertThat(restriction.value()).isEqualTo("%Java%"); soft.assertThat(restriction.isCaseSensitive()).isTrue(); soft.assertThat(restriction.isEscaped()).isFalse(); @@ -78,7 +75,6 @@ void shouldIgnoreCaseForTextRestriction() { soft.assertThat(caseInsensitiveRestriction).isInstanceOf(TextRestrictionRecord.class); TextRestrictionRecord textRestriction = (TextRestrictionRecord) caseInsensitiveRestriction; soft.assertThat(textRestriction.field()).isEqualTo("title"); - soft.assertThat(textRestriction.isNegated()).isFalse(); soft.assertThat(textRestriction.comparison()).isEqualTo(Operator.LIKE); soft.assertThat(textRestriction.value()).isEqualTo("%Java%"); soft.assertThat(textRestriction.isCaseSensitive()).isFalse(); @@ -97,7 +93,6 @@ void shouldCreateTextRestrictionWithEscapedValue() { SoftAssertions.assertSoftly(soft -> { soft.assertThat(restriction.field()).isEqualTo("title"); - soft.assertThat(restriction.isNegated()).isFalse(); soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); soft.assertThat(restriction.value()).isEqualTo("%Java%"); soft.assertThat(restriction.isCaseSensitive()).isTrue(); @@ -113,20 +108,16 @@ void shouldNegateLikeRestriction() { TextRestriction notLikeJakartaEEAnyCase = likeJakartaEE.negate().ignoreCase(); SoftAssertions.assertSoftly(soft -> { - soft.assertThat(likeJakartaEE.isNegated()).isFalse(); soft.assertThat(likeJakartaEE.comparison()).isEqualTo(Operator.LIKE); soft.assertThat(likeJakartaEE.isCaseSensitive()).isTrue(); - soft.assertThat(notLikeJakartaEE.isNegated()).isTrue(); - soft.assertThat(notLikeJakartaEE.comparison()).isEqualTo(Operator.LIKE); + soft.assertThat(notLikeJakartaEE.comparison()).isEqualTo(Operator.NOT_LIKE); soft.assertThat(notLikeJakartaEE.isCaseSensitive()).isTrue(); - soft.assertThat(anyCaseNotLikeJakartaEE.isNegated()).isTrue(); - soft.assertThat(anyCaseNotLikeJakartaEE.comparison()).isEqualTo(Operator.LIKE); + soft.assertThat(anyCaseNotLikeJakartaEE.comparison()).isEqualTo(Operator.NOT_LIKE); soft.assertThat(anyCaseNotLikeJakartaEE.isCaseSensitive()).isFalse(); - soft.assertThat(notLikeJakartaEEAnyCase.isNegated()).isTrue(); - soft.assertThat(notLikeJakartaEEAnyCase.comparison()).isEqualTo(Operator.LIKE); + soft.assertThat(notLikeJakartaEEAnyCase.comparison()).isEqualTo(Operator.NOT_LIKE); soft.assertThat(notLikeJakartaEEAnyCase.isCaseSensitive()).isFalse(); }); } @@ -138,15 +129,12 @@ void shouldNegateNegatedRestriction() { TextRestriction notNotEndsWithJakartaEE = notEndsWithJakartaEE.negate(); SoftAssertions.assertSoftly(soft -> { - soft.assertThat(endsWithJakartaEE.isNegated()).isFalse(); soft.assertThat(endsWithJakartaEE.comparison()).isEqualTo(Operator.LIKE); soft.assertThat(endsWithJakartaEE.value()).isEqualTo("%Jakarta EE"); - soft.assertThat(notEndsWithJakartaEE.isNegated()).isTrue(); - soft.assertThat(notEndsWithJakartaEE.comparison()).isEqualTo(Operator.LIKE); + soft.assertThat(notEndsWithJakartaEE.comparison()).isEqualTo(Operator.NOT_LIKE); soft.assertThat(notEndsWithJakartaEE.value()).isEqualTo("%Jakarta EE"); - soft.assertThat(notNotEndsWithJakartaEE.isNegated()).isFalse(); soft.assertThat(notNotEndsWithJakartaEE.comparison()).isEqualTo(Operator.LIKE); soft.assertThat(notNotEndsWithJakartaEE.value()).isEqualTo("%Jakarta EE"); }); @@ -156,15 +144,13 @@ void shouldNegateNegatedRestriction() { void shouldSupportNegationForTextRestriction() { TextRestrictionRecord restriction = new TextRestrictionRecord<>( "author", - true, - Operator.EQUAL, + Operator.NOT_EQUAL, "John Doe" ); SoftAssertions.assertSoftly(soft -> { soft.assertThat(restriction.field()).isEqualTo("author"); - soft.assertThat(restriction.isNegated()).isTrue(); - soft.assertThat(restriction.comparison()).isEqualTo(Operator.EQUAL); + soft.assertThat(restriction.comparison()).isEqualTo(Operator.NOT_EQUAL); soft.assertThat(restriction.value()).isEqualTo("John Doe"); soft.assertThat(restriction.isCaseSensitive()).isTrue(); soft.assertThat(restriction.isEscaped()).isFalse(); diff --git a/api/src/test/java/jakarta/data/metamodel/AttributeTest.java b/api/src/test/java/jakarta/data/metamodel/AttributeTest.java index f087674c..e1a962b9 100644 --- a/api/src/test/java/jakarta/data/metamodel/AttributeTest.java +++ b/api/src/test/java/jakarta/data/metamodel/AttributeTest.java @@ -40,7 +40,6 @@ void shouldCreateEqualToRestriction() { soft.assertThat(basic.field()).isEqualTo("testAttribute"); soft.assertThat(basic.value()).isEqualTo("testValue"); soft.assertThat(basic.comparison()).isEqualTo(Operator.EQUAL); - soft.assertThat(basic.isNegated()).isFalse(); }); } @@ -53,8 +52,7 @@ void shouldCreateNotEqualToRestriction() { BasicRestriction basic = (BasicRestriction) restriction; soft.assertThat(basic.field()).isEqualTo("testAttribute"); soft.assertThat(basic.value()).isEqualTo("testValue"); - soft.assertThat(basic.comparison()).isEqualTo(Operator.EQUAL); - soft.assertThat(basic.isNegated()).isTrue(); + soft.assertThat(basic.comparison()).isEqualTo(Operator.NOT_EQUAL); }); } @@ -68,7 +66,6 @@ void shouldCreateInRestriction() { soft.assertThat(basic.field()).isEqualTo("testAttribute"); soft.assertThat(basic.value()).isEqualTo(Set.of("value1", "value2")); soft.assertThat(basic.comparison()).isEqualTo(Operator.IN); - soft.assertThat(basic.isNegated()).isFalse(); }); } @@ -88,8 +85,7 @@ void shouldCreateNotInRestriction() { BasicRestriction basic = (BasicRestriction) restriction; soft.assertThat(basic.field()).isEqualTo("testAttribute"); soft.assertThat(basic.value()).isEqualTo(Set.of("value1", "value2")); - soft.assertThat(basic.comparison()).isEqualTo(Operator.IN); - soft.assertThat(basic.isNegated()).isTrue(); + soft.assertThat(basic.comparison()).isEqualTo(Operator.NOT_IN); }); } @@ -110,7 +106,6 @@ void shouldCreateIsNullRestriction() { soft.assertThat(basic.field()).isEqualTo("testAttribute"); soft.assertThat(basic.value()).isNull(); soft.assertThat(basic.comparison()).isEqualTo(Operator.EQUAL); - soft.assertThat(basic.isNegated()).isFalse(); }); } @@ -123,8 +118,7 @@ void shouldCreateNotNullRestriction() { BasicRestriction basic = (BasicRestriction) restriction; soft.assertThat(basic.field()).isEqualTo("testAttribute"); soft.assertThat(basic.value()).isNull(); - soft.assertThat(basic.comparison()).isEqualTo(Operator.EQUAL); - soft.assertThat(basic.isNegated()).isTrue(); + soft.assertThat(basic.comparison()).isEqualTo(Operator.NOT_EQUAL); }); } } diff --git a/api/src/test/java/jakarta/data/metamodel/SortableAttributeTest.java b/api/src/test/java/jakarta/data/metamodel/SortableAttributeTest.java index 697284cd..fcc204ce 100644 --- a/api/src/test/java/jakarta/data/metamodel/SortableAttributeTest.java +++ b/api/src/test/java/jakarta/data/metamodel/SortableAttributeTest.java @@ -56,7 +56,6 @@ void shouldCreateGreaterThanRestriction() { soft.assertThat(basic.field()).isEqualTo("testAttribute"); soft.assertThat(basic.value()).isEqualTo(10); soft.assertThat(basic.comparison()).isEqualTo(Operator.GREATER_THAN); - soft.assertThat(basic.isNegated()).isFalse(); }); } @@ -70,7 +69,6 @@ void shouldCreateGreaterThanEqualRestriction() { soft.assertThat(basic.field()).isEqualTo("testAttribute"); soft.assertThat(basic.value()).isEqualTo(10); soft.assertThat(basic.comparison()).isEqualTo(Operator.GREATER_THAN_EQUAL); - soft.assertThat(basic.isNegated()).isFalse(); }); } @@ -84,7 +82,6 @@ void shouldCreateLessThanRestriction() { soft.assertThat(basic.field()).isEqualTo("testAttribute"); soft.assertThat(basic.value()).isEqualTo(10); soft.assertThat(basic.comparison()).isEqualTo(Operator.LESS_THAN); - soft.assertThat(basic.isNegated()).isFalse(); }); } @@ -98,7 +95,6 @@ void shouldCreateLessThanOrEqualRestriction() { soft.assertThat(basic.field()).isEqualTo("testAttribute"); soft.assertThat(basic.value()).isEqualTo(10); soft.assertThat(basic.comparison()).isEqualTo(Operator.LESS_THAN_EQUAL); - soft.assertThat(basic.isNegated()).isFalse(); }); } @@ -118,7 +114,6 @@ void shouldCreateBetweenRestriction() { soft.assertThat(lower.field()).isEqualTo("testAttribute"); soft.assertThat(lower.value()).isEqualTo(5); soft.assertThat(lower.comparison()).isEqualTo(Operator.GREATER_THAN_EQUAL); - soft.assertThat(lower.isNegated()).isFalse(); Restriction upperBound = composite.restrictions().get(1); soft.assertThat(upperBound).isInstanceOf(BasicRestriction.class); @@ -126,7 +121,6 @@ void shouldCreateBetweenRestriction() { soft.assertThat(upper.field()).isEqualTo("testAttribute"); soft.assertThat(upper.value()).isEqualTo(15); soft.assertThat(upper.comparison()).isEqualTo(Operator.LESS_THAN_EQUAL); - soft.assertThat(upper.isNegated()).isFalse(); }); } } diff --git a/api/src/test/java/jakarta/data/metamodel/TextAttributeTest.java b/api/src/test/java/jakarta/data/metamodel/TextAttributeTest.java index b14f15a7..453f509d 100644 --- a/api/src/test/java/jakarta/data/metamodel/TextAttributeTest.java +++ b/api/src/test/java/jakarta/data/metamodel/TextAttributeTest.java @@ -61,7 +61,6 @@ void shouldCreateContainsRestriction() { soft.assertThat(restriction.field()).isEqualTo("testAttribute"); soft.assertThat(restriction.value()).isEqualTo("%testValue%"); soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); - soft.assertThat(restriction.isNegated()).isFalse(); }); } @@ -73,7 +72,6 @@ void shouldCreateStartsWithRestriction() { soft.assertThat(restriction.field()).isEqualTo("testAttribute"); soft.assertThat(restriction.value()).isEqualTo("testValue%"); soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); - soft.assertThat(restriction.isNegated()).isFalse(); }); } @@ -85,7 +83,6 @@ void shouldCreateEndsWithRestriction() { soft.assertThat(restriction.field()).isEqualTo("testAttribute"); soft.assertThat(restriction.value()).isEqualTo("%testValue"); soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); - soft.assertThat(restriction.isNegated()).isFalse(); }); } @@ -97,7 +94,6 @@ void shouldCreateLikeRestriction() { soft.assertThat(restriction.field()).isEqualTo("testAttribute"); soft.assertThat(restriction.value()).isEqualTo("%test%"); soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); - soft.assertThat(restriction.isNegated()).isFalse(); }); } @@ -108,8 +104,7 @@ void shouldCreateNotContainsRestriction() { SoftAssertions.assertSoftly(soft -> { soft.assertThat(restriction.field()).isEqualTo("testAttribute"); soft.assertThat(restriction.value()).isEqualTo("%testValue%"); - soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); - soft.assertThat(restriction.isNegated()).isTrue(); + soft.assertThat(restriction.comparison()).isEqualTo(Operator.NOT_LIKE); }); } @@ -120,8 +115,7 @@ void shouldCreateNotLikeRestriction() { SoftAssertions.assertSoftly(soft -> { soft.assertThat(restriction.field()).isEqualTo("testAttribute"); soft.assertThat(restriction.value()).isEqualTo("%test%"); - soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); - soft.assertThat(restriction.isNegated()).isTrue(); + soft.assertThat(restriction.comparison()).isEqualTo(Operator.NOT_LIKE); }); } @@ -132,8 +126,7 @@ void shouldCreateNotStartsWithRestriction() { SoftAssertions.assertSoftly(soft -> { soft.assertThat(restriction.field()).isEqualTo("testAttribute"); soft.assertThat(restriction.value()).isEqualTo("testValue%"); - soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); - soft.assertThat(restriction.isNegated()).isTrue(); + soft.assertThat(restriction.comparison()).isEqualTo(Operator.NOT_LIKE); }); } @@ -144,8 +137,7 @@ void shouldCreateNotEndsWithRestriction() { SoftAssertions.assertSoftly(soft -> { soft.assertThat(restriction.field()).isEqualTo("testAttribute"); soft.assertThat(restriction.value()).isEqualTo("%testValue"); - soft.assertThat(restriction.comparison()).isEqualTo(Operator.LIKE); - soft.assertThat(restriction.isNegated()).isTrue(); + soft.assertThat(restriction.comparison()).isEqualTo(Operator.NOT_LIKE); }); } } From 6e161f725fe5790959129df3246d7144396a1726 Mon Sep 17 00:00:00 2001 From: Nathan Rauh Date: Tue, 10 Dec 2024 08:19:06 -0600 Subject: [PATCH 5/6] Update api/src/main/java/jakarta/data/Operator.java Co-authored-by: Gavin King --- api/src/main/java/jakarta/data/Operator.java | 34 +++++++------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/api/src/main/java/jakarta/data/Operator.java b/api/src/main/java/jakarta/data/Operator.java index 36b46aa5..56019721 100644 --- a/api/src/main/java/jakarta/data/Operator.java +++ b/api/src/main/java/jakarta/data/Operator.java @@ -35,29 +35,17 @@ public enum Operator { * @return the operator that is the negation of this operator. */ Operator negate() { - switch (this) { - case EQUAL: - return NOT_EQUAL; - case GREATER_THAN: - return LESS_THAN_EQUAL; - case GREATER_THAN_EQUAL: - return LESS_THAN; - case IN: - return NOT_IN; - case LESS_THAN: - return GREATER_THAN_EQUAL; - case LESS_THAN_EQUAL: - return GREATER_THAN; - case LIKE: - return NOT_LIKE; - case NOT_EQUAL: - return EQUAL; - case NOT_IN: - return IN; - case NOT_LIKE: - return LIKE; - default: // should be unreachable - throw new IllegalStateException(name()); + return switch (this) { + case EQUAL -> NOT_EQUAL; + case GREATER_THAN -> LESS_THAN_EQUAL; + case GREATER_THAN_EQUAL -> LESS_THAN; + case IN -> NOT_IN; + case LESS_THAN -> GREATER_THAN_EQUAL; + case LESS_THAN_EQUAL -> GREATER_THAN; + case LIKE -> NOT_LIKE; + case NOT_EQUAL -> EQUAL; + case NOT_IN -> IN; + case NOT_LIKE -> LIKE; } } } From a8d02bbb97e794eb6dd6ff835df33ab74792fa6e Mon Sep 17 00:00:00 2001 From: Nathan Rauh Date: Tue, 10 Dec 2024 08:22:05 -0600 Subject: [PATCH 6/6] suggested code needs ; to compile --- api/src/main/java/jakarta/data/Operator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/jakarta/data/Operator.java b/api/src/main/java/jakarta/data/Operator.java index 56019721..003357ef 100644 --- a/api/src/main/java/jakarta/data/Operator.java +++ b/api/src/main/java/jakarta/data/Operator.java @@ -46,6 +46,6 @@ Operator negate() { case NOT_EQUAL -> EQUAL; case NOT_IN -> IN; case NOT_LIKE -> LIKE; - } + }; } }