Rangepublic final class Range extends Object Immutable class for describing the range of two numeric values.
A range (or "interval") defines the inclusive boundaries around a contiguous span of
values of some {@link Comparable} type; for example,
"integers from 1 to 100 inclusive."
All ranges are bounded, and the left side of the range is always {@code >=}
the right side of the range.
Although the implementation itself is immutable, there is no restriction that objects
stored must also be immutable. If mutable objects are stored here, then the range
effectively becomes mutable. |
Fields Summary |
---|
private final T | mLower | private final T | mUpper |
Constructors Summary |
---|
public Range(T lower, T upper)Create a new immutable range.
The endpoints are {@code [lower, upper]}; that
is the range is bounded. {@code lower} must be {@link Comparable#compareTo lesser or equal}
to {@code upper}.
mLower = checkNotNull(lower, "lower must not be null");
mUpper = checkNotNull(upper, "upper must not be null");
if (lower.compareTo(upper) > 0) {
throw new IllegalArgumentException("lower must be less than or equal to upper");
}
|
Methods Summary |
---|
public T | clamp(T value)Clamps {@code value} to this range.
If the value is within this range, it is returned. Otherwise, if it
is {@code <} than the lower endpoint, the lower endpoint is returned,
else the upper endpoint is returned. Comparisons are performed using the
{@link Comparable} interface.
checkNotNull(value, "value must not be null");
if (value.compareTo(mLower) < 0) {
return mLower;
} else if (value.compareTo(mUpper) > 0) {
return mUpper;
} else {
return value;
}
| public boolean | contains(T value)Checks if the {@code value} is within the bounds of this range.
A value is considered to be within this range if it's {@code >=}
the lower endpoint and {@code <=} the upper endpoint (using the {@link Comparable}
interface.)
checkNotNull(value, "value must not be null");
boolean gteLower = value.compareTo(mLower) >= 0;
boolean lteUpper = value.compareTo(mUpper) <= 0;
return gteLower && lteUpper;
| public boolean | contains(android.util.Range range)Checks if another {@code range} is within the bounds of this range.
A range is considered to be within this range if both of its endpoints
are within this range.
checkNotNull(range, "value must not be null");
boolean gteLower = range.mLower.compareTo(mLower) >= 0;
boolean lteUpper = range.mUpper.compareTo(mUpper) <= 0;
return gteLower && lteUpper;
| public static android.util.Range | create(T lower, T upper)Create a new immutable range, with the argument types inferred.
The endpoints are {@code [lower, upper]}; that
is the range is bounded. {@code lower} must be {@link Comparable#compareTo lesser or equal}
to {@code upper}.
return new Range<T>(lower, upper);
| public boolean | equals(java.lang.Object obj)Compare two ranges for equality.
A range is considered equal if and only if both the lower and upper endpoints
are also equal.
if (obj == null) {
return false;
} else if (this == obj) {
return true;
} else if (obj instanceof Range) {
@SuppressWarnings("rawtypes")
Range other = (Range) obj;
return mLower.equals(other.mLower) && mUpper.equals(other.mUpper);
}
return false;
| public android.util.Range | extend(android.util.Range range)Returns the smallest range that includes this range and
another {@code range}.
E.g. if a {@code <} b {@code <} c {@code <} d, the
extension of [a, c] and [b, d] ranges is [a, d].
As the endpoints are object references, there is no guarantee
which specific endpoint reference is used from the input ranges:
E.g. if a {@code ==} a' {@code <} b {@code <} c, the
extension of [a, b] and [a', c] ranges could be either
[a, c] or ['a, c], where ['a, c] could be either the exact
input range, or a newly created range with the same endpoints.
checkNotNull(range, "range must not be null");
int cmpLower = range.mLower.compareTo(mLower);
int cmpUpper = range.mUpper.compareTo(mUpper);
if (cmpLower <= 0 && cmpUpper >= 0) {
// other includes this
return range;
} else if (cmpLower >= 0 && cmpUpper <= 0) {
// this inludes other
return this;
} else {
return Range.create(
cmpLower >= 0 ? mLower : range.mLower,
cmpUpper <= 0 ? mUpper : range.mUpper);
}
| public android.util.Range | extend(T lower, T upper)Returns the smallest range that includes this range and
the inclusive range specified by {@code [lower, upper]}.
See {@link #extend(Range)} for more details.
checkNotNull(lower, "lower must not be null");
checkNotNull(upper, "upper must not be null");
int cmpLower = lower.compareTo(mLower);
int cmpUpper = upper.compareTo(mUpper);
if (cmpLower >= 0 && cmpUpper <= 0) {
// this inludes other
return this;
} else {
return Range.create(
cmpLower >= 0 ? mLower : lower,
cmpUpper <= 0 ? mUpper : upper);
}
| public android.util.Range | extend(T value)Returns the smallest range that includes this range and
the {@code value}.
See {@link #extend(Range)} for more details, as this method is
equivalent to {@code extend(Range.create(value, value))}.
checkNotNull(value, "value must not be null");
return extend(value, value);
| public T | getLower()Get the lower endpoint.
return mLower;
| public T | getUpper()Get the upper endpoint.
return mUpper;
| public int | hashCode(){@inheritDoc}
return HashCodeHelpers.hashCode(mLower, mUpper);
| public android.util.Range | intersect(T lower, T upper)Returns the intersection of this range and the inclusive range
specified by {@code [lower, upper]}.
See {@link #intersect(Range)} for more details.
checkNotNull(lower, "lower must not be null");
checkNotNull(upper, "upper must not be null");
int cmpLower = lower.compareTo(mLower);
int cmpUpper = upper.compareTo(mUpper);
if (cmpLower <= 0 && cmpUpper >= 0) {
// [lower, upper] includes this
return this;
} else {
return Range.create(
cmpLower <= 0 ? mLower : lower,
cmpUpper >= 0 ? mUpper : upper);
}
| public android.util.Range | intersect(android.util.Range range)Returns the intersection of this range and another {@code range}.
E.g. if a {@code <} b {@code <} c {@code <} d, the
intersection of [a, c] and [b, d] ranges is [b, c].
As the endpoints are object references, there is no guarantee
which specific endpoint reference is used from the input ranges:
E.g. if a {@code ==} a' {@code <} b {@code <} c, the
intersection of [a, b] and [a', c] ranges could be either
[a, b] or ['a, b], where [a, b] could be either the exact
input range, or a newly created range with the same endpoints.
checkNotNull(range, "range must not be null");
int cmpLower = range.mLower.compareTo(mLower);
int cmpUpper = range.mUpper.compareTo(mUpper);
if (cmpLower <= 0 && cmpUpper >= 0) {
// range includes this
return this;
} else if (cmpLower >= 0 && cmpUpper <= 0) {
// this inludes range
return range;
} else {
return Range.create(
cmpLower <= 0 ? mLower : range.mLower,
cmpUpper >= 0 ? mUpper : range.mUpper);
}
| public java.lang.String | toString()Return the range as a string representation {@code "[lower, upper]"}.
return String.format("[%s, %s]", mLower, mUpper);
|
|