Class FieldRejection<T>

  • Type Parameters:
    T - The type of the value to evaluate.

    public class FieldRejection<T>
    extends Object
    Reject a field based on a few conditions.

    For instance:

     new FieldRejection(validationResult, "houseNumber", "13-a")
             .whenNull()
             .orWhen(h -> h.contains("a"))
             .orWhen(h -> h.length() > 10);
     

    If used with the ValidationResult class this will look like:

     validationResult.rejectField("houseNumber", "13-a")
             .whenNull()
             .orWhen(h -> h.contains("a'))
             .orWhen(h -> h.length() > 10);
     

    The rejections without code parameters have the value invalid, except the whenNull(), this has the required code value.

    The chain will stop evaluating the rejection clauses after the first matching clause. In the examples above the chain will not evaluate the length of the house number.

    Author:
    Rutger Lubbers
    • Constructor Detail

      • FieldRejection

        public FieldRejection​(ValidationResult validationResult,
                              String field,
                              T actual)
        Construct a new field rejection.
        Parameters:
        validationResult - The validation result.
        field - The field name (property name).
        actual - The field's value.
    • Method Detail

      • or

        public FieldRejection<T> or()
        Syntactic sugar, allows the use of or().when(...).or().when(...) syntaxis.
        Returns:
        The current field rejection.
      • or

        public FieldRejection<T> or​(Matcher<T> matcher)
        Rejects the field when the matcher matches the actual value. This will reject the field with the error code invalid.
        Parameters:
        matcher - The matcher to use.
        Returns:
        The current field rejection.
      • or

        public FieldRejection<T> or​(Matcher<T> matcher,
                                    String code)
        Rejects the field when the matcher matches the actual value. This will reject the field with supplied the error code code.
        Parameters:
        matcher - The matcher to use.
        Returns:
        The current field rejection.
      • or

        public FieldRejection<T> or​(Predicate<T> predicate)
        Rejects the field when the predicate evaluates to true. This will reject the field with the error code invalid.
        Parameters:
        predicate - The predicate to use.
        Returns:
        The current field rejection.
      • or

        public FieldRejection<T> or​(Predicate<T> predicate,
                                    String code)
        Rejects the field when the predicate evaluates to true. This will reject the field with supplied the error code code.
        Parameters:
        predicate - The predicate to use.
        Returns:
        The current field rejection.
      • or

        public <R> FieldRejection<T> or​(Function<T,​R> function,
                                        Matcher<R> matcher)
        Rejects the field when the matcher matches the result of the function. This will reject the field with the error code invalid.
        Parameters:
        function - The function to apply to the actual value.
        matcher - The matcher to use against the return value of the function.
        Returns:
        The current field rejection.
      • or

        public <R> FieldRejection<T> or​(Function<T,​R> function,
                                        Matcher<R> matcher,
                                        String code)
        Rejects the field when the matcher matches the result of the function. This will reject the field with supplied the error code code.
        Parameters:
        function - The function to apply to the actual value.
        matcher - The matcher to use against the return value of the function.
        Returns:
        The current field rejection.
      • whenNull

        public FieldRejection<T> whenNull()
        Rejects the field when the actual is null. This will reject the field with the error code required.
        Returns:
        The current field rejection.
      • whenNull

        public FieldRejection<T> whenNull​(String code)
        Rejects the field when the actual is null. This will reject the field with supplied the error code code.
        Parameters:
        code - The error code to set if the actual value is null.
        Returns:
        The current field rejection.
      • when

        public FieldRejection<T> when​(Matcher<T> matcher)
        Rejects the field when the matcher matches the actual value. This will reject the field with the error code invalid.
        Parameters:
        matcher - The matcher to use.
        Returns:
        The current field rejection.
      • when

        public FieldRejection<T> when​(Matcher<T> matcher,
                                      String code)
        Rejects the field when the matcher matches the actual value. This will reject the field with supplied the error code code.
        Parameters:
        matcher - The matcher to use.
        Returns:
        The current field rejection.
      • when

        public FieldRejection<T> when​(Predicate<T> predicate)
        Rejects the field when the predicate evaluates to true. This will reject the field with the error code invalid.
        Parameters:
        predicate - The predicate to use.
        Returns:
        The current field rejection.
      • when

        public <R> FieldRejection<T> when​(Function<T,​R> function,
                                          Matcher<R> matcher)
        Rejects the field when the matcher matches the result of the function. This will reject the field with the error code invalid.
        Parameters:
        function - The function to apply to the actual value.
        matcher - The matcher to use against the return value of the function.
        Returns:
        The current field rejection.
      • when

        public <R> FieldRejection<T> when​(Function<T,​R> function,
                                          Matcher<R> matcher,
                                          String code)
        Rejects the field when the matcher matches the result of the function. This will reject the field with supplied the error code code.
        Parameters:
        function - The function to apply to the actual value.
        matcher - The matcher to use against the return value of the function.
        Returns:
        The current field rejection.
      • when

        public FieldRejection<T> when​(Predicate<T> predicate,
                                      String code)
        Rejects the field when the predicate evaluates to true. This will reject the field with supplied the error code code.
        Parameters:
        predicate - The predicate to use.
        Returns:
        The current field rejection.
      • orWhen

        public FieldRejection<T> orWhen​(Matcher<T> matcher)
        Rejects the field when the matcher matches the actual value. This will reject the field with the error code invalid.
        Parameters:
        matcher - The matcher to use.
        Returns:
        The current field rejection.
      • orWhen

        public FieldRejection<T> orWhen​(Matcher<T> matcher,
                                        String code)
        Rejects the field when the matcher matches the actual value. This will reject the field with supplied the error code code.
        Parameters:
        matcher - The matcher to use.
        Returns:
        The current field rejection.
      • orWhen

        public FieldRejection<T> orWhen​(Predicate<T> predicate)
        Rejects the field when the predicate evaluates to true. This will reject the field with the error code invalid.
        Parameters:
        predicate - The predicate to use.
        Returns:
        The current field rejection.
      • orWhen

        public FieldRejection<T> orWhen​(Predicate<T> predicate,
                                        String code)
        Rejects the field when the predicate evaluates to true. This will reject the field with supplied the error code code.
        Parameters:
        predicate - The predicate to use.
        Returns:
        The current field rejection.
      • orWhen

        public <R> FieldRejection<T> orWhen​(Function<T,​R> function,
                                            Matcher<R> matcher)
        Rejects the field when the matcher matches the result of the function. This will reject the field with the error code invalid.
        Parameters:
        function - The function to apply to the actual value.
        matcher - The matcher to use against the return value of the function.
        Returns:
        The current field rejection.
      • orWhen

        public <R> FieldRejection<T> orWhen​(Function<T,​R> function,
                                            Matcher<R> matcher,
                                            String code)
        Rejects the field when the matcher matches the result of the function. This will reject the field with supplied the error code code.
        Parameters:
        function - The function to apply to the actual value.
        matcher - The matcher to use against the return value of the function.
        Returns:
        The current field rejection.