public static IntervalSet intersect(List<IntervalSet> intervalSets) {
    if (intervalSets.isEmpty()) {
      return IntervalSet.NEVER;
    }
    if (intervalSets.size() == 1) {
      return intervalSets.get(0);
    }

    // at least 2 lists of intervals
    IntervalSet intersection = intervalSets.get(0);

    // scan entire list from the second interval
    for (int i = 1; i < intervalSets.size(); i++) {
      intersection = intersection.intersect(intervalSets.get(i));
    }
    return intersection;
  }
@Test
  public void intersect() {
    IntervalSet s1;
    IntervalSet s2;
    Set<Interval> s;

    s1 = new IntervalSet(Arrays.asList(Interval.between(1, 3), Interval.between(11, 13)));
    s = Sets.newHashSet(
        IntervalUtils.intersect(Arrays.asList(s1, IntervalSet.NEVER)).getIntervals());
    Assert.assertEquals(1, s.size());
    Assert.assertTrue(s.contains(Interval.NEVER));

    s1 = new IntervalSet(Arrays.asList(Interval.between(1, 3), Interval.between(11, 13)));
    s2 = new IntervalSet(Arrays.asList(Interval.between(22, 24), Interval.between(32, 34)));
    s = Sets.newHashSet(IntervalUtils.intersect(Arrays.asList(s1, s2)).getIntervals());
    Assert.assertEquals(1, s.size());
    Assert.assertTrue(s.contains(Interval.NEVER));

    s1 = new IntervalSet(Arrays.asList(Interval.between(1, 3), Interval.between(11, 13)));
    s2 = new IntervalSet(Arrays.asList(Interval.between(2, 4), Interval.between(12, 14)));
    s = Sets.newHashSet(IntervalUtils.intersect(Arrays.asList(s1, s2)).getIntervals());
    Assert.assertEquals(2, s.size());
    Assert
        .assertTrue(s.containsAll(Arrays.asList(Interval.between(2, 3), Interval.between(12, 13))));

    s1 = new IntervalSet(Arrays.asList(Interval.between(11, 13), Interval.between(1, 3)));
    s2 = new IntervalSet(Arrays.asList(Interval.between(12, 14), Interval.between(2, 4)));
    s = Sets.newHashSet(IntervalUtils.intersect(Arrays.asList(s1, s2)).getIntervals());
    Assert.assertEquals(2, s.size());
    Assert
        .assertTrue(s.containsAll(Arrays.asList(Interval.between(2, 3), Interval.between(12, 13))));

    s1 = new IntervalSet(
        Arrays.asList(Interval.between(11, 13), Interval.between(1, 3), Interval.between(21, 23)));
    s2 = new IntervalSet(
        Arrays.asList(Interval.between(12, 14), Interval.between(2, 4), Interval.between(31, 33)));
    s = Sets.newHashSet(IntervalUtils.intersect(Arrays.asList(s1, s2)).getIntervals());
    Assert.assertEquals(2, s.size());
    Assert
        .assertTrue(s.containsAll(Arrays.asList(Interval.between(2, 3), Interval.between(12, 13))));

    s1 = new IntervalSet(Arrays.asList(Interval.after(8), Interval.after(18)));
    s2 = new IntervalSet(Arrays.asList(Interval.before(10), Interval.before(2)));
    s = Sets.newHashSet(IntervalUtils.intersect(Arrays.asList(s1, s2)).getIntervals());
    Assert.assertEquals(1, s.size());
    Assert.assertTrue(s.containsAll(Arrays.asList(Interval.between(8, 10))));
  }