public Interval intersect(Interval other) {
    return between(Math.max(mStartMs, other.mStartMs), Math.min(mEndMs, other.mEndMs));
  }
@Test
  public void intersect() {
    for (int i = 0; i < 100; i++) {
      List<Long> sortedTimes =
          ThreadLocalRandom.current().longs(0, Long.MAX_VALUE).limit(4).sorted().boxed()
              .collect(Collectors.toList());
      // the intersection does exist
      Interval i1 = Interval.between(sortedTimes.get(0), sortedTimes.get(2));
      Interval i2 = Interval.between(sortedTimes.get(1), sortedTimes.get(3));

      Interval intersect = i1.intersect(i2);
      Assert.assertTrue(intersect.isValid());
      Assert.assertEquals(sortedTimes.get(1).longValue(), intersect.getStartMs());
      Assert.assertEquals(sortedTimes.get(2).longValue(), intersect.getEndMs());
      intersect = i2.intersect(i1);
      Assert.assertTrue(intersect.isValid());
      Assert.assertEquals(sortedTimes.get(1).longValue(), intersect.getStartMs());
      Assert.assertEquals(sortedTimes.get(2).longValue(), intersect.getEndMs());

      // the intersection does not exist
      i1 = Interval.between(sortedTimes.get(0), sortedTimes.get(1));
      i2 = Interval.between(sortedTimes.get(2), sortedTimes.get(3));

      intersect = i1.intersect(i2);
      Assert.assertFalse(intersect.isValid());
      intersect = i2.intersect(i1);
      Assert.assertFalse(intersect.isValid());

      // the intervals share the same value, intersection does not exist
      i1 = Interval.between(sortedTimes.get(0), sortedTimes.get(1));
      i2 = Interval.between(sortedTimes.get(1), sortedTimes.get(3));

      intersect = i1.intersect(i2);
      Assert.assertFalse(intersect.isValid());
      intersect = i2.intersect(i1);
      Assert.assertFalse(intersect.isValid());
    }
  }