com.google.common.collect.RangeMap Java Examples

The following examples show how to use com.google.common.collect.RangeMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RemoveUnusedImports.java    From google-java-format with Apache License 2.0 7 votes vote down vote up
/** Construct replacements to fix unused imports. */
private static RangeMap<Integer, String> buildReplacements(
    String contents,
    JCCompilationUnit unit,
    Set<String> usedNames,
    Multimap<String, Range<Integer>> usedInJavadoc) {
  RangeMap<Integer, String> replacements = TreeRangeMap.create();
  for (JCImport importTree : unit.getImports()) {
    String simpleName = getSimpleName(importTree);
    if (!isUnused(unit, usedNames, usedInJavadoc, importTree, simpleName)) {
      continue;
    }
    // delete the import
    int endPosition = importTree.getEndPosition(unit.endPositions);
    endPosition = Math.max(CharMatcher.isNot(' ').indexIn(contents, endPosition), endPosition);
    String sep = Newlines.guessLineSeparator(contents);
    if (endPosition + sep.length() < contents.length()
        && contents.subSequence(endPosition, endPosition + sep.length()).toString().equals(sep)) {
      endPosition += sep.length();
    }
    replacements.put(Range.closedOpen(importTree.getStartPosition(), endPosition), "");
  }
  return replacements;
}
 
Example #2
Source File: BankCharges.java    From levelup-java-exercises with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

		// define rages for checks
		RangeMap<Integer, Double> checkFee = TreeRangeMap.create();
		checkFee.put(Range.closed(0, 19), .1);
		checkFee.put(Range.closed(20, 39), .8);
		checkFee.put(Range.closed(40, 59), .6);
		checkFee.put(Range.closed(60, Integer.MAX_VALUE), .4);

		// Create a Scanner object for keyboard input.
		Scanner keyboard = new Scanner(System.in);

		// Get the number of checks written.
		System.out.print("Enter the number of checks written " + "this month: ");
		int numChecks = keyboard.nextInt();

		//close scanner
		keyboard.close();

		// calculate total fee
		double total = BASE_FEE + (checkFee.get(numChecks) * numChecks); 
		
		// Display the total bank fees.
		System.out.printf("The total fees are $%.2f\n", total);
	}
 
Example #3
Source File: RemoveUnusedImports.java    From google-java-format with Apache License 2.0 6 votes vote down vote up
/** Applies the replacements to the given source, and re-format any edited javadoc. */
private static String applyReplacements(String source, RangeMap<Integer, String> replacements) {
  // save non-empty fixed ranges for reformatting after fixes are applied
  RangeSet<Integer> fixedRanges = TreeRangeSet.create();

  // Apply the fixes in increasing order, adjusting ranges to account for
  // earlier fixes that change the length of the source. The output ranges are
  // needed so we can reformat fixed regions, otherwise the fixes could just
  // be applied in descending order without adjusting offsets.
  StringBuilder sb = new StringBuilder(source);
  int offset = 0;
  for (Map.Entry<Range<Integer>, String> replacement : replacements.asMapOfRanges().entrySet()) {
    Range<Integer> range = replacement.getKey();
    String replaceWith = replacement.getValue();
    int start = offset + range.lowerEndpoint();
    int end = offset + range.upperEndpoint();
    sb.replace(start, end, replaceWith);
    if (!replaceWith.isEmpty()) {
      fixedRanges.add(Range.closedOpen(start, end));
    }
    offset += replaceWith.length() - (range.upperEndpoint() - range.lowerEndpoint());
  }
  return sb.toString();
}
 
Example #4
Source File: SomaticGVCFBlockCombiner.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create {@link HomRefBlock}s which will collectively accept variants of any genotype quality
 *
 * Each individual block covers a band of tumor LODs with the splits between bands occurring at values in {@code gqPartitions}.
 * There will be {@code gqPartitions.size() +1} bands produced
 *
 * @param gqPartitions proposed TLOD partitions as Doubles in LOD-space
 * @return a list of HomRefBlocks accepting bands of genotypes qualities split at the points specified in gqPartitions
 */
@Override
@VisibleForTesting
RangeMap<Integer,Range<Integer>> parsePartitions(final List<Number> gqPartitions) {
    partitionPrecision = calculatePartitionPrecision(gqPartitions);
    Utils.nonEmpty(gqPartitions);
    Utils.containsNoNull(gqPartitions, "The list of TLOD partitions contains a null integer");
    final RangeMap<Integer, Range<Integer>> result = TreeRangeMap.create();
    int lastThreshold = Integer.MIN_VALUE;
    for (final Number num : gqPartitions) {
        final double value = num.doubleValue();
        final int intThreshold = convertLODtoInt(value, partitionPrecision);
        result.put(Range.closedOpen(lastThreshold, intThreshold), Range.closedOpen(lastThreshold, intThreshold));
        lastThreshold = intThreshold;
    }
    result.put(Range.closedOpen(lastThreshold, Integer.MAX_VALUE), Range.closedOpen(lastThreshold, Integer.MAX_VALUE));
    return result;
}
 
Example #5
Source File: GuavaRangeMapUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenRangeMap_whenSubRangeMapIsCalled_returnsSubRangeSucessfully() {
    final RangeMap<Integer, String> experienceRangeDesignationMap = TreeRangeMap.create();

    experienceRangeDesignationMap.put(Range.closed(0, 2), "Associate");
    experienceRangeDesignationMap.put(Range.closed(3, 5), "Senior Associate");
    experienceRangeDesignationMap.put(Range.closed(6, 8), "Vice President");
    experienceRangeDesignationMap.put(Range.closed(8, 15), "Executive Director");
    experienceRangeDesignationMap.put(Range.closed(16, 30), "Managing Director");
    final RangeMap<Integer, String> experiencedSubRangeDesignationMap = experienceRangeDesignationMap.subRangeMap(Range.closed(4, 14));

    assertNull(experiencedSubRangeDesignationMap.get(3));
    assertTrue(experiencedSubRangeDesignationMap.asMapOfRanges().values()
        .containsAll(Arrays.asList("Executive Director", "Vice President", "Executive Director")));
    
}
 
Example #6
Source File: RuntimeColorMap.java    From tcl-regex-java with Apache License 2.0 6 votes vote down vote up
private void computeBmp(RangeMap<Integer, Short> fullMap) {
    for (Map.Entry<Range<Integer>, Short> me : fullMap.asMapOfRanges().entrySet()) {
        Range<Integer> range = me.getKey();
        int min = range.lowerEndpoint();
        if (range.lowerBoundType() == BoundType.OPEN) {
            min++;
        }
        if (min < Character.MAX_VALUE) {
            int rmax = range.upperEndpoint();
            if (range.upperBoundType() == BoundType.OPEN) {
                rmax--;
            }
            int max = Math.min(Character.MAX_VALUE, rmax);
            for (int x = min; x <= max; x++) {
                this.bmpMap[x] = me.getValue();
            }
        }
    }
}
 
Example #7
Source File: GuavaRangeMapUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenRangeMap_whenRemoveRangeIsCalled_removesSucessfully() {
    final RangeMap<Integer, String> experienceRangeDesignationMap = TreeRangeMap.create();

    experienceRangeDesignationMap.put(Range.closed(0, 2), "Associate");
    experienceRangeDesignationMap.put(Range.closed(3, 5), "Senior Associate");
    experienceRangeDesignationMap.put(Range.closed(6, 8), "Vice President");
    experienceRangeDesignationMap.put(Range.closed(9, 15), "Executive Director");
    experienceRangeDesignationMap.put(Range.closed(16, 30), "Managing Director");
    experienceRangeDesignationMap.remove(Range.closed(8, 15));
    experienceRangeDesignationMap.remove(Range.closed(20, 26));

    assertNull(experienceRangeDesignationMap.get(9));
    assertEquals("Managing Director", experienceRangeDesignationMap.get(16));
    assertEquals("Managing Director", experienceRangeDesignationMap.get(30));
    assertNull(experienceRangeDesignationMap.get(25));
}
 
Example #8
Source File: DiscretizationUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private RangeMap<Double, Object> parseDiscretize(Discretize discretize){
	RangeMap<Double, Object> result = TreeRangeMap.create();

	List<DiscretizeBin> discretizeBins = discretize.getDiscretizeBins();
	for(DiscretizeBin discretizeBin : discretizeBins){
		Interval interval = discretizeBin.getInterval();
		if(interval == null){
			throw new MissingElementException(discretizeBin, PMMLElements.DISCRETIZEBIN_INTERVAL);
		}

		Range<Double> range = toRange(interval);

		Object binValue = discretizeBin.getBinValue();
		if(binValue == null){
			throw new MissingAttributeException(discretizeBin, PMMLAttributes.DISCRETIZEBIN_BINVALUE);
		}

		result.put(range, binValue);
	}

	return result;
}
 
Example #9
Source File: DescriptionBasedDiff.java    From Refaster with Apache License 2.0 5 votes vote down vote up
private void addReplacement(Replacement replacement) {
  checkNotNull(replacement);
  Range<Integer> range = Range.closedOpen(replacement.startPosition, replacement.endPosition);

  RangeMap<Integer, Replacement> overlaps = replacements.subRangeMap(range);
  checkArgument(overlaps.asMapOfRanges().isEmpty(), "Replacement %s overlaps with %s",
      replacement, overlaps);

  replacements.put(range, replacement);
}
 
