T
- the type of values that the constraint operates onpublic final class ComparableConstraint<T extends java.lang.Comparable<T>> extends java.lang.Object implements IConstraint<T>, IFormatter<T>
constraint
that wraps a persister
for Comparable
values, and constrains the allowed values with minimum and maximum values.Modifier and Type | Field and Description |
---|---|
private T |
max |
private T |
min |
private IPersister<T> |
persister |
Constructor and Description |
---|
ComparableConstraint(IPersister<T> persister,
T min,
T max) |
Modifier and Type | Method and Description |
---|---|
ComparableConstraint<T> |
combine(IConstraint<?> other)
Return a constraint that honors both this constraint and
other , if such a constraint
would accept anything except null . |
protected ComparableConstraint<T> |
combineNonSame(ComparableConstraint<T> other)
Combine with other ComparableConstraint known not to be the same instance, but having the
same persister.
|
static <U,T extends java.lang.Comparable<T>> |
constrain(IConstraint<U> constraint,
java.lang.String persistedMin,
java.lang.String persistedMax) |
static <U,T extends java.lang.Comparable<T>> |
constrain(IConstraint<U> constraint,
U min,
U max) |
private IConstraint<T> |
constrain(T otherMin,
T otherMax) |
private static <T extends java.lang.Comparable<T>> |
constrainComparable(IConstraint<T> constraint,
T min,
T max) |
java.lang.String |
format(T value) |
java.lang.String |
interactiveFormat(T value)
An exact string representation taking locale and internationalization into account.
|
T |
parseInteractive(java.lang.String interactiveValue)
Parse an interactive string.
|
T |
parsePersisted(java.lang.String persistedValue)
Parse a persisted string.
|
java.lang.String |
persistableString(T value)
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 . |
boolean |
validate(T value)
Fundamentally, check that
value satisfies this constraint and throw an exception
otherwise. |
protected boolean |
validateRange(T value) |
private final IPersister<T extends java.lang.Comparable<T>> persister
public ComparableConstraint(IPersister<T> persister, T min, T max)
public static <U,T extends java.lang.Comparable<T>> IConstraint<U> constrain(IConstraint<U> constraint, java.lang.String persistedMin, java.lang.String persistedMax) throws QuantityConversionException
QuantityConversionException
public static <U,T extends java.lang.Comparable<T>> IConstraint<U> constrain(IConstraint<U> constraint, U min, U max)
private static <T extends java.lang.Comparable<T>> IConstraint<T> constrainComparable(IConstraint<T> constraint, T min, T max)
private IConstraint<T> constrain(T otherMin, T otherMax)
public ComparableConstraint<T> combine(IConstraint<?> other)
IConstraint
other
, if such a constraint
would accept anything except null
. Otherwise, return null
.combine
in interface IConstraint<T extends java.lang.Comparable<T>>
null
protected ComparableConstraint<T> combineNonSame(ComparableConstraint<T> other)
null
public boolean validate(T value) throws QuantityConversionException
IConstraint
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.validate
in interface IConstraint<T extends java.lang.Comparable<T>>
value
is valid.QuantityConversionException
- if the constraint isn't satisfied in some other wayprotected boolean validateRange(T value) throws QuantityConversionException
QuantityConversionException
public java.lang.String persistableString(T value) throws QuantityConversionException
IConstraint
IConstraint.parsePersisted(String)
(on this instance) yields a result that is
equal
to the given value
. That is, the exact
representation must be preserved.persistableString
in interface IConstraint<T extends java.lang.Comparable<T>>
QuantityConversionException
- if the constraint isn't satisfied in some other waypublic T parsePersisted(java.lang.String persistedValue) throws QuantityConversionException
IConstraint
IConstraint.persistableString(Object)
on this instance. Only use this on persisted strings,
never for interactive input.parsePersisted
in interface IConstraint<T extends java.lang.Comparable<T>>
QuantityConversionException
- if persistedValue
couldn't be parsed or didn't satisfy the constraintpublic java.lang.String interactiveFormat(T value) throws QuantityConversionException
IConstraint
IConstraint.parseInteractive(String)
(on this instance) yields a result that is
equal
to the given value
. That is, the exact
representation must be preserved.interactiveFormat
in interface IConstraint<T extends java.lang.Comparable<T>>
QuantityConversionException
- if value
doesn't satisfy the constraintpublic T parseInteractive(java.lang.String interactiveValue) throws QuantityConversionException
IConstraint
IConstraint.interactiveFormat(Object)
on this instance and in the same locale. Only use this for
interactive input, never for persisted strings.parseInteractive
in interface IConstraint<T extends java.lang.Comparable<T>>
QuantityConversionException
- if interactiveValue
couldn't be parsed or didn't satisfy the constraintpublic java.lang.String format(T value)
format
in interface IFormatter<T extends java.lang.Comparable<T>>