Class RangeValidator<T>

    • Constructor Summary

      Constructors 
      Constructor Description
      RangeValidator​(java.lang.String errorMessage, java.util.Comparator<? super T> comparator, T minValue, T maxValue)
      Creates a new range validator of the given type.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      ValidationResult apply​(T value, ValueContext context)
      Returns Result.ok if the value is within the specified bounds, Result.error otherwise.
      T getMaxValue()
      Gets the maximum value of the range.
      T getMinValue()
      Returns the minimum value of the range.
      boolean isMaxValueIncluded()
      Returns whether the maximum value is part of the accepted range.
      boolean isMinValueIncluded()
      Returns whether the minimum value is part of the accepted range.
      protected boolean isValid​(T value)
      Returns whether the given value lies in the valid range.
      static <C extends java.lang.Comparable<? super C>>
      RangeValidator<C>
      of​(java.lang.String errorMessage, C minValue, C maxValue)
      Returns a RangeValidator comparing values of a Comparable type using their natural order.
      void setMaxValue​(T maxValue)
      Sets the maximum value of the range.
      void setMaxValueIncluded​(boolean maxValueIncluded)
      Sets whether the maximum value is part of the accepted range.
      void setMinValue​(T minValue)
      Sets the minimum value of the range.
      void setMinValueIncluded​(boolean minValueIncluded)
      Sets whether the minimum value is part of the accepted range.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.function.BiFunction

        andThen
    • Constructor Detail

      • RangeValidator

        public RangeValidator​(java.lang.String errorMessage,
                              java.util.Comparator<? super T> comparator,
                              T minValue,
                              T maxValue)
        Creates a new range validator of the given type. Passing null to either minValue or maxValue means there is no limit in that direction. Both limits may be null; this can be useful if the limits are resolved programmatically. The result of passing null to apply depends on the given comparator.
        Parameters:
        errorMessage - the error message to return if validation fails, not null
        comparator - the comparator to compare with, not null
        minValue - the least value of the accepted range or null for no limit
        maxValue - the greatest value of the accepted range or null for no limit
    • Method Detail

      • of

        public static <C extends java.lang.Comparable<? super C>> RangeValidator<C> of​(java.lang.String errorMessage,
                                                                                       C minValue,
                                                                                       C maxValue)
        Returns a RangeValidator comparing values of a Comparable type using their natural order. Passing null to either minValue or maxValue means there is no limit in that direction. Both limits may be null; this can be useful if the limits are resolved programmatically.

        Null is considered to be less than any non-null value. This means null never passes validation if a minimum value is specified.

        Type Parameters:
        C - the Comparable value type
        Parameters:
        errorMessage - the error message to return if validation fails, not null
        minValue - the least value of the accepted range or null for no limit
        maxValue - the greatest value of the accepted range or null for no limit
        Returns:
        the new validator
      • apply

        public ValidationResult apply​(T value,
                                      ValueContext context)
        Returns Result.ok if the value is within the specified bounds, Result.error otherwise. If null is passed to apply, the behavior depends on the used comparator.
        Parameters:
        value - the input value to validate
        context - the value context for validation
        Returns:
        the validation result
      • isMinValueIncluded

        public boolean isMinValueIncluded()
        Returns whether the minimum value is part of the accepted range.
        Returns:
        true if the minimum value is part of the range, false otherwise
      • setMinValueIncluded

        public void setMinValueIncluded​(boolean minValueIncluded)
        Sets whether the minimum value is part of the accepted range.
        Parameters:
        minValueIncluded - true if the minimum value should be part of the range, false otherwise
      • isMaxValueIncluded

        public boolean isMaxValueIncluded()
        Returns whether the maximum value is part of the accepted range.
        Returns:
        true if the maximum value is part of the range, false otherwise
      • setMaxValueIncluded

        public void setMaxValueIncluded​(boolean maxValueIncluded)
        Sets whether the maximum value is part of the accepted range.
        Parameters:
        maxValueIncluded - true if the maximum value should be part of the range, false otherwise
      • getMinValue

        public T getMinValue()
        Returns the minimum value of the range.
        Returns:
        the minimum value
      • setMinValue

        public void setMinValue​(T minValue)
        Sets the minimum value of the range. Use setMinValueIncluded(boolean) to control whether this value is part of the range or not.
        Parameters:
        minValue - the minimum value
      • getMaxValue

        public T getMaxValue()
        Gets the maximum value of the range.
        Returns:
        the maximum value
      • setMaxValue

        public void setMaxValue​(T maxValue)
        Sets the maximum value of the range. Use setMaxValueIncluded(boolean) to control whether this value is part of the range or not.
        Parameters:
        maxValue - the maximum value
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • isValid

        protected boolean isValid​(T value)
        Returns whether the given value lies in the valid range.
        Parameters:
        value - the value to validate
        Returns:
        true if the value is valid, false otherwise