All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cosium.code.format.formatter.LineRanges Maven / Gradle / Ivy

package com.cosium.code.format.formatter;

import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet;
import java.util.Collections;
import java.util.Set;

/** @author Réda Housni Alaoui */
public class LineRanges {

  private static final LineRanges ALL =
      new LineRanges(TreeRangeSet.create(Collections.singleton(Range.all())));

  private final RangeSet rangeSet;

  private LineRanges(RangeSet rangeSet) {
    if (rangeSet.isEmpty()) {
      throw new IllegalArgumentException("There must be at least one range");
    }

    this.rangeSet = rangeSet;
  }

  public static LineRanges all() {
    return ALL;
  }

  public static LineRanges of(Set> ranges) {
    return new LineRanges(TreeRangeSet.create(ranges));
  }

  public static LineRanges singleton(Range range) {
    return new LineRanges(TreeRangeSet.create(Collections.singleton(range)));
  }

  public boolean isAll() {
    return this == ALL;
  }

  public RangeSet rangeSet() {
    return rangeSet;
  }

  public static LineRanges concat(LineRanges lineRanges1, LineRanges lineRanges2) {
    if (lineRanges1.isAll()) {
      return lineRanges1;
    }
    if (lineRanges2.isAll()) {
      return lineRanges2;
    }

    RangeSet newRangeSet = TreeRangeSet.create();
    newRangeSet.addAll(lineRanges1.rangeSet);
    newRangeSet.addAll(lineRanges2.rangeSet);

    return new LineRanges(newRangeSet);
  }

  @Override
  public String toString() {
    return rangeSet.toString();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy