code: public static ObjectSequence<TimeSeriesCollection> fixSequence(ObjectSequence<TimeSeriesCollection> seq) {
          seq = seq.sort();  // Does nothing if already sorted.
  
          if (!seq.isDistinct() && !seq.isEmpty()) {
              List<ObjectSequence<TimeSeriesCollection>> toConcat = new ArrayList<>();
              int lastGoodIdx = 0;
              int curIdx = 0;
  
              while (curIdx < seq.size()) {
                  final TimeSeriesCollection curElem = seq.get(curIdx);
  
                  // Find first item with different timestamp.
                  int nextIdx = curIdx + 1;
                  while (nextIdx < seq.size()
                          && curElem.getTimestamp().equals(seq.get(nextIdx).getTimestamp())) {
                      ++nextIdx;
                  }
  
                  // If more than 1 adjecent element share timestamp, merge them together.
                  if (curIdx + 1 < nextIdx) {
                      toConcat.add(seq.limit(curIdx).skip(lastGoodIdx));
  
                      TimeSeriesCollection replacement = new LazyMergedTSC(seq.limit(nextIdx).skip(curIdx));
                      toConcat.add(ObjectSequence.of(true, true, true, replacement));
                      lastGoodIdx = curIdx = nextIdx;
                  } else {
                      // Advance curIdx.
                      ++curIdx;
                  }
              }
  
              if (lastGoodIdx < curIdx)
                  toConcat.add(seq.skip(lastGoodIdx));
              seq = ObjectSequence.concat(toConcat, true, true);
          }
  
          return seq;
      }
docs: Fix a sequence to contain only distinct, sorted elements.