MCPcopy Create free account
hub / github.com/apache/groovy / NumberRange

Class NumberRange

src/main/java/groovy/lang/NumberRange.java:56–777  ·  view source on GitHub ↗

Represents an immutable list of Numbers from a value to a value with a particular step size. In general, it isn't recommended using a NumberRange as a key to a map. The range 0..3 is deemed to be equal to 0.0..3.0 but they have different hashCode values, so storing a value using one of these ranges

Source from the content-addressed store, hash-verified

54 * @since 2.5.0
55 */
56public class NumberRange extends AbstractList<Comparable> implements Range<Comparable>, Serializable {
57
58 /**
59 * The first value in the range.
60 */
61 @Serial private static final long serialVersionUID = 5107424833653948484L;
62 private final Comparable from;
63
64 /**
65 * The last value in the range.
66 */
67 private final Comparable to;
68
69 /**
70 * The step size in the range.
71 */
72 private final Number stepSize;
73
74 /**
75 * The cached size, or -1 if not yet computed
76 */
77 private int size = -1;
78
79 /**
80 * The cached hashCode (once calculated)
81 */
82 private Integer hashCodeCache = null;
83
84 /**
85 * <code>true</code> if the range counts backwards from <code>to</code> to <code>from</code>.
86 */
87 private final boolean reverse;
88
89 /**
90 * <code>true</code> if the range includes the lower bound.
91 */
92 private final boolean inclusiveLeft;
93
94 /**
95 * <code>true</code> if the range includes the upper bound.
96 */
97 private final boolean inclusiveRight;
98
99 /**
100 * Creates an inclusive {@link NumberRange} with step size 1.
101 * Creates a reversed range if <code>from</code> &lt; <code>to</code>.
102 *
103 * @param from the first value in the range
104 * @param to the last value in the range
105 */
106 public <T extends Number & Comparable, U extends Number & Comparable>
107 NumberRange(T from, U to) {
108 this(from, to, null, true, true);
109 }
110
111 /**
112 * Creates a new {@link NumberRange} with step size 1.
113 * Creates a reversed range if <code>from</code> &lt; <code>to</code>.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected