public static Iterator < Integer > getPortRangeFromString ( String rangeDefinition ) throws NumberFormatException { final String [ ] ranges = rangeDefinition . trim ( ) . split ( "," ) ; UnionIterator < Integer > iterators = new UnionIterator <> ( ) ; for ( String rawRange : ranges ) { Iterator < Integer > rangeIterator ; String range = rawRange . trim ( ) ; int dashIdx = range . indexOf ( ' ' ) ; if ( dashIdx == - 1 ) { // only one port in range: final int port = Integer . valueOf ( range ) ; if ( port < 0 || port > 65535 ) { throw new IllegalConfigurationException ( "Invalid port configuration. Port must be between 0" + "and 65535, but was " + port + "." ) ; } rangeIterator = Collections . singleton ( Integer . valueOf ( range ) ) . iterator ( ) ; } else { // evaluate range final int start = Integer . valueOf ( range . substring ( 0 , dashIdx ) ) ; if ( start < 0 || start > 65535 ) { throw new IllegalConfigurationException ( "Invalid port configuration. Port must be between 0" + "and 65535, but was " + start + "." ) ; } final int end = Integer . valueOf ( range . substring ( dashIdx + 1 , range . length ( ) ) ) ; if ( end < 0 || end > 65535 ) { throw new IllegalConfigurationException ( "Invalid port configuration. Port must be between 0" + "and 65535, but was " + end + "." ) ; } 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 ; }
Returns an iterator over available ports defined by the range definition .