Module jakarta.data

Interface Like

All Superinterfaces:
Constraint<String>

public interface Like extends Constraint<String>

A constraint that requires matching a pattern.

A parameter-based repository method can impose a constraint on an entity attribute by defining a method parameter that is of type Like or is annotated @Is(Like.class) and is of type String. For example,

 @Find
 List<Car> matchVIN(@By(_Car.VIN) Like vinPattern);

 @Find // requires the -parameters compiler option to preserve parameter names
 List<Car> makeAndModel(Like make, Like model);

 @Find
 List<Car> search(@By(_Car.MAKE) @Is(Like.class) String makePattern,
                  @By(_Car.MODEL) @Is(Like.class) String modelPattern,
                  Order<Car> sorts);

 ...

 found = cars.matchVIN(Like.prefix("1GM"));

 found = cars.makeAndModel(Like.contains(makeSubstring),
                           Like.contains(modelSubstring));

 found = cars.search("Chev%",
                     "% EV",
                     Order.by(_Car.price.desc()));
 

Repository methods can also accept Like constraints at run time in the form of a Restriction on a TextExpression. For example,

 @Find
 List<Car> searchAll(Restriction<Car> restrict, Order<Car> sorts);

 ...

 found = cars.searchAll(_Car.make.startsWith("Chev"),
                        Order.by(_Car.model.asc(),
                                 _Car.year.desc(),
                                 _Car.price.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
    char
    The escape character to use for the pattern().
    static Like
    literal(String value)
    Requires that the constraint target consist of exactly the same characters as the given value.
    An expression that evaluates to a pattern against which the constraint target must match.
    static Like
    pattern(TextExpression<?> pattern, char escape)
    Requires that the constraint target match the given pattern expression, in which _ and % represent wildcards and the given character represents escape.
    static Like
    pattern(String pattern)
    Requires that the constraint target match the given pattern, in which _ and % represent wildcards.
    static Like
    pattern(String pattern, char charWildcard, char stringWildcard)
    Requires that the constraint target match the given pattern, in which the given characters represent wildcards.
    static Like
    pattern(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.
    static Like
    prefix(String prefix)
    Requires that the constraint target begin with the given prefix.
    static Like
    substring(String substring)
    Requires that the constraint target contain the given substring.
    static Like
    suffix(String suffix)
    Requires that the constraint target end with the given suffix.

    Methods inherited from interface jakarta.data.constraint.Constraint

    negate
  • Method Details

    • pattern

      static Like pattern(String pattern)

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

      For example, the following requires that the first 3 positions of a VIN number are JHM, positions 4 through 6 are any character, position 7 is E, and the remaining positions are any characters,

       found = cars.matchVIN(Like.pattern("JHM___E%"));
       
      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.
    • pattern

      static Like pattern(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.

      For example, the following requires that the first 3 positions of a VIN number are JHM, positions 4 through 6 are any character, position 7 is F, and the remaining positions are any characters,

       found = cars.matchVIN(Like.pattern("JHM???F*", '?', '*'));
       
      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.
    • pattern

      static Like pattern(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.

      For example, the following requires that the first 3 positions of a VIN number are JHM, positions 4 through 6 are any character, position 7 is C, and the remaining positions are any characters,

       found = cars.matchVIN(Like.pattern("JHM---^CC", '-', 'C', '^'));
       
      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.
    • pattern

      static Like pattern(TextExpression<?> pattern, char escape)

      Requires that the constraint target match the given pattern expression, in which _ and % represent wildcards and the given character represents escape.

      Parameters:
      pattern - an expression representing a pattern that can include the given escape character.
      escape - escape character.
      Returns:
      a Like constraint.
      Throws:
      NullPointerException - if the pattern expression is null.
    • prefix

      static Like prefix(String prefix)

      Requires that the constraint target begin with the given prefix.

      For example, the following requires that the first 3 positions of a VIN number are the characters JTP.

       found = cars.matchVIN(Like.prefix("JTP"));
       
      Parameters:
      prefix - text that the beginning characters of the constraint target must exactly match.
      Returns:
      a Like constraint.
      Throws:
      NullPointerException - if the prefix is null.
    • substring

      static Like substring(String substring)

      Requires that the constraint target contain the given substring.

      For example, the following requires that the entity attribute value contain the character string Hybrid,

       found = cars.makeAndModel(Like.literal(make),
                                 Like.substring("Hybrid"));
       
      Parameters:
      substring - text that must be contained in the constraint target.
      Returns:
      a Like constraint.
      Throws:
      NullPointerException - if the substring is null.
    • suffix

      static Like suffix(String suffix)

      Requires that the constraint target end with the given suffix.

      For example, the following requires that the entity attribute value end with the characters EV,

       found = cars.makeAndModel(Like.literal(make),
                                 Like.suffix("EV"));
       
      Parameters:
      suffix - text that the ending characters of the constraint target must exactly match.
      Returns:
      a Like constraint.
      Throws:
      NullPointerException - if the suffix is null.
    • literal

      static Like literal(String value)

      Requires that the constraint target consist of exactly the same characters as the given value. A repository method that does not require the flexibility of allowing different types of Like constraints should use the EqualTo constraint instead.

      For example, the following requires a VIN number to exactly match,

       found = cars.matchVIN(Like.literal(vin));
       
      Parameters:
      value - a value that must exactly match.
      Returns:
      a Like constraint.
      Throws:
      NullPointerException - if the literal value is null.
    • escape

      char escape()

      The escape character to use for the pattern(). The pattern is assigned an escape character even if the application did not supply one when requesting the Like constraint.

      Returns:
      the escape character.
    • pattern

      TextExpression<?> pattern()

      An expression that evaluates to a pattern against which the constraint target must match.

      Any custom wildcards that were supplied appear as _ and % within the pattern, with the escape character used to indicate where characters are interpreted literally rather than as wildcards or escape.

      For example, Like.pattern("is --.-*% of", '-', '*', '^') is represented as is __._%^% of where ^ is the escape character.

      Returns:
      an expression representing the pattern.