synchronized void add ( Range range ) { if ( range . isEmpty ( ) ) { return ; } long startIndex = range . getStartIndex ( ) ; long endIndex = range . getEndIndex ( ) ; //make sure that there are no overlapping ranges SortedSet < Range > headSet = ranges . headSet ( range ) ; if ( headSet . size ( ) > 0 ) { Range previousRange = headSet . last ( ) ; LOG . debug ( "previousRange " + previousRange ) ; if ( startIndex < previousRange . getEndIndex ( ) ) { //previousRange overlaps this range //remove the previousRange if ( ranges . remove ( previousRange ) ) { indicesCount -= previousRange . getLength ( ) ; } //expand this range startIndex = previousRange . getStartIndex ( ) ; endIndex = endIndex >= previousRange . getEndIndex ( ) ? endIndex : previousRange . getEndIndex ( ) ; } } Iterator < Range > tailSetIt = ranges . tailSet ( range ) . iterator ( ) ; while ( tailSetIt . hasNext ( ) ) { Range nextRange = tailSetIt . next ( ) ; LOG . debug ( "nextRange " + nextRange + " startIndex:" + startIndex + " endIndex:" + endIndex ) ; if ( endIndex >= nextRange . getStartIndex ( ) ) { //nextRange overlaps this range //remove the nextRange tailSetIt . remove ( ) ; indicesCount -= nextRange . getLength ( ) ; if ( endIndex < nextRange . getEndIndex ( ) ) { //expand this range endIndex = nextRange . getEndIndex ( ) ; break ; } } else { break ; } } add ( startIndex , endIndex ) ; }
Add the range indices . It is ensured that the added range doesn t overlap the existing ranges . If it overlaps the existing overlapping ranges are removed and a single range having the superset of all the removed ranges and this range is added . If the range is of 0 length doesn t do anything .