Example #10
Source File: RangeMapExample.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void google_guava_range_map_example () {

	RangeMap<Integer, String> gradeScale = TreeRangeMap.create();
	gradeScale.put(Range.closed(0, 60), "F");
	gradeScale.put(Range.closed(61, 70), "D");
	gradeScale.put(Range.closed(71, 80), "C");
	gradeScale.put(Range.closed(81, 90), "B");
	gradeScale.put(Range.closed(91, 100), "A");
	
	String grade = gradeScale.get(77);
	
	assertEquals("C", grade);
}
 
Example #11
Source File: GuavaRangeMapUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenImmutableRangeMap_whenQueryWithinRange_returnsSucessfully() {
    final RangeMap<Integer, String> experienceRangeDesignationMap = ImmutableRangeMap.<Integer, String> builder()
 .put(Range.closed(0, 2), "Associate")
 .put(Range.closed(3, 5), "Senior Associate")
 .put(Range.closed(6, 8), "Vice President")
        .put(Range.closed(9, 15), "Executive Director")
 .put(Range.closed(16, 30), "Managing Director").build();

    assertEquals("Vice President", experienceRangeDesignationMap.get(6));
    assertEquals("Executive Director", experienceRangeDesignationMap.get(15));
}
 
Example #12
Source File: GuavaRangeMapUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenRangeMap_whenGetEntryIsCalled_returnsEntrySucessfully() {
    final RangeMap<Integer, String> experienceRangeDesignationMap = TreeRangeMap.create();

    experienceRangeDesignationMap.put(Range.closed(0, 2), "Associate");
    experienceRangeDesignationMap.put(Range.closed(3, 5), "Senior Associate");
    experienceRangeDesignationMap.put(Range.closed(6, 8), "Vice President");
    experienceRangeDesignationMap.put(Range.closed(9, 15), "Executive Director");
    experienceRangeDesignationMap.put(Range.closed(20, 30), "Managing Director");
    final Map.Entry<Range<Integer>, String> experiencEntry = experienceRangeDesignationMap.getEntry(10);

    assertEquals(Range.closed(9, 15), experiencEntry.getKey());
    assertEquals("Executive Director", experiencEntry.getValue());
}
 
Example #13
Source File: GuavaRangeMapUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenRangeMap_whenSpanIsCalled_returnsSucessfully() {
    final RangeMap<Integer, String> experienceRangeDesignationMap = TreeRangeMap.create();

    experienceRangeDesignationMap.put(Range.closed(0, 2), "Associate");
    experienceRangeDesignationMap.put(Range.closed(3, 5), "Senior Associate");
    experienceRangeDesignationMap.put(Range.closed(6, 8), "Vice President");
    experienceRangeDesignationMap.put(Range.closed(9, 15), "Executive Director");
    experienceRangeDesignationMap.put(Range.closed(16, 30), "Managing Director");
    final Range<Integer> experienceSpan = experienceRangeDesignationMap.span();

    assertEquals(0, experienceSpan.lowerEndpoint().intValue());
    assertEquals(30, experienceSpan.upperEndpoint().intValue());
}
 
Example #14
Source File: GuavaRangeMapUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenRangeMap_whenQueryOutsideRange_returnsNull() {
    final RangeMap<Integer, String> experienceRangeDesignationMap = TreeRangeMap.create();

    experienceRangeDesignationMap.put(Range.closed(0, 2), "Associate");
    experienceRangeDesignationMap.put(Range.closed(3, 5), "Senior Associate");
    experienceRangeDesignationMap.put(Range.closed(6, 8), "Vice President");
    experienceRangeDesignationMap.put(Range.closed(9, 15), "Executive Director");
    experienceRangeDesignationMap.put(Range.closed(16, 30), "Managing Director");

    assertNull(experienceRangeDesignationMap.get(31));
}
 
Example #15
Source File: GuavaRangeMapUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenRangeMap_whenQueryWithinRange_returnsSucessfully() {
    final RangeMap<Integer, String> experienceRangeDesignationMap = TreeRangeMap.create();

    experienceRangeDesignationMap.put(Range.closed(0, 2), "Associate");
    experienceRangeDesignationMap.put(Range.closed(3, 5), "Senior Associate");
    experienceRangeDesignationMap.put(Range.closed(6, 8), "Vice President");
    experienceRangeDesignationMap.put(Range.closed(9, 15), "Executive Director");
    experienceRangeDesignationMap.put(Range.closed(16, 30), "Managing Director");

    assertEquals("Vice President", experienceRangeDesignationMap.get(6));
    assertEquals("Executive Director", experienceRangeDesignationMap.get(15));
}
 
Example #16
Source File: Renewable.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
MaterialData chooseShuffledMaterial() {
  ImmutableRangeMap.Builder<Double, MaterialData> weightsBuilder = ImmutableRangeMap.builder();
  double sum = 0d;
  for (MaterialData material : shuffleableMaterialDeficit.materials()) {
    double weight = shuffleableMaterialDeficit.get(material);
    if (weight > 0) {
      weightsBuilder.put(Range.closedOpen(sum, sum + weight), material);
      sum += weight;
    }
  }
  RangeMap<Double, MaterialData> weights = weightsBuilder.build();
  return weights.get(match.getRandom().nextDouble() * sum);
}
 
Example #17
Source File: GVCFBlockCombiner.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Create {@link HomRefBlock}s which will collectively accept variants of any genotype quality
 *
 * Each individual block covers a band of genotype qualities with the splits between bands occurring at values in {@code gqPartitions}.
 * There will be {@code gqPartitions.size() +1} bands produced covering the entire possible range of genotype qualities from 0 to {@link VCFConstants#MAX_GENOTYPE_QUAL}.
 *
 * Note that this has to return a RangeMap with concrete types because Numbers aren't Comparable
 *
 * @param gqPartitions proposed GQ partitions
 * @return a list of HomRefBlocks accepting bands of genotypes qualities split at the points specified in gqPartitions
 */
@VisibleForTesting
RangeMap<Integer,Range<Integer>> parsePartitions(final List<Number> gqPartitions) {
    Utils.nonEmpty(gqPartitions);
    Utils.containsNoNull(gqPartitions, "The list of GQ partitions contains a null integer");
    final RangeMap<Integer, Range<Integer>> result = TreeRangeMap.create();
    int lastThreshold = 0;
    for (final Number num : gqPartitions) {
        final int value = num.intValue();
        if (value < 0) {
            throw new IllegalArgumentException("The list of GQ partitions contains a non-positive integer.");
        } else if (value > MAX_GENOTYPE_QUAL + 1) {
            throw new IllegalArgumentException(String.format("The value %d in the list of GQ partitions is greater than VCFConstants.MAX_GENOTYPE_QUAL + 1 = %d.", value, MAX_GENOTYPE_QUAL + 1));
        } else if (value < lastThreshold) {
            throw new IllegalArgumentException(String.format("The list of GQ partitions is out of order. Previous value is %d but the next is %d.", lastThreshold, value));
        } else if (value == lastThreshold) {
            throw new IllegalArgumentException(String.format("The value %d appears more than once in the list of GQ partitions.", value));
        }

        result.put(Range.closedOpen(lastThreshold, value), Range.closedOpen(lastThreshold, value));
        lastThreshold = value;
    }

    if (lastThreshold <= MAX_GENOTYPE_QUAL) {
        result.put(Range.closedOpen(lastThreshold, MAX_GENOTYPE_QUAL + 1), Range.closedOpen(lastThreshold,MAX_GENOTYPE_QUAL + 1));
    }

    return result;
}
 
Example #18
Source File: Renewable.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
MaterialData chooseShuffledMaterial() {
    ImmutableRangeMap.Builder<Double, MaterialData> weightsBuilder = ImmutableRangeMap.builder();
    double sum = 0d;
    for(MaterialData material : shuffleableMaterialDeficit.materials()) {
        double weight = shuffleableMaterialDeficit.get(material);
        if(weight > 0) {
            weightsBuilder.put(Range.closedOpen(sum, sum + weight), material);
            sum += weight;
        }
    }
    RangeMap<Double, MaterialData> weights = weightsBuilder.build();
    return weights.get(match.getRandom().nextDouble() * sum);
}
 
Example #19
Source File: DiscretizationUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public RangeMap<Double, Object> load(Discretize discretize){
	return ImmutableRangeMap.copyOf(parseDiscretize(discretize));
}
 
Example #20
Source File: RuntimeColorMap.java    From tcl-regex-java with Apache License 2.0 4 votes vote down vote up
/**
 * Make a runtime color map. It might be sensible for the BMP optimization
 * to be saved someplace and not recomputed here.
 * @param fullMap -- the map as built in {@link com.basistech.tclre.ColorMap}
 */
RuntimeColorMap(RangeMap<Integer, Short> fullMap) {
    this.fullMap = fullMap;
    this.bmpMap = new short[Character.MAX_VALUE + 1];
    computeBmp(fullMap);
}
 
Example #21
Source File: MetricRetention.java    From graphouse with Apache License 2.0 4 votes vote down vote up
public MetricRetention build(RangeMap<Integer, Integer> ranges) {
    result.ranges.clear();
    result.ranges.putAll(ranges);
    return result;
}
 
Example #22
Source File: MetricRetention.java    From graphouse with Apache License 2.0 4 votes vote down vote up
public RangeMap<Integer, Integer> getRanges() {
    return ranges;
}
 
Example #23
Source File: ColorMap.java    From tcl-regex-java with Apache License 2.0 2 votes vote down vote up
/**
 * Return the map for use in the runtime.
 * @return the map.
 */
RangeMap<Integer, Short> getMap() {
    return map;
}