Package org.openjdk.jmc.common.unit
Class ComparableConstraint<T extends java.lang.Comparable<T>>
- java.lang.Object
-
- org.openjdk.jmc.common.unit.ComparableConstraint<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>
Aconstraint
that wraps apersister
forComparable
values, and constrains the allowed values with minimum and maximum values.
-
-
Constructor Summary
Constructors Constructor Description ComparableConstraint(IPersister<T> persister, T min, T max)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ComparableConstraint<T>
combine(IConstraint<?> other)
Return a constraint that honors both this constraint andother
, if such a constraint would accept anything exceptnull
.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>>
IConstraint<U>constrain(IConstraint<U> constraint, java.lang.String persistedMin, java.lang.String persistedMax)
static <U,T extends java.lang.Comparable<T>>
IConstraint<U>constrain(IConstraint<U> constraint, U min, U max)
private IConstraint<T>
constrain(T otherMin, T otherMax)
private static <T extends java.lang.Comparable<T>>
IConstraint<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 usingIConstraint.parsePersisted(String)
(on this instance) yields a result that isequal
to the givenvalue
.boolean
validate(T value)
Fundamentally, check thatvalue
satisfies this constraint and throw an exception otherwise.protected boolean
validateRange(T value)
-
-
-
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, java.lang.String persistedMin, java.lang.String persistedMax) throws QuantityConversionException
- Throws:
QuantityConversionException
-
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 andother
, if such a constraint would accept anything exceptnull
. Otherwise, returnnull
.- Specified by:
combine
in interfaceIConstraint<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 thatvalue
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 interfaceIConstraint<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
-
validateRange
protected boolean validateRange(T value) throws QuantityConversionException
- Throws:
QuantityConversionException
-
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 usingIConstraint.parsePersisted(String)
(on this instance) yields a result that isequal
to the givenvalue
. That is, the exact representation must be preserved.- Specified by:
persistableString
in interfaceIConstraint<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
-
parsePersisted
public T parsePersisted(java.lang.String persistedValue) throws QuantityConversionException
Description copied from interface:IConstraint
Parse a persisted string. Only guaranteed to be able to parse strings produced byIConstraint.persistableString(Object)
on this instance. Only use this on persisted strings, never for interactive input.- Specified by:
parsePersisted
in interfaceIConstraint<T extends java.lang.Comparable<T>>
- Returns:
- a valid value for this instance
- Throws:
QuantityConversionException
- ifpersistedValue
couldn't be parsed or didn't satisfy the constraint
-
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 usingIConstraint.parseInteractive(String)
(on this instance) yields a result that isequal
to the givenvalue
. That is, the exact representation must be preserved.- Specified by:
interactiveFormat
in interfaceIConstraint<T extends java.lang.Comparable<T>>
- Returns:
- a string representation taking locale and internationalization into account.
- Throws:
QuantityConversionException
- ifvalue
doesn't satisfy the constraint
-
parseInteractive
public T parseInteractive(java.lang.String interactiveValue) throws QuantityConversionException
Description copied from interface:IConstraint
Parse an interactive string. Only guaranteed to be able to parse strings produced byIConstraint.interactiveFormat(Object)
on this instance and in the same locale. Only use this for interactive input, never for persisted strings.- Specified by:
parseInteractive
in interfaceIConstraint<T extends java.lang.Comparable<T>>
- Returns:
- a valid value for this instance
- Throws:
QuantityConversionException
- ifinteractiveValue
couldn't be parsed or didn't satisfy the constraint
-
format
public java.lang.String format(T value)
- Specified by:
format
in interfaceIFormatter<T extends java.lang.Comparable<T>>
-
-