private List<CubeSegment> calculateToBeSegments(CubeInstance cube,CubeSegment newSegments){
  List<CubeSegment> tobe=Lists.newArrayList(cube.getSegments());
  if (newSegments != null && !tobe.contains(newSegments)) {
    tobe.add(newSegments);
  }
  if (tobe.size() == 0)   return tobe;
  Collections.sort(tobe);
  CubeSegment firstSeg=tobe.get(0);
  firstSeg.validate();
  for (int i=0, j=1; j < tobe.size(); ) {
    CubeSegment is=tobe.get(i);
    CubeSegment js=tobe.get(j);
    js.validate();
    if (!isNew(is) && !isReady(is)) {
      tobe.remove(i);
      continue;
    }
    if (!isNew(js) && !isReady(js)) {
      tobe.remove(j);
      continue;
    }
    if (is.getSourceOffsetStart() == js.getSourceOffsetStart()) {
      if (isReady(is) && isReady(js) || isNew(is) && isNew(js)) {
        if (is.getSourceOffsetEnd() <= js.getSourceOffsetEnd()) {
          tobe.remove(i);
        }
 else {
          tobe.remove(j);
        }
        continue;
      }
 else {
        if (isNew(is) && is.equals(newSegments)) {
          tobe.remove(j);
          continue;
        }
 else         if (js.equals(newSegments)) {
          tobe.remove(i);
          continue;
        }
      }
    }
    if (is.getSourceOffsetEnd() <= js.getSourceOffsetStart()) {
      i++;
      j++;
      continue;
    }
    if (is.equals(newSegments)) {
      tobe.remove(j);
      continue;
    }
 else {
      i++;
      j++;
      continue;
    }
  }
  return tobe;
}

private List < CubeSegment > calculateToBeSegments ( CubeInstance cube , CubeSegment newSegments ) { List < CubeSegment > tobe = Lists . newArrayList ( cube . getSegments ( ) ) ; if ( newSegments != null && ! tobe . contains ( newSegments ) ) { tobe . add ( newSegments ) ; } if ( tobe . size ( ) == 0 ) return tobe ; Collections . sort ( tobe ) ; CubeSegment firstSeg = tobe . get ( 0 ) ; firstSeg . validate ( ) ; for ( int i = 0 , j = 1 ; j < tobe . size ( ) ; ) { CubeSegment is = tobe . get ( i ) ; CubeSegment js = tobe . get ( j ) ; js . validate ( ) ; if ( ! isNew ( is ) && ! isReady ( is ) ) { tobe . remove ( i ) ; continue ; } if ( ! isNew ( js ) && ! isReady ( js ) ) { tobe . remove ( j ) ; continue ; } if ( is . getSourceOffsetStart ( ) == js . getSourceOffsetStart ( ) ) { if ( isReady ( is ) && isReady ( js ) || isNew ( is ) && isNew ( js ) ) { if ( is . getSourceOffsetEnd ( ) <= js . getSourceOffsetEnd ( ) ) { tobe . remove ( i ) ; } else { tobe . remove ( j ) ; } continue ; } else { if ( isNew ( is ) && is . equals ( newSegments ) ) { tobe . remove ( j ) ; continue ; } else if ( js . equals ( newSegments ) ) { tobe . remove ( i ) ; continue ; } } } if ( is . getSourceOffsetEnd ( ) <= js . getSourceOffsetStart ( ) ) { i ++ ; j ++ ; continue ; } if ( is . equals ( newSegments ) ) { tobe . remove ( j ) ; continue ; } else { i ++ ; j ++ ; continue ; } } return tobe ; }
smartly figure out the tobe segments once all new segments are built . - ensures no gap , no overlap - favors new segments over the old - favors big segments over the small
