public static IPAddressSeqRange [ ] join ( IPAddressSeqRange ... ranges ) { int joinedCount = 0 ; Arrays . sort ( ranges , Address . ADDRESS_LOW_VALUE_COMPARATOR ) ; for ( int i = 0 ; i < ranges . length ; i ++ ) { IPAddressSeqRange range = ranges [ i ] ; if ( range == null ) { continue ; } for ( int j = i + 1 ; j < ranges . length ; j ++ ) { IPAddressSeqRange range2 = ranges [ j ] ; if ( range2 == null ) { continue ; } IPAddress upper = range . getUpper ( ) ; IPAddress lower = range2 . getLower ( ) ; if ( compareLowValues ( upper , lower ) >= 0 || upper . increment ( 1 ) . equals ( lower ) ) { //join them ranges [ i ] = range = range . create ( range . getLower ( ) , range2 . getUpper ( ) ) ; ranges [ j ] = null ; joinedCount ++ ; } else break ; } } if ( joinedCount == 0 ) { return ranges ; } IPAddressSeqRange joined [ ] = new IPAddressSeqRange [ ranges . length - joinedCount ] ; for ( int i = 0 , j = 0 ; i < ranges . length ; i ++ ) { IPAddressSeqRange range = ranges [ i ] ; if ( range == null ) { continue ; } joined [ j ++ ] = range ; if ( j >= joined . length ) { break ; } } return joined ; }
Joins the given ranges into the fewest number of ranges . If no joining can take place the original array is returned .