com.google.common.collect.ImmutableRangeMap Java Examples

The following examples show how to use com.google.common.collect.ImmutableRangeMap. 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: MutableDistributionTest.java    From java-monitoring-client-library with Apache License 2.0 6 votes vote down vote up
@Test
public void testAdd_wideRangeOfValues() {
  distribution.add(2.0);
  distribution.add(16.0);
  distribution.add(128.0, 5);
  distribution.add(1024.0, 0);

  assertThat(distribution.count()).isEqualTo(7);
  assertThat(distribution.mean()).isWithin(0.0).of(94.0);
  assertThat(distribution.sumOfSquaredDeviation()).isWithin(0.0).of(20328.0);
  assertThat(distribution.intervalCounts())
      .isEqualTo(
          ImmutableRangeMap.<Double, Long>builder()
              .put(Range.lessThan(3.0), 1L)
              .put(Range.closedOpen(3.0, 5.0), 0L)
              .put(Range.atLeast(5.0), 6L)
              .build());
}
 
Example #2
Source File: MutableDistributionTest.java    From java-monitoring-client-library with Apache License 2.0 6 votes vote down vote up
@Test
public void testAdd_positiveThenNegativeValue() {
  distribution.add(2.0);
  distribution.add(-2.0);

  assertThat(distribution.count()).isEqualTo(2);
  assertThat(distribution.mean()).isWithin(0.0).of(0.0);
  assertThat(distribution.sumOfSquaredDeviation()).isWithin(0.0).of(8.0);
  assertThat(distribution.intervalCounts())
      .isEqualTo(
          ImmutableRangeMap.<Double, Long>builder()
              .put(Range.lessThan(3.0), 2L)
              .put(Range.closedOpen(3.0, 5.0), 0L)
              .put(Range.atLeast(5.0), 0L)
              .build());
}
 
Example #3
Source File: HowToCollectToCustomTypesTest.java    From mug with Apache License 2.0 5 votes vote down vote up
@Test public void how_to_collect_to_immutableRangeMap() {
  BiStream<Range<Integer>, String> biStream =
      BiStream.of(Range.closed(10, 19), "ten", Range.closed(20, 29), "twenty");
  ImmutableRangeMap<Integer, String> map =
      biStream.collect(new BiCollector<Range<Integer>, String, ImmutableRangeMap<Integer, String>>() {
        @Override
        public <E> Collector<E, ?, ImmutableRangeMap<Integer, String>> splitting(Function<E, Range<Integer>> toKey, Function<E, String> toValue) {
          return ImmutableRangeMap.toImmutableRangeMap(toKey,toValue);
        }
      });
  assertThat(map.get(12)).isEqualTo("ten");
}
 
Example #4
Source File: MutableDistributionTest.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd_noFiniteIntervals_overflowValue_returnsOverflowInterval() throws Exception {
  MutableDistribution distribution =
      new MutableDistribution(CustomFitter.create(ImmutableSet.of(5.0)));

  distribution.add(10.0);

  assertThat(distribution.intervalCounts())
      .isEqualTo(
          ImmutableRangeMap.<Double, Long>builder()
              .put(Range.lessThan(5.0), 0L)
              .put(Range.atLeast(5.0), 1L)
              .build());
}
 
Example #5
Source File: CapabilityMultiplexer.java    From besu with Apache License 2.0 5 votes vote down vote up
private ImmutableRangeMap<Integer, Capability> calculateAgreedCapabilities(
    final List<Capability> a, final List<Capability> b) {
  final List<Capability> caps = new ArrayList<>(a);
  caps.sort(CAPABILITY_COMPARATOR);
  caps.retainAll(b);

  final ImmutableRangeMap.Builder<Integer, Capability> builder = ImmutableRangeMap.builder();
  // Reserve some messages for WireProtocol
  int offset = WIRE_PROTOCOL_MESSAGE_SPACE;
  String prevProtocol = null;
  for (final Iterator<Capability> itr = caps.iterator(); itr.hasNext(); ) {
    final Capability cap = itr.next();
    final String curProtocol = cap.getName();
    if (curProtocol.equalsIgnoreCase(prevProtocol)) {
      // A later version of this protocol is already being used, so ignore this version
      continue;
    }
    prevProtocol = curProtocol;
    final SubProtocol subProtocol = subProtocols.get(cap.getName());
    final int messageSpace = subProtocol == null ? 0 : subProtocol.messageSpace(cap.getVersion());
    if (messageSpace > 0) {
      builder.put(Range.closedOpen(offset, offset + messageSpace), cap);
    }
    offset += messageSpace;
  }

  return builder.build();
}
 
