IntRange extends Pair<Integer, Integer> { public IntRange select(final IntRange sub) { final int index1 = Math.min(Math.max(sub.v1, this.v1), this.v2); final int index2 = Math.min(Math.max(sub.v2, this.v1), this.v2); if (index1 == sub.v1 && index2 == sub.v2) { return sub; } return new IntRange(index1, index2); } IntRange(final Integer v1, final Integer v2); IntRange select(final IntRange sub); @Override String toString();  }
@Test public void testSelect() { final Object[][] testParameters = new Object[][] {{1, 5, 10, 15}, {-10, 1, 100, 101}, {-1, 0, 1, 2}, {-2, -1, 0, 1}}; for (final Object[] testParameter : testParameters) { final Integer first = Integer.valueOf((Integer) testParameter[0]); final Integer second = Integer.valueOf((Integer) testParameter[1]); final Integer third = Integer.valueOf((Integer) testParameter[2]); final Integer fourth = Integer.valueOf((Integer) testParameter[3]); final IntRange intRange1 = new IntRange(first, third); final IntRange intRange2 = new IntRange(second, fourth); final IntRange join = intRange1.select(intRange2); logger.debug("Range: {}", join); assertEquals(second, join.v1); assertEquals(third, join.v2); } }