private boolean addAtKey(
      NavigableMap<Interval, TimelineEntry> timeline,
      Interval key,
      TimelineEntry entry
  )
  {
    boolean retVal = false;
    Interval currKey = key;
    Interval entryInterval = entry.getTrueInterval();

    if (!currKey.overlaps(entryInterval)) {
      return false;
    }

    while (entryInterval != null && currKey != null && currKey.overlaps(entryInterval)) {
      Interval nextKey = timeline.higherKey(currKey);

      int versionCompare = versionComparator.compare(
          entry.getVersion(),
          timeline.get(currKey).getVersion()
      );

      if (versionCompare < 0) {
        if (currKey.contains(entryInterval)) {
          return true;
        } else if (currKey.getStart().isBefore(entryInterval.getStart())) {
          entryInterval = new Interval(currKey.getEnd(), entryInterval.getEnd());
        } else {
          addIntervalToTimeline(new Interval(entryInterval.getStart(), currKey.getStart()), entry, timeline);

          if (entryInterval.getEnd().isAfter(currKey.getEnd())) {
            entryInterval = new Interval(currKey.getEnd(), entryInterval.getEnd());
          } else {
            entryInterval = null; // discard this entry
          }
        }
      } else if (versionCompare > 0) {
        TimelineEntry oldEntry = timeline.remove(currKey);

        if (currKey.contains(entryInterval)) {
          addIntervalToTimeline(new Interval(currKey.getStart(), entryInterval.getStart()), oldEntry, timeline);
          addIntervalToTimeline(new Interval(entryInterval.getEnd(), currKey.getEnd()), oldEntry, timeline);
          addIntervalToTimeline(entryInterval, entry, timeline);

          return true;
        } else if (currKey.getStart().isBefore(entryInterval.getStart())) {
          addIntervalToTimeline(new Interval(currKey.getStart(), entryInterval.getStart()), oldEntry, timeline);
        } else if (entryInterval.getEnd().isBefore(currKey.getEnd())) {
          addIntervalToTimeline(new Interval(entryInterval.getEnd(), currKey.getEnd()), oldEntry, timeline);
        }
      } else {
        if (timeline.get(currKey).equals(entry)) {
          // This occurs when restoring segments
          timeline.remove(currKey);
        } else {
          throw new UOE(
              "Cannot add overlapping segments [%s and %s] with the same version [%s]",
              currKey,
              entryInterval,
              entry.getVersion()
          );
        }
      }

      currKey = nextKey;
      retVal = true;
    }

    addIntervalToTimeline(entryInterval, entry, timeline);

    return retVal;
  }
private boolean addAtKey ( NavigableMap < Interval , TimelineEntry > timeline , Interval key , TimelineEntry entry ) { boolean retVal = false ; Interval currKey = key ; Interval entryInterval = entry . getTrueInterval ( ) ; if ( ! currKey . overlaps ( entryInterval ) ) { return false ; } while ( entryInterval != null && currKey != null && currKey . overlaps ( entryInterval ) ) { Interval nextKey = timeline . higherKey ( currKey ) ; int versionCompare = versionComparator . compare ( entry . getVersion ( ) , timeline . get ( currKey ) . getVersion ( ) ) ; if ( versionCompare < 0 ) { if ( currKey . contains ( entryInterval ) ) { return true ; } else if ( currKey . getStart ( ) . isBefore ( entryInterval . getStart ( ) ) ) { entryInterval = new Interval ( currKey . getEnd ( ) , entryInterval . getEnd ( ) ) ; } else { addIntervalToTimeline ( new Interval ( entryInterval . getStart ( ) , currKey . getStart ( ) ) , entry , timeline ) ; if ( entryInterval . getEnd ( ) . isAfter ( currKey . getEnd ( ) ) ) { entryInterval = new Interval ( currKey . getEnd ( ) , entryInterval . getEnd ( ) ) ; } else { entryInterval = null ; } } } else if ( versionCompare > 0 ) { TimelineEntry oldEntry = timeline . remove ( currKey ) ; if ( currKey . contains ( entryInterval ) ) { addIntervalToTimeline ( new Interval ( currKey . getStart ( ) , entryInterval . getStart ( ) ) , oldEntry , timeline ) ; addIntervalToTimeline ( new Interval ( entryInterval . getEnd ( ) , currKey . getEnd ( ) ) , oldEntry , timeline ) ; addIntervalToTimeline ( entryInterval , entry , timeline ) ; return true ; } else if ( currKey . getStart ( ) . isBefore ( entryInterval . getStart ( ) ) ) { addIntervalToTimeline ( new Interval ( currKey . getStart ( ) , entryInterval . getStart ( ) ) , oldEntry , timeline ) ; } else if ( entryInterval . getEnd ( ) . isBefore ( currKey . getEnd ( ) ) ) { addIntervalToTimeline ( new Interval ( entryInterval . getEnd ( ) , currKey . getEnd ( ) ) , oldEntry , timeline ) ; } } else { if ( timeline . get ( currKey ) . equals ( entry ) ) { timeline . remove ( currKey ) ; } else { throw new UOE ( "Cannot add overlapping segments [%s and %s] with the same version [%s]" , currKey , entryInterval , entry . getVersion ( ) ) ; } } currKey = nextKey ; retVal = true ; } addIntervalToTimeline ( entryInterval , entry , timeline ) ; return retVal ; }
@param timeline
@param key
@param entry

@return boolean flag indicating whether or not we inserted or discarded something