java.lang.Object
io.github.darvil.utils.Range
A range class to contain a start and end value, or a single value. Both non-negative.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionbooleancontains(int value, boolean startInclusive, boolean endInclusive) Returnstrueif the given value is in the range.booleancontainsExclusive(int value) Returnstrueif the given value is in the range, exclusive.booleancontainsInclusive(int value) Returnstrueif the given value is in the range, inclusive.intend()Returns the end value, orInteger.MAX_VALUEif the range is infinite.static @NotNull Range.RangeBuilderfrom(int start) Creates a new range builder with the given start value.@NotNull StringgetMessage(@NotNull String kind) Returns a string representation of the range, such as "from 3 to 5 times", or "3 times".@NotNull StringReturns a string representation of the range, such as"{3, 5}"or"{3}".booleanReturnstrueif the end value is infinity.booleanisSimple()Returnstrueif this is representing a single value.booleanisZero()Returnstrueif the range is 0.iterator()Returns an iterator that iterates over the range.iterator(boolean startInclusive, boolean endInclusive) Returns an iterator that iterates over the range.static @NotNull Rangeof(int value) Creates a new single-value range with the given value.@NotNull Rangeoffset(int offset) Offsets both the start and end values by the given offset.intstart()Returns the start value.int[]toArray()Returns an array of all the values in the range, inclusive.int[]toArray(boolean startInclusive, boolean endInclusive) Returns an array of all the values in the range.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
ANY
A range between 0 and infinity. -
AT_LEAST_ONE
A range between 1 and infinity. -
NONE
A range of 0. -
ONE
A range of 1. -
NONE_OR_ONE
A range between 0 and 1.
-
-
Method Details
-
from
Creates a new range builder with the given start value. -
of
Creates a new single-value range with the given value. -
isSimple
public boolean isSimple()Returnstrueif this is representing a single value. -
isZero
public boolean isZero()Returnstrueif the range is 0. -
isInfinite
public boolean isInfinite()Returnstrueif the end value is infinity. -
start
public int start()Returns the start value. -
end
public int end()Returns the end value, orInteger.MAX_VALUEif the range is infinite. -
getMessage
Returns a string representation of the range, such as "from 3 to 5 times", or "3 times".- Parameters:
kind- The kind of thing the range is for, such as "time" or "argument"- Returns:
- The string representation
-
getRepresentation
Returns a string representation of the range, such as"{3, 5}"or"{3}". If the end value isShort.MAX_VALUE, it will be represented as"...".- Returns:
- The string representation
-
contains
public boolean contains(int value, boolean startInclusive, boolean endInclusive) Returnstrueif the given value is in the range.- Parameters:
value- The value to checkstartInclusive- Whether the start value is inclusiveendInclusive- Whether the end value is inclusive- Returns:
trueif the value is in the range
-
containsInclusive
public boolean containsInclusive(int value) Returnstrueif the given value is in the range, inclusive.- Parameters:
value- The value to check- Returns:
trueif the value is in the range
-
containsExclusive
public boolean containsExclusive(int value) Returnstrueif the given value is in the range, exclusive.- Parameters:
value- The value to check- Returns:
trueif the value is in the range
-
offset
Offsets both the start and end values by the given offset. The range will be infinite if it was infinite before.- Parameters:
offset- The offset to apply- Returns:
- The new range
-
iterator
Returns an iterator that iterates over the range. Both the start and end values are inclusive. -
iterator
Returns an iterator that iterates over the range.- Parameters:
startInclusive- Whether the start value is inclusiveendInclusive- Whether the end value is inclusive- Returns:
- The iterator
-
toArray
public int[] toArray()Returns an array of all the values in the range, inclusive.If the range is infinite, an
UnsupportedOperationExceptionwill be thrown.- Returns:
- The array
- Throws:
UnsupportedOperationException- if the range is infinite- See Also:
-
toArray
public int[] toArray(boolean startInclusive, boolean endInclusive) Returns an array of all the values in the range.If the range is infinite, an
The array will always contain at least one element.UnsupportedOperationExceptionwill be thrown.- Parameters:
startInclusive- Whether the start value is inclusiveendInclusive- Whether the end value is inclusive- Returns:
- The array
- Throws:
UnsupportedOperationException- if the range is infinite
-
toString
-