Example #6
Source File: MutableDistributionTest.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd_oneFiniteInterval_underflowValue_returnsUnderflowInterval() throws Exception {
  MutableDistribution distribution =
      new MutableDistribution(CustomFitter.create(ImmutableSet.of(1.0, 5.0)));

  distribution.add(0.0);

  assertThat(distribution.intervalCounts())
      .isEqualTo(
          ImmutableRangeMap.<Double, Long>builder()
              .put(Range.lessThan(1.0), 1L)
              .put(Range.closedOpen(1.0, 5.0), 0L)
              .put(Range.atLeast(5.0), 0L)
              .build());
}
 
Example #7
Source File: MutableDistributionTest.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd_oneFiniteInterval_overflowValue_returnsOverflowInterval() throws Exception {
  MutableDistribution distribution =
      new MutableDistribution(CustomFitter.create(ImmutableSet.of(1.0, 5.0)));

  distribution.add(10.0);

  assertThat(distribution.intervalCounts())
      .isEqualTo(
          ImmutableRangeMap.<Double, Long>builder()
              .put(Range.lessThan(1.0), 0L)
              .put(Range.closedOpen(1.0, 5.0), 0L)
              .put(Range.atLeast(5.0), 1L)
              .build());
}
 
Example #8
Source File: MutableDistributionTest.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd_oneFiniteInterval_inBoundsValue_returnsInBoundsInterval() throws Exception {
  MutableDistribution distribution =
      new MutableDistribution(CustomFitter.create(ImmutableSet.of(1.0, 5.0)));

  distribution.add(3.0);

  assertThat(distribution.intervalCounts())
      .isEqualTo(
          ImmutableRangeMap.<Double, Long>builder()
              .put(Range.lessThan(1.0), 0L)
              .put(Range.closedOpen(1.0, 5.0), 1L)
              .put(Range.atLeast(5.0), 0L)
              .build());
}
 
Example #9
Source File: MutableDistributionTest.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd_oneFiniteInterval_firstEdgeValue_returnsFiniteInterval() throws Exception {
  MutableDistribution distribution =
      new MutableDistribution(CustomFitter.create(ImmutableSet.of(1.0, 5.0)));

  distribution.add(1.0);

  assertThat(distribution.intervalCounts())
      .isEqualTo(
          ImmutableRangeMap.<Double, Long>builder()
              .put(Range.lessThan(1.0), 0L)
              .put(Range.closedOpen(1.0, 5.0), 1L)
              .put(Range.atLeast(5.0), 0L)
              .build());
}
 
Example #10
Source File: MutableDistributionTest.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd_oneFiniteInterval_secondEdgeValue_returnsOverflowInterval() throws Exception {
  MutableDistribution distribution =
      new MutableDistribution(CustomFitter.create(ImmutableSet.of(1.0, 5.0)));

  distribution.add(5.0);

  assertThat(distribution.intervalCounts())
      .isEqualTo(
          ImmutableRangeMap.<Double, Long>builder()
              .put(Range.lessThan(1.0), 0L)
              .put(Range.closedOpen(1.0, 5.0), 0L)
              .put(Range.atLeast(5.0), 1L)
              .build());
}
 
Example #11
Source File: MutableDistributionTest.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd_noFiniteIntervals_edgeValue_returnsOverflowInterval() throws Exception {
  MutableDistribution distribution =
      new MutableDistribution(CustomFitter.create(ImmutableSet.of(2.0)));

  distribution.add(2.0);

  assertThat(distribution.intervalCounts())
      .isEqualTo(
          ImmutableRangeMap.<Double, Long>builder()
              .put(Range.lessThan(2.0), 0L)
              .put(Range.atLeast(2.0), 1L)
              .build());
}
 
Example #12
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 #13
Source File: BlockMapBuilder.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
protected List<CompleteFileWork> runInner() throws Exception {
  final List<CompleteFileWork> work = Lists.newArrayList();
  boolean error = false;
  if (blockify && !compressed(attributes)) {
    try {
      ImmutableRangeMap<Long, FileBlockLocation> rangeMap = getBlockMap(attributes);
      for (Entry<Range<Long>, FileBlockLocation> l : rangeMap.asMapOfRanges().entrySet()) {
        work.add(new CompleteFileWork(getEndpointByteMap(new FileAttributesWork(attributes, l.getValue().getOffset(), l.getValue().getSize())),
                l.getValue().getOffset(), l.getValue().getSize(), attributes));
      }
    } catch (IOException e) {
      logger.warn("failure while generating file work.", e);
      error = true;
    }
  }


  if (!blockify || error || compressed(attributes)) {
    work.add(new CompleteFileWork(getEndpointByteMap(new FileAttributesWork(attributes)), 0, attributes.size(), attributes));
  }

  // This if-condition is specific for empty CSV file
  // For CSV files, the global variable blockify is set as true
  // And if this CSV file is empty, rangeMap would be empty also
  // Therefore, at the point before this if-condition, work would not be populated
  if(work.isEmpty()) {
    work.add(new CompleteFileWork(getEndpointByteMap(new FileAttributesWork(attributes)), 0, 0, attributes));
  }

  return work;
}
 
Example #14
Source File: BlockMapBuilder.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private ImmutableRangeMap<Long,FileBlockLocation> getBlockMap(FileAttributes attributes) throws IOException{
  ImmutableRangeMap<Long,FileBlockLocation> blockMap  = blockMapMap.get(attributes.getPath());
  if (blockMap == null) {
    blockMap = buildBlockMap(attributes);
  }
  return blockMap;
}
 
Example #15
Source File: BlockMapBuilder.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * For a given FileWork, calculate how many bytes are available on each on node endpoint
 *
 * @param work the FileWork to calculate endpoint bytes for
 * @throws IOException
 */
public EndpointByteMap getEndpointByteMap(FileWork work) throws IOException {
  Stopwatch watch = Stopwatch.createStarted();



  ImmutableRangeMap<Long,FileBlockLocation> blockMap = getBlockMap(work.getFileAttributes());
  EndpointByteMapImpl endpointByteMap = new EndpointByteMapImpl();
  long start = work.getStart();
  long end = start + work.getLength();
  Range<Long> rowGroupRange = Range.closedOpen(start, end);

  // Find submap of ranges that intersect with the rowGroup
  ImmutableRangeMap<Long,FileBlockLocation> subRangeMap = blockMap.subRangeMap(rowGroupRange);

  // Iterate through each block in this submap and get the host for the block location
  for (Map.Entry<Range<Long>,FileBlockLocation> block : subRangeMap.asMapOfRanges().entrySet()) {
    List<String> hosts = block.getValue().getHosts();
    Range<Long> blockRange = block.getKey();
    Range<Long> intersection = rowGroupRange.intersection(blockRange);
    long bytes = intersection.upperEndpoint() - intersection.lowerEndpoint();

    // For each host in the current block location, add the intersecting bytes to the corresponding endpoint
    for (String host : hosts) {
      NodeEndpoint endpoint = getNodeEndpoint(host);
      if (endpoint != null) {
        endpointByteMap.add(HostAndPort.fromHost(endpoint.getAddress()), bytes);
      } else {
        logger.debug("Failure finding SabotNode running on host {}.  Skipping affinity to that host.", host);
      }
    }
  }

  logger.debug("FileWork group ({},{}) max bytes {}", work.getFileAttributes().getPath(), work.getStart(), endpointByteMap.getMaxBytes());

  logger.debug("Took {} ms to set endpoint bytes", watch.stop().elapsed(TimeUnit.MILLISECONDS));
  return endpointByteMap;
}
 
Example #16
Source File: TestAffinityCalculator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildRangeMap() {
  BlockLocation[] blocks = buildBlockLocations(new String[4], 256*1024*1024);
  long tA = System.nanoTime();
  ImmutableRangeMap.Builder<Long, BlockLocation> blockMapBuilder = new ImmutableRangeMap.Builder<>();
  for (BlockLocation block : blocks) {
    long start = block.getOffset();
    long end = start + block.getLength();
    Range<Long> range = Range.closedOpen(start, end);
    blockMapBuilder = blockMapBuilder.put(range, block);
  }
  ImmutableRangeMap<Long,BlockLocation> map = blockMapBuilder.build();
  long tB = System.nanoTime();
  System.out.println(String.format("Took %f ms to build range map", (tB - tA) / 1e6));
}
 
Example #17
Source File: LineMap.java    From turbine with Apache License 2.0 5 votes vote down vote up
public static LineMap create(String source) {
  int last = 0;
  int line = 1;
  ImmutableRangeMap.Builder<Integer, Integer> builder = ImmutableRangeMap.builder();
  for (int idx = 0; idx < source.length(); idx++) {
    char ch = source.charAt(idx);
    switch (ch) {
        // handle CR line endings
      case '\r':
        // ...and CRLF
        if (idx + 1 < source.length() && source.charAt(idx + 1) == '\n') {
          idx++;
        }
        // falls through
      case '\n':
        builder.put(Range.closedOpen(last, idx + 1), line++);
        last = idx + 1;
        break;
      default:
        break;
    }
  }
  // no trailing newline
  if (last < source.length()) {
    builder.put(Range.closedOpen(last, source.length()), line++);
  }
  return new LineMap(source, builder.build());
}
 
Example #18
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 #19
Source File: GuavaRangeMapUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void givenImmutableRangeMap_whenRangeOverlaps_ThrowsException() {
    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(8, 15), "Executive Director")
   .put(Range.closed(16, 30), "Managing Director").build();

}
 
Example #20
Source File: ImmutableDistribution.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static ImmutableDistribution create(
    double mean,
    double sumOfSquaredDeviation,
    long count,
    ImmutableRangeMap<Double, Long> intervalCounts,
    DistributionFitter distributionFitter) {
  checkDouble(mean);
  checkDouble(sumOfSquaredDeviation);
  checkArgument(count >= 0);

  return new AutoValue_ImmutableDistribution(
      mean, sumOfSquaredDeviation, count, intervalCounts, distributionFitter);
}
 
Example #21
Source File: CapabilityMultiplexer.java    From besu with Apache License 2.0 5 votes vote down vote up
private static ImmutableMap<Capability, Integer> calculateCapabilityOffsets(
    final ImmutableRangeMap<Integer, Capability> agreedCaps) {
  final ImmutableMap.Builder<Capability, Integer> capToOffset = ImmutableMap.builder();
  for (final Map.Entry<Range<Integer>, Capability> entry :
      agreedCaps.asMapOfRanges().entrySet()) {
    capToOffset.put(entry.getValue(), entry.getKey().lowerEndpoint());
  }
  return capToOffset.build();
}
 
Example #22
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 #23
Source File: MutableDistributionTest.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd_fitterWithNoFiniteIntervals_underflowValue_returnsUnderflowInterval()
    throws Exception {
  MutableDistribution distribution =
      new MutableDistribution(CustomFitter.create(ImmutableSet.of(5.0)));

  distribution.add(3.0);

  assertThat(distribution.intervalCounts())
      .isEqualTo(
          ImmutableRangeMap.<Double, Long>builder()
              .put(Range.lessThan(5.0), 1L)
              .put(Range.atLeast(5.0), 0L)
              .build());
}
 
Example #24
Source File: MutableDistributionTest.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd_multipleOfOneValue() {
  distribution.add(4.0, 2);

  assertThat(distribution.count()).isEqualTo(2);
  assertThat(distribution.mean()).isWithin(0.0).of(4.0);
  assertThat(distribution.sumOfSquaredDeviation()).isWithin(0.0).of(0);
  assertThat(distribution.intervalCounts())
      .isEqualTo(
          ImmutableRangeMap.<Double, Long>builder()
              .put(Range.lessThan(3.0), 0L)
              .put(Range.closedOpen(3.0, 5.0), 2L)
              .put(Range.atLeast(5.0), 0L)
              .build());
}
 
Example #25
Source File: MutableDistributionTest.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd_zero() {
  distribution.add(0.0);

  assertThat(distribution.count()).isEqualTo(1);
  assertThat(distribution.mean()).isWithin(0.0).of(0.0);
  assertThat(distribution.sumOfSquaredDeviation()).isWithin(0.0).of(0);
  assertThat(distribution.intervalCounts())
      .isEqualTo(
          ImmutableRangeMap.<Double, Long>builder()
              .put(Range.lessThan(3.0), 1L)
              .put(Range.closedOpen(3.0, 5.0), 0L)
              .put(Range.atLeast(5.0), 0L)
              .build());
}
 
Example #26
Source File: MutableDistributionTest.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd_oneValue() {
  distribution.add(5.0);

  assertThat(distribution.count()).isEqualTo(1);
  assertThat(distribution.mean()).isWithin(0.0).of(5.0);
  assertThat(distribution.sumOfSquaredDeviation()).isWithin(0.0).of(0);
  assertThat(distribution.intervalCounts())
      .isEqualTo(
          ImmutableRangeMap.<Double, Long>builder()
              .put(Range.lessThan(3.0), 0L)
              .put(Range.closedOpen(3.0, 5.0), 0L)
              .put(Range.atLeast(5.0), 1L)
              .build());
}
 
Example #27
Source File: MutableDistribution.java    From java-monitoring-client-library with Apache License 2.0 4 votes vote down vote up
@Override
public ImmutableRangeMap<Double, Long> intervalCounts() {
  return ImmutableRangeMap.copyOf(intervalCounts);
}
 
Example #28
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 #29
Source File: Input.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
/** A map from [start, end] position ranges to {@link Token}s. */
public abstract ImmutableRangeMap<Integer, ? extends Token> getPositionTokenMap();
 
Example #30
Source File: LineMap.java    From turbine with Apache License 2.0 4 votes vote down vote up
private LineMap(String source, ImmutableRangeMap<Integer, Integer> lines) {
  this.source = source;
  this.lines = lines;
}