Package org.apache.pivot.wtk
Class Limits
- java.lang.Object
-
- org.apache.pivot.wtk.Limits
-
- All Implemented Interfaces:
java.io.Serializable
public final class Limits extends java.lang.Object implements java.io.Serializable
Immutable object representing minimum and maximum values.Note: these values are inclusive, and so the minimum can be equal to the maximum, implying a range of one value.
Also note that minimum must be less than or equal the maximum at construction or decode time.
- See Also:
contains(int)
,constrain(int)
, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description int
maximum
static java.lang.String
MAXIMUM_KEY
int
minimum
static java.lang.String
MINIMUM_KEY
-
Constructor Summary
Constructors Constructor Description Limits()
Limits(int value)
Construct a new limits of a single value, setting both the minimum and maximum to this value.Limits(int minimum, int maximum)
Limits(Dictionary<java.lang.String,?> limits)
Construct a new Limits based on the given Dictionary or Map.Limits(Sequence<?> limits)
Limits(Limits limits)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
constrain(int value)
Limits the specified value to the minimum and maximum values of this object.boolean
contains(int value)
Determines whether the given value is contained by thisLimits
, that is, whether thevalue >= minimum
andvalue <= maximum
.static Limits
decode(java.lang.String value)
Decode a JSON-encoded string (map or list) that contains the values for a new limits.boolean
equals(java.lang.Object object)
int
hashCode()
long
range()
java.lang.String
toString()
-
-
-
Field Detail
-
minimum
public final int minimum
-
maximum
public final int maximum
-
MINIMUM_KEY
public static final java.lang.String MINIMUM_KEY
- See Also:
- Constant Field Values
-
MAXIMUM_KEY
public static final java.lang.String MAXIMUM_KEY
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Limits
public Limits()
-
Limits
public Limits(int minimum, int maximum)
-
Limits
public Limits(int value)
Construct a new limits of a single value, setting both the minimum and maximum to this value.- Parameters:
value
- The single value range for this limits.
-
Limits
public Limits(Limits limits)
-
Limits
public Limits(Dictionary<java.lang.String,?> limits)
Construct a new Limits based on the given Dictionary or Map.The map keys for the values are
MINIMUM_KEY
andMAXIMUM_KEY
. Missing minimum value will setInteger.MIN_VALUE
as the min, and missing maximum will setInteger.MAX_VALUE
as the max.- Parameters:
limits
- The map/dictionary containing the desired limits values.- Throws:
java.lang.IllegalArgumentException
- if the min is greater than the max.
-
Limits
public Limits(Sequence<?> limits)
-
-
Method Detail
-
range
public long range()
- Returns:
- The range of this limits, that is, the maximum less the minimum plus one (since the limits are inclusive). Returns a long value because the default min and max are the maximum range of the integers, so that the range in this case cannot be represented by an integer.
-
constrain
public int constrain(int value)
Limits the specified value to the minimum and maximum values of this object.- Parameters:
value
- The value to limit.- Returns:
- The bounded value.
-
contains
public boolean contains(int value)
Determines whether the given value is contained by thisLimits
, that is, whether thevalue >= minimum
andvalue <= maximum
.- Parameters:
value
- The value to test.- Returns:
- Whether the value is contained within the limits.
-
equals
public boolean equals(java.lang.Object object)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
decode
public static Limits decode(java.lang.String value)
Decode a JSON-encoded string (map or list) that contains the values for a new limits.The format of a JSON map format will be:
{ "minimum": nnn, "maximum": nnn }
The format of a JSON list format will be:
[ minimum, maximum ]
Also accepted is a simple comma- or semicolon-separated list of two integer values, as in:
min, max
, or asmin-max
(as in the format produced bytoString()
).- Parameters:
value
- The JSON string containing the map or list of limits values (must not benull
).- Returns:
- The new limits object if the string can be successfully decoded.
- Throws:
java.lang.IllegalArgumentException
- if the given string isnull
or empty or the string could not be parsed as a JSON map or list.- See Also:
Limits(Dictionary)
,Limits(int, int)
-
-