PortRange implements Range<Port> { @Override public boolean isInRange(Port candidate) { return Optional.ofNullable(candidate) .map(port -> port.greaterOrEqualsTo(from()) && port.lessOrEqualsTo(to())) .orElse(Boolean.FALSE); }  abstract Port from(); abstract Port to(); @Override boolean isInRange(Port candidate);  }
@Test public void isInRange() throws Exception { final PortRange portRange = ImmutablePortRange.builder().from(ImmutablePort.of(8000)).to(ImmutablePort.of(8002)).build(); Assertions.assertThat(portRange.isInRange(ImmutablePort.of(8000))).isTrue(); Assertions.assertThat(portRange.isInRange(ImmutablePort.of(8001))).isTrue(); Assertions.assertThat(portRange.isInRange(ImmutablePort.of(8002))).isTrue(); Assertions.assertThat(portRange.isInRange(ImmutablePort.of(8003))).isFalse(); Assertions.assertThat(portRange.isInRange(ImmutablePort.of(9000))).isFalse(); }