T
- a Comparable
value typepublic final class Range<T extends java.lang.Comparable<?>> extends java.lang.Object implements Predicate<T>, java.io.Serializable
Useful in filtering in predicates.
A Range consists of a lower and upper endpoint and a Range.BoundType
for each endpoint.
Range.BoundType.CLOSED
includes the endpoint's value in the range and
is represented as "[" or "]" in the string form for a lower and upper bound
type respectively.
Range.BoundType.OPEN
excludes the endpoint's value in the range and
is represented as "(" or ")" in the string form for a lower and upper bound
type respectively. e.g. "[2..5)"
Typically, the convenience methods in Ranges
are used to
construct a Range.
contains()
is used to check for containment:
e.g.
Ranges.closed(2,4).contains(2); // returns true
Ranges.open(2,4).contains(2); // returns false
Ranges.atLeast(2).contains(2); // returns true
Ranges.greaterThan(2).contains(2); // returns false
Ranges.atMost(2).contains(2); // returns true
Ranges.lessThan(2).contains(2); // returns false
toString()
yields a convenient string representation and a
Range may be created from the string representation.
String s = Ranges.closed(2,4).toString(); // yields "[2..4]"
Range<Integer> range = Ranges.valueOfInteger(s); // yields a Range<Integer>(2,4)
Range works with Gson (new Gson().toJson(Range<T>)
.
As as documented by Gson, for generic types you must use
Gson.fromJson(String, java.lang.reflect.Type)
to
create a Range from its json.
Sample use in a TStream context:
TStream<JsonObject> jStream = ...;
TStream<JsonObject> filtered = Filters.deadband(jStream,
json -> json.getProperty("pressureReading").asInteger(),
Ranges.closed(10, 30));
Modifier and Type | Class and Description |
---|---|
static class |
Range.BoundType
Exclude or include an endpoint value in the range.
|
Modifier and Type | Method and Description |
---|---|
boolean |
contains(T v)
Determine if the Region contains the value.
|
boolean |
contains(T v,
java.util.Comparator<T> cmp)
Determine if the Region contains the value.
|
boolean |
equals(java.lang.Object o)
Returns true if o is a Range having equal endpoints and bound types to this Range.
|
int |
hashCode() |
boolean |
hasLowerEndpoint() |
boolean |
hasUpperEndpoint() |
Range.BoundType |
lowerBoundType()
Get the BoundType for the lowerEndpoint.
|
T |
lowerEndpoint()
Get the range's lower endpoint.
|
static <T extends java.lang.Comparable<?>> |
range(T lowerEndpoint,
Range.BoundType lbt,
T upperEndpoint,
Range.BoundType ubt)
Create a new Range<T>
|
boolean |
test(T value)
Predicate.test() implementation.
|
java.lang.String |
toString()
Yields
"<lowerBoundType><lowerEndpoint>..<upperEndpoint><upperBoundType>" . |
java.lang.String |
toStringUnsigned()
Return a String treating the endpoints as an unsigned value.
|
Range.BoundType |
upperBoundType()
Get the BoundType for the upperEndpoint.
|
T |
upperEndpoint()
Get the range's upper endpoint.
|
static <T extends java.lang.Comparable<?>> |
valueOf(java.lang.String toStringValue,
Function<java.lang.String,T> fromString)
Create a Range from a String produced by toString().
|
public static <T extends java.lang.Comparable<?>> Range<T> range(T lowerEndpoint, Range.BoundType lbt, T upperEndpoint, Range.BoundType ubt)
See Ranges
for a collection of convenience constructors.
T
- a Comparable typelowerEndpoint
- null for an infinite value (and lbt must be OPEN)lbt
- Range.BoundType
for the lowerEndpointupperEndpoint
- null for an infinite value (and ubt must be OPEN)ubt
- Range.BoundType
for the upperEndpointpublic boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public boolean hasLowerEndpoint()
public T lowerEndpoint()
java.lang.IllegalStateException
- if hasLowerEndpoint()==falsepublic boolean hasUpperEndpoint()
public T upperEndpoint()
java.lang.IllegalStateException
- if hasUpperEndpoint()==falsepublic Range.BoundType lowerBoundType()
public Range.BoundType upperBoundType()
public boolean contains(T v, java.util.Comparator<T> cmp)
contains(v)
typically suffices. This
can be used in cases where it isn't sufficient.
E.g., for unsigned byte comparisons
Comparator<Byte> unsignedByteComparator = new Comparator<Byte>() { public int compare(Byte b1, Byte b2) { return Integer.compareUnsigned(Byte.toUnsignedInt(b1), Byte.toUnsignedInt(b2)); } public boolean equals(Object o2) { return o2==this; } }; Range<Byte> unsignedByteRange = Ranges.valueOfByte("[0..255]"); unsignedByteRange.contains(byteValue, unsignedByteComparator);
v
- the value to check for containmentcmp
- the Comparator to usepublic boolean contains(T v)
The Comparable's compareTo() is used for the comparisons.
v
- the value to check for containmentcontains(Comparable, Comparator)
public boolean test(T value)
public static <T extends java.lang.Comparable<?>> Range<T> valueOf(java.lang.String toStringValue, Function<java.lang.String,T> fromString)
See Ranges
for a collection of valueOf methods
for several types of T
.
T
- a Comparable typetoStringValue
- value from toString() or has the same syntax.fromString
- function to create a T from its String value from
the parsed toStringValue. Should throw an IllegalArgumentException
if unable to perform the conversion.java.lang.IllegalArgumentException
- if unable to parse or convert to
endpoint in toStringValue to a T.public java.lang.String toString()
"<lowerBoundType><lowerEndpoint>..<upperEndpoint><upperBoundType>"
.
lowerBoundType is either "[" or "(" for BoundType.CLOSED or OPEN respectively. upperBoundType is either "]" or ")" for BoundType.CLOSED or OPEN respectively.
The endpoint value "*" is used to indicate an infinite value. Otherwise, endpoint.toString() is used to get the endpoint's value.
Likely yields an undesirable result when wanting to treat a Byte, Short, Integer, or Long T in an unsigned fashion. See toStringUnsigned().
No special processing is performed to escape/encode a "." present
in an endpoint.toString() value. Hence Range<T>.toString() for
a T
of String
(of value "." or with embedded ".."),
or some other non-numeric type may yield values that are not amenable
to parsing by valueOf(String, Function)
.
.e.g.,
"[120..156)" // lowerEndpoint=120 inclusive, upperEndpoint=156 exclusive "[120..*)" // an "atLeast" 120 range
toString
in class java.lang.Object
public java.lang.String toStringUnsigned()
toString()
java.lang.IllegalArgumentException
- if the Range is not one of
Byte, Short, Integer, LongCopyright © 2016 The Apache Software Foundation. All Rights Reserved - bbe71fa-20161201-1641