Class ComparableConstraint<T extends java.lang.Comparable<T>>

  • Type Parameters:
    T - the type of values that the constraint operates on
    All Implemented Interfaces:
    IConstraint<T>, IFormatter<T>

    public final class ComparableConstraint<T extends java.lang.Comparable<T>>
    extends java.lang.Object
    implements IConstraint<T>, IFormatter<T>
    A constraint that wraps a persister for Comparable values, and constrains the allowed values with minimum and maximum values.
    • Field Detail

      • persister

        private final IPersister<T extends java.lang.Comparable<T>> persister
      • min

        private final T extends java.lang.Comparable<T> min
      • max

        private final T extends java.lang.Comparable<T> max
    • Constructor Detail

      • ComparableConstraint

        public ComparableConstraint​(IPersister<T> persister,
                                    T min,
                                    T max)
    • Method Detail

      • constrain

        public static <U,​T extends java.lang.Comparable<T>> IConstraint<U> constrain​(IConstraint<U> constraint,
                                                                                           U min,
                                                                                           U max)
      • constrainComparable

        private static <T extends java.lang.Comparable<T>> IConstraint<T> constrainComparable​(IConstraint<T> constraint,
                                                                                              T min,
                                                                                              T max)
      • constrain

        private IConstraint<T> constrain​(T otherMin,
                                         T otherMax)
      • combine

        public ComparableConstraint<T> combine​(IConstraint<?> other)
        Description copied from interface: IConstraint
        Return a constraint that honors both this constraint and other, if such a constraint would accept anything except null. Otherwise, return null.
        Specified by:
        combine in interface IConstraint<T extends java.lang.Comparable<T>>
        Returns:
        a constraint or null
      • combineNonSame

        protected ComparableConstraint<T> combineNonSame​(ComparableConstraint<T> other)
        Combine with other ComparableConstraint known not to be the same instance, but having the same persister.
        Returns:
        a combined constraint, if possible, otherwise null
      • validate

        public boolean validate​(T value)
                         throws QuantityConversionException
        Description copied from interface: IConstraint
        Fundamentally, check that value satisfies this constraint and throw an exception otherwise. As long as the method returns normally, value is a valid value, regardless of the return value. However, when wrapping a persister in a constraint, it is possible that the persister treats some magic values differently. If the constraint isn't aware of these magical values it should typically not try to validate them. This is signaled by the persister by returning true from this method.
        Specified by:
        validate in interface IConstraint<T extends java.lang.Comparable<T>>
        Returns:
        true if this value is considered magical and further validation should be skipped, false otherwise. Any return value mean that the value is valid.
        Throws:
        QuantityConversionException - if the constraint isn't satisfied in some other way
      • persistableString

        public java.lang.String persistableString​(T value)
                                           throws QuantityConversionException
        Description copied from interface: IConstraint
        A string representation independent of locale or internationalization, that when parsed using IConstraint.parsePersisted(String) (on this instance) yields a result that is equal to the given value. That is, the exact representation must be preserved.
        Specified by:
        persistableString in interface IConstraint<T extends java.lang.Comparable<T>>
        Returns:
        a string representation independent of locale or internationalization.
        Throws:
        QuantityConversionException - if the constraint isn't satisfied in some other way
      • interactiveFormat

        public java.lang.String interactiveFormat​(T value)
                                           throws QuantityConversionException
        Description copied from interface: IConstraint
        An exact string representation taking locale and internationalization into account. When parsed using IConstraint.parseInteractive(String) (on this instance) yields a result that is equal to the given value. That is, the exact representation must be preserved.
        Specified by:
        interactiveFormat in interface IConstraint<T extends java.lang.Comparable<T>>
        Returns:
        a string representation taking locale and internationalization into account.
        Throws:
        QuantityConversionException - if value doesn't satisfy the constraint
      • format

        public java.lang.String format​(T value)
        Specified by:
        format in interface IFormatter<T extends java.lang.Comparable<T>>