NetUtils { public static Iterator<Integer> getPortRangeFromString(String rangeDefinition) throws NumberFormatException { final String[] ranges = rangeDefinition.trim().split(","); List<Iterator<Integer>> iterators = new ArrayList<>(ranges.length); for(String rawRange: ranges) { Iterator<Integer> rangeIterator = null; String range = rawRange.trim(); int dashIdx = range.indexOf('-'); if (dashIdx == -1) { rangeIterator = Iterators.singletonIterator(Integer.valueOf(range)); } else { final int start = Integer.valueOf(range.substring(0, dashIdx)); final int end = Integer.valueOf(range.substring(dashIdx+1, range.length())); rangeIterator = new Iterator<Integer>() { int i = start; @Override public boolean hasNext() { return i <= end; } @Override public Integer next() { return i++; } @Override public void remove() { throw new UnsupportedOperationException("Remove not supported"); } }; } iterators.add(rangeIterator); } return Iterators.concat(iterators.iterator()); }  static String getHostnameFromFQDN(String fqdn); static URL getCorrectHostnamePort(String hostPort); static int getAvailablePort(); static String ipAddressToUrlString(InetAddress address); static String ipAddressAndPortToUrlString(InetAddress address, int port); static String socketAddressToUrlString(InetSocketAddress address); static String hostAndPortToUrlString(String host, int port); static Iterator<Integer> getPortRangeFromString(String rangeDefinition); static ServerSocket createSocketFromPorts(Iterator<Integer> portsIterator, SocketFactory factory);  }
@Test public void testFreePortRangeUtility() { String rangeDefinition = "50000-50050, 50100-50200,51234 "; Iterator<Integer> portsIter = NetUtils.getPortRangeFromString(rangeDefinition); Set<Integer> ports = new HashSet<>(); while(portsIter.hasNext()) { Assert.assertTrue("Duplicate element", ports.add(portsIter.next())); } Assert.assertEquals(51+101+1, ports.size()); Assert.assertThat(ports, hasItems(50000, 50001, 50002, 50050)); Assert.assertThat(ports, hasItems(50100, 50101, 50110, 50200, 51234)); Assert.assertThat(ports, not(hasItems(50051, 50052, 1337, 50201, 49999, 50099))); portsIter = NetUtils.getPortRangeFromString(" 51234"); Assert.assertTrue(portsIter.hasNext()); Assert.assertEquals(51234, (int)portsIter.next()); Assert.assertFalse(portsIter.hasNext()); portsIter = NetUtils.getPortRangeFromString("5,1,2,3,4"); Assert.assertTrue(portsIter.hasNext()); Assert.assertEquals(5, (int)portsIter.next()); Assert.assertEquals(1, (int)portsIter.next()); Assert.assertEquals(2, (int)portsIter.next()); Assert.assertEquals(3, (int)portsIter.next()); Assert.assertEquals(4, (int)portsIter.next()); Assert.assertFalse(portsIter.hasNext()); Throwable error = null; try { NetUtils.getPortRangeFromString("localhost"); } catch(Throwable t) { error = t; } Assert.assertTrue(error instanceof NumberFormatException); error = null; try { NetUtils.getPortRangeFromString("5-"); } catch(Throwable t) { error = t; } Assert.assertTrue(error instanceof NumberFormatException); error = null; try { NetUtils.getPortRangeFromString("-5"); } catch(Throwable t) { error = t; } Assert.assertTrue(error instanceof NumberFormatException); error = null; try { NetUtils.getPortRangeFromString(",5"); } catch(Throwable t) { error = t; } Assert.assertTrue(error instanceof NumberFormatException); error = null; }