Module jakarta.data

Interface Constraint<V>

Type Parameters:
V - type of the entity attribute.
All Known Subinterfaces:
AtLeast<V>, AtMost<V>, Between<V>, EqualTo<V>, GreaterThan<V>, In<V>, LessThan<V>, Like, NotBetween<V>, NotEqualTo<V>, NotIn<V>, NotLike, NotNull<V>, Null<V>

public interface Constraint<V>

Supertype of interfaces that define constraints on entity attributes.

Constraints are used on parameter-based Find and Delete methods. Constraint parameters must be positioned before special parameters (such as Restriction and Order) in the method signature.

Method parameter

Constraint parameters can be of type C<T> or C<? super T> where C is Constraint or any subtype of Constraint, such as Like or LessThan, and T is the entity attribute type. For example,

 @Find
 List<Car> withinYears(@By(_Car.YEAR) Between<Integer> year,
                       @By(_Car.MAKE) Like makePattern,
                       @By(_Car.MODEL) Like modelPattern,
                       Order<Car> sorts);

 ...

 found = cars.withinYears(Between.bounds(2021, 2025),
                          Like.prefix(makePrefix),
                          Like.contains(modelSubstring),
                          Order.by(_Car.year.desc(),
                                   _Car.price.desc(),
                                   _Car.vin.asc()));
 

Annotation value

Constraint parameters of repository methods can be annotated with the @Is annotation to indicate the subtype of Constraint. The type of the method parameter must be the entity attribute type. For example,

 @Find
 List<Car> pricedAtMost(@By(_Car.PRICE) @Is(AtMost.class) int maxPrice,
                        @By(_Car.MAKE) @Is(Like.class) String makePattern,
                        @By(_Car.MODEL) @Is(Like.class) Sting modelPattern,
                        Order<Car> sorts);

 ...

 found = cars.pricedAtMost(35000, "Chev%", "% SUV",
                           Order.by(_Car.price.desc(),
                                    _Car.vin.asc()));
 

The entity and static metamodel for the code examples within this class are shown in the Attribute Javadoc.

