of static method

Interval of(
  1. int a,
  2. int b
)

Interval objects are used readonly so share all with the same single value a==b up to some max size. Use an array as a perfect hash. Return shared object for 0..INTERVAL_POOL_MAX_VALUE or a new Interval object with a..a in it. On Java.g4, 218623 IntervalSets have a..a (set with 1 element).

Implementation

static Interval of(int a, int b) {
  // cache just a..a
  if (a != b || a < 0 || a > INTERVAL_POOL_MAX_VALUE) {
    return Interval(a, b);
  }
  if (cache[a] == null) {
    cache[a] = Interval(a, a);
  }
  return cache[a]!;
}