Skip to content

Commit

Permalink
Merge pull request #915 from njr-11/906-restrictions-to-use-attribute…
Browse files Browse the repository at this point in the history
…-terminology

update Restrictions to standardize on attribute terminology
  • Loading branch information
otaviojava authored Jan 7, 2025
2 parents 4e9e756 + 96544bd commit 6c31186
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 115 deletions.
4 changes: 2 additions & 2 deletions api/src/main/java/jakarta/data/BasicRestriction.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
package jakarta.data;

public interface BasicRestriction<T> extends Restriction<T> {
Operator comparison();
String attribute();

String field();
Operator comparison();

@Override
BasicRestriction<T> negate();
Expand Down
6 changes: 3 additions & 3 deletions api/src/main/java/jakarta/data/BasicRestrictionRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
import java.util.Objects;

record BasicRestrictionRecord<T>(
String field,
String attribute,
Operator comparison,
Object value) implements BasicRestriction<T> {

BasicRestrictionRecord {
Objects.requireNonNull(field, "Field must not be null");
Objects.requireNonNull(attribute, "Attribute must not be null");
}

@Override
public BasicRestriction<T> negate() {
return new BasicRestrictionRecord<>(
field,
attribute,
comparison.negate(),
value);
}
Expand Down
106 changes: 53 additions & 53 deletions api/src/main/java/jakarta/data/Restrict.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,83 +53,83 @@ public static <T> Restriction<T> any(Restriction<T>... restrictions) {

public static <T, V extends Comparable<V>> Restriction<T> between(V min,
V max,
String field) {
return all(greaterThanEqual(min, field),
lessThanEqual(max, field));
String attribute) {
return all(greaterThanEqual(min, attribute),
lessThanEqual(max, attribute));
}

// TODO Need to think more about how to best cover negation of multiple
// and then make negation of Single consistent with it

public static <T> TextRestriction<T> contains(String substring, String field) {
public static <T> TextRestriction<T> contains(String substring, String attribute) {
String pattern = toLikeEscaped(CHAR_WILDCARD, STRING_WILDCARD, true, substring, true);
return new TextRestrictionRecord<>(field, Operator.LIKE, ESCAPED, pattern);
return new TextRestrictionRecord<>(attribute, Operator.LIKE, ESCAPED, pattern);
}

public static <T> TextRestriction<T> endsWith(String suffix, String field) {
public static <T> TextRestriction<T> endsWith(String suffix, String attribute) {
String pattern = toLikeEscaped(CHAR_WILDCARD, STRING_WILDCARD, true, suffix, false);
return new TextRestrictionRecord<>(field, Operator.LIKE, ESCAPED, pattern);
return new TextRestrictionRecord<>(attribute, Operator.LIKE, ESCAPED, pattern);
}

public static <T> Restriction<T> equalTo(Object value, String field) {
return new BasicRestrictionRecord<>(field, Operator.EQUAL, value);
public static <T> Restriction<T> equalTo(Object value, String attribute) {
return new BasicRestrictionRecord<>(attribute, Operator.EQUAL, value);
}

public static <T> TextRestriction<T> equalTo(String value, String field) {
return new TextRestrictionRecord<>(field, Operator.EQUAL, value);
public static <T> TextRestriction<T> equalTo(String value, String attribute) {
return new TextRestrictionRecord<>(attribute, Operator.EQUAL, value);
}

public static <T, V extends Comparable<V>> Restriction<T> greaterThan(V value, String field) {
return new BasicRestrictionRecord<>(field, Operator.GREATER_THAN, value);
public static <T, V extends Comparable<V>> Restriction<T> greaterThan(V value, String attribute) {
return new BasicRestrictionRecord<>(attribute, Operator.GREATER_THAN, value);
}

public static <T> TextRestriction<T> greaterThan(String value, String field) {
return new TextRestrictionRecord<>(field, Operator.GREATER_THAN, value);
public static <T> TextRestriction<T> greaterThan(String value, String attribute) {
return new TextRestrictionRecord<>(attribute, Operator.GREATER_THAN, value);
}

public static <T, V extends Comparable<V>> Restriction<T> greaterThanEqual(V value, String field) {
return new BasicRestrictionRecord<>(field, Operator.GREATER_THAN_EQUAL, value);
public static <T, V extends Comparable<V>> Restriction<T> greaterThanEqual(V value, String attribute) {
return new BasicRestrictionRecord<>(attribute, Operator.GREATER_THAN_EQUAL, value);
}

public static <T> TextRestriction<T> greaterThanEqual(String value, String field) {
return new TextRestrictionRecord<>(field, Operator.GREATER_THAN_EQUAL, value);
public static <T> TextRestriction<T> greaterThanEqual(String value, String attribute) {
return new TextRestrictionRecord<>(attribute, Operator.GREATER_THAN_EQUAL, value);
}

public static <T> Restriction<T> in(Set<Object> values, String field) {
return new BasicRestrictionRecord<>(field, Operator.IN, values);
public static <T> Restriction<T> in(Set<Object> values, String attribute) {
return new BasicRestrictionRecord<>(attribute, Operator.IN, values);
}

public static <T, V extends Comparable<V>> Restriction<T> lessThan(V value, String field) {
return new BasicRestrictionRecord<>(field, Operator.LESS_THAN, value);
public static <T, V extends Comparable<V>> Restriction<T> lessThan(V value, String attribute) {
return new BasicRestrictionRecord<>(attribute, Operator.LESS_THAN, value);
}

public static <T> TextRestriction<T> lessThan(String value, String field) {
return new TextRestrictionRecord<>(field, Operator.LESS_THAN, value);
public static <T> TextRestriction<T> lessThan(String value, String attribute) {
return new TextRestrictionRecord<>(attribute, Operator.LESS_THAN, value);
}

public static <T, V extends Comparable<V>> Restriction<T> lessThanEqual(V value, String field) {
return new BasicRestrictionRecord<>(field, Operator.LESS_THAN_EQUAL, value);
public static <T, V extends Comparable<V>> Restriction<T> lessThanEqual(V value, String attribute) {
return new BasicRestrictionRecord<>(attribute, Operator.LESS_THAN_EQUAL, value);
}

public static <T> TextRestriction<T> lessThanEqual(String value, String field) {
return new TextRestrictionRecord<>(field, Operator.LESS_THAN_EQUAL, value);
public static <T> TextRestriction<T> lessThanEqual(String value, String attribute) {
return new TextRestrictionRecord<>(attribute, Operator.LESS_THAN_EQUAL, value);
}

// TODO this would be possible if Pattern is added, but is it even useful?
//public static <T> TextRestriction<T> like(Pattern pattern, String field) {
// return new TextRestriction<>(field, Operator.LIKE, ESCAPED, pattern);
//public static <T> TextRestriction<T> like(Pattern pattern, String attribute) {
// return new TextRestriction<>(attribute, Operator.LIKE, ESCAPED, pattern);
//}

public static <T> TextRestriction<T> like(String pattern, String field) {
return new TextRestrictionRecord<>(field, Operator.LIKE, pattern);
public static <T> TextRestriction<T> like(String pattern, String attribute) {
return new TextRestrictionRecord<>(attribute, Operator.LIKE, pattern);
}

public static <T> TextRestriction<T> like(String pattern,
char charWildcard,
char stringWildcard,
String field) {
String attribute) {
String p = toLikeEscaped(charWildcard, stringWildcard, false, pattern, false);
return new TextRestrictionRecord<>(field, Operator.LIKE, ESCAPED, p);
return new TextRestrictionRecord<>(attribute, Operator.LIKE, ESCAPED, p);
}

// convenience method for those who would prefer to avoid .negate()
Expand All @@ -138,48 +138,48 @@ public static <T> Restriction<T> not(Restriction<T> restriction) {
return restriction.negate();
}

public static <T> Restriction<T> notEqualTo(Object value, String field) {
return new BasicRestrictionRecord<>(field, Operator.NOT_EQUAL, value);
public static <T> Restriction<T> notEqualTo(Object value, String attribute) {
return new BasicRestrictionRecord<>(attribute, Operator.NOT_EQUAL, value);
}

public static <T> TextRestriction<T> notEqualTo(String value, String field) {
return new TextRestrictionRecord<>(field, Operator.NOT_EQUAL, value);
public static <T> TextRestriction<T> notEqualTo(String value, String attribute) {
return new TextRestrictionRecord<>(attribute, Operator.NOT_EQUAL, value);
}

public static <T> TextRestriction<T> notContains(String substring, String field) {
public static <T> TextRestriction<T> notContains(String substring, String attribute) {
String pattern = toLikeEscaped(CHAR_WILDCARD, STRING_WILDCARD, true, substring, true);
return new TextRestrictionRecord<>(field, Operator.NOT_LIKE, ESCAPED, pattern);
return new TextRestrictionRecord<>(attribute, Operator.NOT_LIKE, ESCAPED, pattern);
}

public static <T> TextRestriction<T> notEndsWith(String suffix, String field) {
public static <T> TextRestriction<T> notEndsWith(String suffix, String attribute) {
String pattern = toLikeEscaped(CHAR_WILDCARD, STRING_WILDCARD, true, suffix, false);
return new TextRestrictionRecord<>(field, Operator.NOT_LIKE, ESCAPED, pattern);
return new TextRestrictionRecord<>(attribute, Operator.NOT_LIKE, ESCAPED, pattern);
}

public static <T> Restriction<T> notIn(Set<Object> values, String field) {
return new BasicRestrictionRecord<>(field, Operator.NOT_IN, values);
public static <T> Restriction<T> notIn(Set<Object> values, String attribute) {
return new BasicRestrictionRecord<>(attribute, Operator.NOT_IN, values);
}

public static <T> TextRestriction<T> notLike(String pattern, String field) {
return new TextRestrictionRecord<>(field, Operator.NOT_LIKE, pattern);
public static <T> TextRestriction<T> notLike(String pattern, String attribute) {
return new TextRestrictionRecord<>(attribute, Operator.NOT_LIKE, pattern);
}

public static <T> TextRestriction<T> notLike(String pattern,
char charWildcard,
char stringWildcard,
String field) {
String attribute) {
String p = toLikeEscaped(charWildcard, stringWildcard, false, pattern, false);
return new TextRestrictionRecord<>(field, Operator.NOT_LIKE, ESCAPED, p);
return new TextRestrictionRecord<>(attribute, Operator.NOT_LIKE, ESCAPED, p);
}

public static <T> TextRestriction<T> notStartsWith(String prefix, String field) {
public static <T> TextRestriction<T> notStartsWith(String prefix, String attribute) {
String pattern = toLikeEscaped(CHAR_WILDCARD, STRING_WILDCARD, false, prefix, true);
return new TextRestrictionRecord<>(field, Operator.NOT_LIKE, ESCAPED, pattern);
return new TextRestrictionRecord<>(attribute, Operator.NOT_LIKE, ESCAPED, pattern);
}

public static <T> TextRestriction<T> startsWith(String prefix, String field) {
public static <T> TextRestriction<T> startsWith(String prefix, String attribute) {
String pattern = toLikeEscaped(CHAR_WILDCARD, STRING_WILDCARD, false, prefix, true);
return new TextRestrictionRecord<>(field, Operator.LIKE, ESCAPED, pattern);
return new TextRestrictionRecord<>(attribute, Operator.LIKE, ESCAPED, pattern);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions api/src/main/java/jakarta/data/TextRestrictionRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
import java.util.Objects;

record TextRestrictionRecord<T>(
String field,
String attribute,
Operator comparison,
boolean isCaseSensitive,
boolean isEscaped,
String value) implements TextRestriction<T> {

TextRestrictionRecord {
Objects.requireNonNull(field, "Field must not be null");
Objects.requireNonNull(attribute, "Attribute must not be null");
}

TextRestrictionRecord(String field, Operator comparison, boolean escaped, String value) {
Expand All @@ -44,14 +44,14 @@ record TextRestrictionRecord<T>(

@Override
public TextRestriction<T> ignoreCase() {
return new TextRestrictionRecord<>(field, comparison, false, isEscaped, value);
return new TextRestrictionRecord<>(attribute, comparison, false, isEscaped, value);
}

@Override
public TextRestriction<T> negate() {

return new TextRestrictionRecord<>(
field,
attribute,
comparison.negate(),
isCaseSensitive,
isEscaped,
Expand Down
12 changes: 6 additions & 6 deletions api/src/test/java/jakarta/data/BasicRestrictionRecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void shouldCreateBasicRestrictionWithDefaultNegation() {
BasicRestrictionRecord<String> restriction = new BasicRestrictionRecord<>("title", Operator.EQUAL, "Java Guide");

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(restriction.field()).isEqualTo("title");
soft.assertThat(restriction.attribute()).isEqualTo("title");
soft.assertThat(restriction.comparison()).isEqualTo(Operator.EQUAL);
soft.assertThat(restriction.value()).isEqualTo("Java Guide");
});
Expand All @@ -46,7 +46,7 @@ void shouldCreateBasicRestrictionWithExplicitNegation() {
.negate();

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(restriction.field()).isEqualTo("title");
soft.assertThat(restriction.attribute()).isEqualTo("title");
soft.assertThat(restriction.comparison()).isEqualTo(Operator.NOT_EQUAL);
soft.assertThat(restriction.value()).isEqualTo("Java Guide");
});
Expand All @@ -58,7 +58,7 @@ void shouldCreateBasicRestrictionWithNullValue() {
BasicRestrictionRecord<String> restriction = new BasicRestrictionRecord<>("title", Operator.EQUAL, null);

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(restriction.field()).isEqualTo("title");
soft.assertThat(restriction.attribute()).isEqualTo("title");
soft.assertThat(restriction.comparison()).isEqualTo(Operator.EQUAL);
soft.assertThat(restriction.value()).isNull();
});
Expand Down Expand Up @@ -114,16 +114,16 @@ void shouldSupportNegatedRestrictionUsingDefaultConstructor() {
(BasicRestriction<Book>) Restrict.<Book>notEqualTo((Object) "Unknown", "author");

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(negatedRestriction.field()).isEqualTo("author");
soft.assertThat(negatedRestriction.attribute()).isEqualTo("author");
soft.assertThat(negatedRestriction.comparison()).isEqualTo(Operator.NOT_EQUAL);
soft.assertThat(negatedRestriction.value()).isEqualTo("Unknown");
});
}

@Test
void shouldThrowExceptionWhenFieldIsNull() {
void shouldThrowExceptionWhenAttributeIsNull() {
assertThatThrownBy(() -> new BasicRestrictionRecord<>(null, Operator.EQUAL, "testValue"))
.isInstanceOf(NullPointerException.class)
.hasMessage("Field must not be null");
.hasMessage("Attribute must not be null");
}
}
Loading

0 comments on commit 6c31186

Please sign in to comment.