public final class ValueRange extends Object implements Serializable
All TemporalField
instances have a valid range of values.
For example, the ISO day-of-month runs from 1 to somewhere between 28 and 31.
This class captures that valid range.
It is important to be aware of the limitations of this class. Only the minimum and maximum values are provided. It is possible for there to be invalid values within the outer range. For example, a weird field may have valid values of 1, 2, 4, 6, 7, thus have a range of '1 - 7', despite that fact that values 3 and 5 are invalid.
Instances of this class are not tied to a specific field.
Modifier and Type | Method and Description |
---|---|
int |
checkValidIntValue(long value,
TemporalField field)
Checks that the specified value is valid and fits in an
int . |
long |
checkValidValue(long value,
TemporalField field)
Checks that the specified value is valid.
|
boolean |
equals(Object obj)
Checks if this range is equal to another range.
|
long |
getLargestMinimum()
Gets the largest possible minimum value that the field can take.
|
long |
getMaximum()
Gets the maximum value that the field can take.
|
long |
getMinimum()
Gets the minimum value that the field can take.
|
long |
getSmallestMaximum()
Gets the smallest possible maximum value that the field can take.
|
int |
hashCode()
A hash code for this range.
|
boolean |
isFixed()
Is the value range fixed and fully known.
|
boolean |
isIntValue()
Checks if all values in the range fit in an
int . |
boolean |
isValidIntValue(long value)
Checks if the value is within the valid range and that all values
in the range fit in an
int . |
boolean |
isValidValue(long value)
Checks if the value is within the valid range.
|
static ValueRange |
of(long min,
long max)
Obtains a fixed value range.
|
static ValueRange |
of(long min,
long maxSmallest,
long maxLargest)
Obtains a variable value range.
|
static ValueRange |
of(long minSmallest,
long minLargest,
long maxSmallest,
long maxLargest)
Obtains a fully variable value range.
|
String |
toString()
Outputs this range as a
String . |
public static ValueRange of(long min, long max)
This factory obtains a range where the minimum and maximum values are fixed. For example, the ISO month-of-year always runs from 1 to 12.
min
- the minimum valuemax
- the maximum valueIllegalArgumentException
- if the minimum is greater than the maximumpublic static ValueRange of(long min, long maxSmallest, long maxLargest)
This factory obtains a range where the minimum value is fixed and the maximum value may vary. For example, the ISO day-of-month always starts at 1, but ends between 28 and 31.
min
- the minimum valuemaxSmallest
- the smallest maximum valuemaxLargest
- the largest maximum valueIllegalArgumentException
- if
the minimum is greater than the smallest maximum,
or the smallest maximum is greater than the largest maximumpublic static ValueRange of(long minSmallest, long minLargest, long maxSmallest, long maxLargest)
This factory obtains a range where both the minimum and maximum value may vary.
minSmallest
- the smallest minimum valueminLargest
- the largest minimum valuemaxSmallest
- the smallest maximum valuemaxLargest
- the largest maximum valueIllegalArgumentException
- if
the smallest minimum is greater than the smallest maximum,
or the smallest maximum is greater than the largest maximum
or the largest minimum is greater than the largest maximumpublic boolean isFixed()
For example, the ISO day-of-month runs from 1 to between 28 and 31. Since there is uncertainty about the maximum value, the range is not fixed. However, for the month of January, the range is always 1 to 31, thus it is fixed.
public long getMinimum()
For example, the ISO day-of-month always starts at 1. The minimum is therefore 1.
public long getLargestMinimum()
For example, the ISO day-of-month always starts at 1. The largest minimum is therefore 1.
public long getSmallestMaximum()
For example, the ISO day-of-month runs to between 28 and 31 days. The smallest maximum is therefore 28.
public long getMaximum()
For example, the ISO day-of-month runs to between 28 and 31 days. The maximum is therefore 31.
public boolean isIntValue()
int
.
This checks that all valid values are within the bounds of an int
.
For example, the ISO month-of-year has values from 1 to 12, which fits in an int
.
By comparison, ISO nano-of-day runs from 1 to 86,400,000,000,000 which does not fit in an int
.
This implementation uses getMinimum()
and getMaximum()
.
int
public boolean isValidValue(long value)
This checks that the value is within the stored range of values.
value
- the value to checkpublic boolean isValidIntValue(long value)
int
.
This method combines isIntValue()
and isValidValue(long)
.
value
- the value to checkint
public long checkValidValue(long value, TemporalField field)
This validates that the value is within the valid range of values. The field is only used to improve the error message.
value
- the value to checkfield
- the field being checked, may be nullisValidValue(long)
public int checkValidIntValue(long value, TemporalField field)
int
.
This validates that the value is within the valid range of values and that
all valid values are within the bounds of an int
.
The field is only used to improve the error message.
value
- the value to checkfield
- the field being checked, may be nullisValidIntValue(long)
public boolean equals(Object obj)
The comparison is based on the four values, minimum, largest minimum,
smallest maximum and maximum.
Only objects of type ValueRange
are compared, other types return false.
equals
in class Object
obj
- the object to check, null returns falseObject.hashCode()
,
HashMap
public int hashCode()
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public String toString()
String
.
The format will be '{min}/{largestMin} - {smallestMax}/{max}', where the largestMin or smallestMax sections may be omitted, together with associated slash, if they are the same as the min or max.
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2024, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.