public void put(Range<R> range, K value) {
    RangeMap<R, List<K>> subRangeMap = map.subRangeMap(range);
    Map<Range<R>, List<K>> submap = subRangeMap.asMapOfRanges();
    if (submap.isEmpty()) {
      ArrayList<K> list = new ArrayList<K>();
      list.add(value);
      map.put(range, list);
    } else {

      RangeSet<R> notCoveredSpan = TreeRangeSet.create();
      notCoveredSpan.add(range);

      List<Pair<Range<R>, List<K>>> newElements = new ArrayList<Pair<Range<R>, List<K>>>();
      for (Entry<Range<R>, List<K>> mapEntry : submap.entrySet()) {
        List<K> newValue = new ArrayList<K>(mapEntry.getValue());
        newValue.add(value);

        newElements.add(new ImmutablePair<Range<R>, List<K>>(mapEntry.getKey(), newValue));
        notCoveredSpan.remove(mapEntry.getKey());
      }

      for (Pair<Range<R>, List<K>> el : newElements) {
        map.put(el.getLeft(), el.getRight());
      }

      if (!notCoveredSpan.isEmpty()) {
        for (Range<R> notYetCoveredSpan : notCoveredSpan.asRanges()) {
          ArrayList<K> list = new ArrayList<K>();
          list.add(value);
          map.put(notYetCoveredSpan, list);
        }
      }
    }
  }
 private static RangeSet<Integer> characterRangesToTokenRanges(
     JavaInput javaInput, List<Range<Integer>> characterRanges) throws FormatterException {
   RangeSet<Integer> tokenRangeSet = TreeRangeSet.create();
   for (Range<Integer> characterRange0 : characterRanges) {
     Range<Integer> characterRange = characterRange0.canonical(DiscreteDomain.integers());
     tokenRangeSet.add(
         javaInput.characterRangeToTokenRange(
             characterRange.lowerEndpoint(),
             characterRange.upperEndpoint() - characterRange.lowerEndpoint()));
   }
   return tokenRangeSet;
 }
 /**
  * Format an input string (a Java compilation unit) into an output string.
  *
  * @param input the input string
  * @return the output string
  * @throws FormatterException if the input string cannot be parsed
  */
 public String formatSource(String input) throws FormatterException {
   JavaInput javaInput = new JavaInput(STDIN_FILENAME, input);
   JavaOutput javaOutput = new JavaOutput(javaInput, new JavaCommentsHelper());
   List<FormatterDiagnostic> errors = new ArrayList<>();
   format(javaInput, javaOutput, MAX_WIDTH, errors, 1);
   if (!errors.isEmpty()) {
     throw new FormatterException(errors);
   }
   StringBuilder result = new StringBuilder(input.length());
   RangeSet<Integer> lineRangeSet = TreeRangeSet.create();
   lineRangeSet.add(Range.<Integer>all());
   try {
     javaOutput.writeMerged(result, lineRangeSet);
   } catch (IOException ignored) {
     throw new AssertionError("IOException impossible for StringWriter");
   }
   return result.toString();
 }
 /** Strips the given ranges from {@code text}. */
 public static final String stripRanges(String text, RangeSet<Integer> rangesToRemove) {
   StringBuilder contentBuilder = new StringBuilder(text);
   // Delete the ranges.  Go from last to first to avoid having to
   // compute the offsets.
   List<Range<Integer>> ranges = Lists.newArrayList(rangesToRemove.asRanges());
   Collections.reverse(ranges);
   for (Range<Integer> range : ranges) {
     contentBuilder.delete(range.lowerEndpoint(), range.upperEndpoint());
   }
   return contentBuilder.toString();
 }
  @Test
  public void whenCreateRangeSet_thenCreated() {
    final RangeSet<Integer> rangeSet = TreeRangeSet.create();
    rangeSet.add(Range.closed(1, 10));
    rangeSet.add(Range.closed(12, 15));

    assertEquals(2, rangeSet.asRanges().size());

    rangeSet.add(Range.closed(10, 12));
    assertTrue(rangeSet.encloses(Range.closed(1, 15)));
    assertEquals(1, rangeSet.asRanges().size());
  }