Since:
1.1
  • Method Summary

    Modifier and Type
    Method
    Description
    static <V extends Comparable<?>>
    Between<V>
    between(V minimum, V maximum)
    Requires that the constraint target evaluates to a value that is greater than or equal to the given minimum and less than or equal to the given maximum.
    static <V> EqualTo<V>
    equalTo(V value)
    Requires that the constraint target evaluates to a value that is equal to the given value.
    static <V extends Comparable<?>>
    GreaterThan<V>
    greaterThan(V bound)
    Requires that the constraint target evaluates to a value that is greater than the given bound.
    static <V extends Comparable<?>>
    AtLeast<V>
    greaterThanEqual(V minimum)
    Requires that the constraint target evaluates to a value that is greater than or equal to the given minimum.
    static <V> In<V>
    in(Set<V> values)
    Requires that the constraint target evaluates to a value that is equal to one of the given values.
    static <V> In<V>
    in(V... values)
    Requires that the constraint target evaluates to a value that is equal to one of the given values.
    static <V> Null<V>
    Requires that the constraint target evaluates to null.
    static <V extends Comparable<?>>
    LessThan<V>
    lessThan(V bound)
    Requires that the constraint target evaluates to a value that is less than the given bound.
    static <V extends Comparable<?>>
    AtMost<V>
    lessThanEqual(V maximum)
    Requires that the constraint target evaluates to a value that is less than or equal to the given maximum.
    static Like
    like(String pattern)
    Requires that the constraint target match the given pattern, in which _ and % represent wildcards.
    static Like
    like(String pattern, char charWildcard, char stringWildcard)
    Requires that the constraint target match the given pattern, in which the given characters represent wildcards.
    static Like
    like(String pattern, char charWildcard, char stringWildcard, char escape)
    Requires that the constraint target match the given pattern, in which the given characters represent wildcards and escape.
    Obtains the negation of the Constraint.
    static <V extends Comparable<?>>
    NotBetween<V>
    notBetween(V lowerBound, V upperBound)
    Requires that the constraint target evaluates to a value that is less than the given lowerBound or greater than the given upperBound.
    static <V> NotEqualTo<V>
    notEqualTo(V value)
    Requires that the constraint target evaluates to a value that is not equal to the given value.
    static <V> NotIn<V>
    notIn(Set<V> values)
    Requires that the constraint target evaluates to a value that is not equal to any of the given values.
    static <V> NotIn<V>
    notIn(V... values)
    Requires that the constraint target evaluates to a value that is not equal to any of the given values.
    static NotLike
    notLike(String pattern)
    Requires that the constraint target does not match the given pattern, in which _ and % represent wildcards.
    static NotLike
    notLike(String pattern, char charWildcard, char stringWildcard)
    Requires that the constraint target does not match the given pattern, in which the given characters represent wildcards.
    static NotLike
    notLike(String pattern, char charWildcard, char stringWildcard, char escape)
    Requires that the constraint target does not match the given pattern, in which the given characters represent wildcards and escape.
    static <V> NotNull<V>
    Requires that the constraint target does not evaluate to null.
  • Method Details

    • negate

      Constraint<V> negate()

      Obtains the negation of the Constraint. For example, the negation of Like is NotLike, the negation of Null is NotNull, and the negation of AtLeast is LessThan. A value satisfies the negation of a constraint if and only if it does not satisfy the constraint.

      Returns:
      the opposite Constraint subtype.
    • equalTo

      static <V> EqualTo<V> equalTo(V value)

      Requires that the constraint target evaluates to a value that is equal to the given value.

      Type Parameters:
      V - type of the entity attribute.
      Parameters:
      value - a value.
      Returns:
      an EqualTo constraint.
      Throws:
      NullPointerException - if the value is null.
      See Also:
    • notEqualTo

      static <V> NotEqualTo<V> notEqualTo(V value)

      Requires that the constraint target evaluates to a value that is not equal to the given value.

      Type Parameters:
      V - type of the entity attribute.
      Parameters:
      value - a value.
      Returns:
      a NotEqualTo constraint.
      Throws:
      NullPointerException - if the value is null.
      See Also:
    • in

      @SafeVarargs static <V> In<V> in(V... values)

      Requires that the constraint target evaluates to a value that is equal to one of the given values.

      Type Parameters:
      V - type of the entity attribute.
      Parameters:
      values - one or more values.
      Returns:
      an In constraint.
      Throws:
      IllegalArgumentException - if the array of values is empty.
      NullPointerException - if the array of values or any element of the array is null.
      See Also:
    • in

      static <V> In<V> in(Set<V> values)

      Requires that the constraint target evaluates to a value that is equal to one of the given values.

      Type Parameters:
      V - type of the entity attribute.
      Parameters:
      values - one or more values.
      Returns:
      an In constraint.
      Throws:
      IllegalArgumentException - if the collection of values is empty.
      NullPointerException - if the collection of values or any value within the collection is null.
      See Also:
    • notIn

      @SafeVarargs static <V> NotIn<V> notIn(V... values)

      Requires that the constraint target evaluates to a value that is not equal to any of the given values.

      Type Parameters:
      V - type of the entity attribute.
      Parameters:
      values - one or more values.
      Returns:
      a NotIn constraint.
      Throws:
      IllegalArgumentException - if the array of values is empty.
      NullPointerException - if the array of values or any element of the array is null.
      See Also:
    • notIn

      static <V> NotIn<V> notIn(Set<V> values)

      Requires that the constraint target evaluates to a value that is not equal to any of the given values.

      Type Parameters:
      V - type of the entity attribute.
      Parameters:
      values - one or more values.
      Returns:
      a NotIn constraint.
      Throws:
      IllegalArgumentException - if the collection of values is empty.
      NullPointerException - if the collection of values or any value within the collection is null.
      See Also:
    • like

      static Like like(String pattern)

      Requires that the constraint target match the given pattern, in which _ and % represent wildcards. The supplied pattern has no escape character.

      Parameters:
      pattern - a pattern in which _ matches a single character and % matches 0 or more characters.
      Returns:
      a Like constraint.
      Throws:
      NullPointerException - if the pattern is null.
      See Also:
    • like

      static Like like(String pattern, char charWildcard, char stringWildcard)

      Requires that the constraint target match the given pattern, in which the given characters represent wildcards. The supplied pattern has no escape character.

      Parameters:
      pattern - a pattern that can include the given wildcard characters.
      charWildcard - wildcard that represents any single character.
      stringWildcard - wildcard that represents 0 or more characters.
      Returns:
      a Like constraint.
      Throws:
      NullPointerException - if the pattern is null.
      See Also:
    • like

      static Like like(String pattern, char charWildcard, char stringWildcard, char escape)

      Requires that the constraint target match the given pattern, in which the given characters represent wildcards and escape.

      Parameters:
      pattern - a pattern that can include the given wildcard characters and escape character.
      charWildcard - wildcard that represents any single character.
      stringWildcard - wildcard that represents 0 or more characters.
      escape - escape character.
      Returns:
      a Like constraint.
      Throws:
      NullPointerException - if the pattern is null.
      See Also:
    • notLike

      static NotLike notLike(String pattern)

      Requires that the constraint target does not match the given pattern, in which _ and % represent wildcards. The supplied pattern has no escape character.

      Parameters:
      pattern - a pattern in which _ matches a single character and % matches 0 or more characters.
      Returns:
      a NotLike constraint.
      Throws:
      NullPointerException - if the pattern is null.
      See Also:
    • notLike

      static NotLike notLike(String pattern, char charWildcard, char stringWildcard)

      Requires that the constraint target does not match the given pattern, in which the given characters represent wildcards. The supplied pattern has no escape character.

      Parameters:
      pattern - a pattern that can include the given wildcard characters.
      charWildcard - wildcard that represents any single character.
      stringWildcard - wildcard that represents 0 or more characters.
      Returns:
      a NotLike constraint.
      Throws:
      NullPointerException - if the pattern is null.
      See Also:
    • notLike

      static NotLike notLike(String pattern, char charWildcard, char stringWildcard, char escape)

      Requires that the constraint target does not match the given pattern, in which the given characters represent wildcards and escape.

      Parameters:
      pattern - a pattern that can include the given wildcard characters and escape character.
      charWildcard - wildcard that represents any single character.
      stringWildcard - wildcard that represents 0 or more characters.
      escape - escape character.
      Returns:
      a NotLike constraint.
      Throws:
      NullPointerException - if the pattern is null.
      See Also:
    • isNull

      static <V> Null<V> isNull()

      Requires that the constraint target evaluates to null.

      Type Parameters:
      V - type of the entity attribute.
      Returns:
      a Null constraint.
      See Also:
    • notNull

      static <V> NotNull<V> notNull()

      Requires that the constraint target does not evaluate to null.

      Type Parameters:
      V - type of the entity attribute.
      Returns:
      a NotNull constraint.
      See Also:
    • greaterThan

      static <V extends Comparable<?>> GreaterThan<V> greaterThan(V bound)

      Requires that the constraint target evaluates to a value that is greater than the given bound.

      Type Parameters:
      V - type of the entity attribute.
      Parameters:
      bound - an exclusive minimum value.
      Returns:
      a GreaterThan constraint.
      Throws:
      NullPointerException - if the bound is null.
      See Also:
    • lessThan

      static <V extends Comparable<?>> LessThan<V> lessThan(V bound)

      Requires that the constraint target evaluates to a value that is less than the given bound.

      Type Parameters:
      V - type of the entity attribute.
      Parameters:
      bound - an exclusive maximum value.
      Returns:
      a LessThan constraint.
      Throws:
      NullPointerException - if the bound is null.
      See Also:
    • greaterThanEqual

      static <V extends Comparable<?>> AtLeast<V> greaterThanEqual(V minimum)

      Requires that the constraint target evaluates to a value that is greater than or equal to the given minimum.

      Type Parameters:
      V - type of the entity attribute.
      Parameters:
      minimum - the minimum value.
      Returns:
      an AtLeast constraint.
      Throws:
      NullPointerException - if the minimum is null.
      See Also:
    • lessThanEqual

      static <V extends Comparable<?>> AtMost<V> lessThanEqual(V maximum)

      Requires that the constraint target evaluates to a value that is less than or equal to the given maximum.

      Type Parameters:
      V - type of the entity attribute.
      Parameters:
      maximum - the maximum value.
      Returns:
      an AtMost constraint.
      Throws:
      NullPointerException - if the maximum is null.
      See Also:
    • between

      static <V extends Comparable<?>> Between<V> between(V minimum, V maximum)

      Requires that the constraint target evaluates to a value that is greater than or equal to the given minimum and less than or equal to the given maximum.

      Type Parameters:
      V - type of the entity attribute.
      Parameters:
      minimum - the minimum value.
      maximum - the maximum value.
      Returns:
      a Between constraint.
      Throws:
      NullPointerException - if the minimum or maximum is null.
      See Also:
    • notBetween

      static <V extends Comparable<?>> NotBetween<V> notBetween(V lowerBound, V upperBound)

      Requires that the constraint target evaluates to a value that is less than the given lowerBound or greater than the given upperBound.

      Type Parameters:
      V - type of the entity attribute.
      Parameters:
      lowerBound - a lower bound.
      upperBound - an upper bound.
      Returns:
      a NotBetween constraint.
      Throws:
      NullPointerException - if the lower bound or upper bound is null.
      See Also: