Module jakarta.data

Class Restrict

java.lang.Object
jakarta.data.restrict.Restrict

public class Restrict extends Object

Creates composite restrictions.

Composite restrictions are created by combining multiple restrictions, which might themselves be composite restrictions or can be obtained from static metamodel Attribute subtypes.

For example, the following constructs a composite restriction (from a basic restriction and another composite restriction) that matches cars that either cost less than $35000 or have newer model years than 2023 and cost less than $40000.

 List<Car> found =
         cars.search(make,
                     model,
                     Restrict.any(_Car.price.lessThan(35000),
                                  Restrict.all(_Car.year.greaterThan(2023),
                                               _Car.price.lessThan(40000))),
                     Order.by(_Car.year.desc(),
                              _Car.price.desc()));
 
Since:
1.1
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Restriction<T>
    all(Restriction<? super T>... restrictions)
    Returns a composite restriction that is satisfied when all of the supplied restrictions are satisfied.
    static <T> Restriction<T>
    all(List<? extends Restriction<? super T>> restrictions)
    Returns a composite restriction that is satisfied when all of the supplied restrictions are satisfied.
    static <T> Restriction<T>
    any(Restriction<? super T>... restrictions)
    Returns a composite restriction that is satisfied when at least one of the supplied restrictions is satisfied.
    static <T> Restriction<T>
    any(List<? extends Restriction<? super T>> restrictions)
    Returns a composite restriction that is satisfied when at least one of the supplied restrictions is satisfied.
    static <T> Restriction<T>
    not(Restriction<T> restriction)
    Returns the negation of the specified restriction.
    static <T> Restriction<T>
    Returns a restriction that always evaluates to satisfied.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • all

      @SafeVarargs public static <T> Restriction<T> all(Restriction<? super T>... restrictions)

      Returns a composite restriction that is satisfied when all of the supplied restrictions are satisfied. The order of the restrictions is preserved in the composite restriction.

      For example,

       List<Book> jakartaEEBooksByAuthor =
               books.writtenBy(author,
                               Restrict.all(_Book.title.notNull(),
                                            _Book.title.upper().contains("JAKARTA EE")),
                               Order.by(_Book.title.asc()));
       
      Type Parameters:
      T - entity type.
      Parameters:
      restrictions - one or more restrictions obtained from a method of Restrict or from a static metamodel Attribute subtype.
      Returns:
      Throws:
      IllegalArgumentException - if the supplied restrictions array is empty or null.
      NullPointerException - if the supplied restrictions array includes a null value.
    • all

      public static <T> Restriction<T> all(List<? extends Restriction<? super T>> restrictions)

      Returns a composite restriction that is satisfied when all of the supplied restrictions are satisfied. The order of the restrictions is preserved in the composite restriction, which keeps a copy of the supplied List rather than the original if the list is modifiable.

      For example,

       List<Restriction<Book>> restrictions = new ArrayList<>();
       restrictions.add(_Book.price.lessThanEqual(10.0f));
       restrictions.add(_Book.numPages.greaterThanEqual(200));
      
       List<Sort<Book>> sorts = new ArrayList<>();
       sorts.add(_Book.price.desc());
       sorts.add(_Book.title.asc());
      
       List<Book> inexpensiveLongBooks =
               books.search(Restrict.all(restrictions),
                            Order.by(sorts));
       
      Type Parameters:
      T - entity type.
      Parameters:
      restrictions - one or more restrictions obtained from a method of Restrict or from a static metamodel Attribute subtype.
      Returns:
      Throws:
      IllegalArgumentException - if the supplied restrictions list is empty or null.
      NullPointerException - if the supplied restrictions list includes a null value.
    • any

      @SafeVarargs public static <T> Restriction<T> any(Restriction<? super T>... restrictions)

      Returns a composite restriction that is satisfied when at least one of the supplied restrictions is satisfied. The order of the restrictions is preserved in the composite restriction.

      For example,

       List<Product> found =
               products.search(productName,
                               Restrict.any(_Product.color.equalTo(Color.BLUE),
                                            _Product.color.equalTo(Color.GREEN)));
       
      Type Parameters:
      T - entity type.
      Parameters:
      restrictions - one or more restrictions obtained from a method of Restrict or from a static metamodel Attribute subtype.
      Returns:
      Throws:
      IllegalArgumentException - if the supplied restrictions array is empty or null.
      NullPointerException - if the supplied restrictions array includes a null value.
    • any

      public static <T> Restriction<T> any(List<? extends Restriction<? super T>> restrictions)

      Returns a composite restriction that is satisfied when at least one of the supplied restrictions is satisfied. The order of the restrictions is preserved in the composite restriction The order of the restrictions is preserved in the composite restriction, which keeps a copy of the supplied List rather than the original if the list is modifiable.

      For example,

       List<Restriction<Book>> restrictions = new ArrayList<>();
       restrictions.add(_Book.price.lessThan(15.0f));
       restrictions.add(_Book.hardcovered.isEqualTo(true));
      
       List<Sort<Book>> sorts = new ArrayList<>();
       sorts.add(_Book.price.desc());
       sorts.add(_Book.isbn.asc());
      
       List<Book> found =
               books.titled(title,
                            Restrict.any(restrictions),
                            Order.by(sorts));
       

      The example method above requires the book's title to always match and the book to either be priced under $15 or have a hard cover.

      Type Parameters:
      T - entity type.
      Parameters:
      restrictions - one or more restrictions obtained from a method of Restrict or from a static metamodel Attribute subtype.
      Returns:
      Throws:
      IllegalArgumentException - if the supplied restrictions array is empty or null.
      NullPointerException - if the supplied restrictions array includes a null value.
    • not

      public static <T> Restriction<T> not(Restriction<T> restriction)

      Returns the negation of the specified restriction.

      This restriction returned by this method is obtained by invoking Restriction.negate() on the supplied restriction.

      Returns:
      the negated restriction.
      Throws:
      NullPointerException - if the supplied restriction is null.
    • unrestricted

      public static <T> Restriction<T> unrestricted()

      Returns a restriction that always evaluates to satisfied. This can be used to avoid imposing additional restrictions in places where a Restriction value is required.

      Type Parameters:
      T - entity type.
      Returns:
      a restriction that is always considered to be satisfied.