Java Code Examples for java.util.function.LongSupplier#getAsLong()

The following examples show how to use java.util.function.LongSupplier#getAsLong() . 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: AddressDivisionGrouping.java    From IPAddress with Apache License 2.0 6 votes vote down vote up
protected static void checkOverflow(
		long increment,
		long lowerValue,
		long upperValue,
		long count,
		LongSupplier maxValue
		) {
	if(increment < 0) {
		if(lowerValue < -increment) {
			throw new AddressValueException(increment);
		}
	} else {
		if(count > 1) {
			increment -= count - 1;
		}
		if(increment > maxValue.getAsLong() - upperValue) {
			throw new AddressValueException(increment);
		}
	}
}
 
Example 2
Source File: LeaderState.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
private long[] getSorted(List<RaftPeerId> followerIDs, boolean includeSelf,
    ToLongFunction<FollowerInfo> getFollowerIndex, LongSupplier getLogIndex) {
  final int length = includeSelf ? followerIDs.size() + 1 : followerIDs.size();
  if (length == 0) {
    throw new IllegalArgumentException("followers.size() == "
        + followerIDs.size() + " and includeSelf == " + includeSelf);
  }

  final long[] indices = new long[length];
  List<FollowerInfo> followerInfos = getFollowerInfos(followerIDs);
  for (int i = 0; i < followerInfos.size(); i++) {
    indices[i] = getFollowerIndex.applyAsLong(followerInfos.get(i));
  }

  if (includeSelf) {
    // note that we also need to wait for the local disk I/O
    indices[length - 1] = getLogIndex.getAsLong();
  }

  Arrays.sort(indices);
  return indices;
}
 
Example 3
Source File: AbstractSpaceUsageSource.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Measures execution time of {@code supplier#getAsLong} and logs it via the
 * given {@code logger}.
 * @return the same value as returned by {@code supplier#getAsLong}
 */
protected static long time(LongSupplier supplier, Logger logger) {
  long start = Time.monotonicNow();

  long result = supplier.getAsLong();

  long end = Time.monotonicNow();
  long elapsed = end - start;
  logger.debug("Completed check in {} ms, result: {}", elapsed, result);

  return result;
}
 
Example 4
Source File: AbstractCommandSizeValidator.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Guard function that throws when a size limit is specified, and the given size supplier returns a size
 * greater than the limit.
 *
 * @param sizeSupplier the length calc function (only called when limit is present)
 * @param headersSupplier the headersSupplier for the exception
 * @throws T if size limit is set and exceeded
 */
public void ensureValidSize(final LongSupplier sizeSupplier, final Supplier<DittoHeaders> headersSupplier) {
    if (null != maxSize) {
        long actualSize = sizeSupplier.getAsLong();
        if (maxSize < actualSize) {
            throw newInvalidSizeException(maxSize, actualSize, headersSupplier.get());
        }
    }
}
 
Example 5
Source File: TimeWindowMax.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private void record(LongSupplier sampleSupplier) {
    rotate();
    long sample = sampleSupplier.getAsLong();
    for (AtomicLong max : ringBuffer) {
        updateMax(max, sample);
    }
}
 
Example 6
Source File: TestLucene80DocValuesFormat.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void doTestSparseNumericBlocksOfVariousBitsPerValue(double density) throws Exception {
  Directory dir = newDirectory();
  IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
  conf.setMaxBufferedDocs(atLeast(Lucene80DocValuesFormat.NUMERIC_BLOCK_SIZE));
  conf.setRAMBufferSizeMB(-1);
  conf.setMergePolicy(newLogMergePolicy(random().nextBoolean()));
  IndexWriter writer = new IndexWriter(dir, conf);
  Document doc = new Document();
  Field storedField = newStringField("stored", "", Field.Store.YES);
  Field dvField = new NumericDocValuesField("dv", 0);
  doc.add(storedField);
  doc.add(dvField);

  final int numDocs = atLeast(Lucene80DocValuesFormat.NUMERIC_BLOCK_SIZE*3);
  final LongSupplier longs = blocksOfVariousBPV();
  for (int i = 0; i < numDocs; i++) {
    if (random().nextDouble() > density) {
      writer.addDocument(new Document());
      continue;
    }
    long value = longs.getAsLong();
    storedField.setStringValue(Long.toString(value));
    dvField.setLongValue(value);
    writer.addDocument(doc);
  }

  writer.forceMerge(1);

  writer.close();
  
  // compare
  assertDVIterate(dir);
  assertDVAdvance(dir, 1); // Tests all jump-lengths from 1 to maxDoc (quite slow ~= 1 minute for 200K docs)

  dir.close();
}
 
Example 7
Source File: DynamicLambdaTest.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public void testPromoteReturnType() throws Exception {
    class C { int woot() { return 123; } }
    final LongSupplier lambda = Methods.lambda(LongSupplier.class, C.class.getDeclaredMethod("woot"), new C());
    lambda.getAsLong();
}
 
Example 8
Source File: UnmodifiableConfig.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Like {@link #getOrElse(String[], Supplier)} but returns a primitive long.
 * The config's value must be a {@link Number} or null or nonexistant.
 */
default long getLongOrElse(String[] path, LongSupplier defaultValueSupplier) {
	Number n = get(path);
	return (n == null) ? defaultValueSupplier.getAsLong() : n.longValue();
}
 
Example 9
Source File: TestLucene80DocValuesFormat.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void doTestSortedNumericBlocksOfVariousBitsPerValue(LongSupplier counts) throws Exception {
  Directory dir = newDirectory();
  IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
  conf.setMaxBufferedDocs(atLeast(Lucene80DocValuesFormat.NUMERIC_BLOCK_SIZE));
  conf.setRAMBufferSizeMB(-1);
  conf.setMergePolicy(newLogMergePolicy(random().nextBoolean()));
  IndexWriter writer = new IndexWriter(dir, conf);
  
  final int numDocs = atLeast(Lucene80DocValuesFormat.NUMERIC_BLOCK_SIZE*3);
  final LongSupplier values = blocksOfVariousBPV();
  for (int i = 0; i < numDocs; i++) {
    Document doc = new Document();
    
    int valueCount = (int) counts.getAsLong();
    long valueArray[] = new long[valueCount];
    for (int j = 0; j < valueCount; j++) {
      long value = values.getAsLong();
      valueArray[j] = value;
      doc.add(new SortedNumericDocValuesField("dv", value));
    }
    Arrays.sort(valueArray);
    for (int j = 0; j < valueCount; j++) {
      doc.add(new StoredField("stored", Long.toString(valueArray[j])));
    }
    writer.addDocument(doc);
    if (random().nextInt(31) == 0) {
      writer.commit();
    }
  }
  writer.forceMerge(1);

  writer.close();
  
  // compare
  DirectoryReader ir = DirectoryReader.open(dir);
  TestUtil.checkReader(ir);
  for (LeafReaderContext context : ir.leaves()) {
    LeafReader r = context.reader();
    SortedNumericDocValues docValues = DocValues.getSortedNumeric(r, "dv");
    for (int i = 0; i < r.maxDoc(); i++) {
      if (i > docValues.docID()) {
        docValues.nextDoc();
      }
      String expected[] = r.document(i).getValues("stored");
      if (i < docValues.docID()) {
        assertEquals(0, expected.length);
      } else {
        String actual[] = new String[docValues.docValueCount()];
        for (int j = 0; j < actual.length; j++) {
          actual[j] = Long.toString(docValues.nextValue());
        }
        assertArrayEquals(expected, actual);
      }
    }
  }
  ir.close();
  dir.close();
}
 
Example 10
Source File: TomlTable.java    From cava with Apache License 2.0 3 votes vote down vote up
/**
 * Get a long from the TOML document, or return a default.
 *
 * @param path The key path.
 * @param defaultValue A supplier for the default value.
 * @return The value, or the default.
 * @throws TomlInvalidTypeException If the value is present but not a long, or any element of the path preceding the
 *         final key is not a table.
 */
default long getLong(List<String> path, LongSupplier defaultValue) {
  requireNonNull(defaultValue);
  Long value = getLong(path);
  if (value != null) {
    return value;
  }
  return defaultValue.getAsLong();
}
 
Example 11
Source File: TomlTable.java    From incubator-tuweni with Apache License 2.0 3 votes vote down vote up
/**
 * Get a long from the TOML document, or return a default.
 *
 * @param path The key path.
 * @param defaultValue A supplier for the default value.
 * @return The value, or the default.
 * @throws TomlInvalidTypeException If the value is present but not a long, or any element of the path preceding the
 *         final key is not a table.
 */
default long getLong(List<String> path, LongSupplier defaultValue) {
  requireNonNull(defaultValue);
  Long value = getLong(path);
  if (value != null) {
    return value;
  }
  return defaultValue.getAsLong();
}
 
Example 12
Source File: OptionalLong.java    From Java8CN with Apache License 2.0 2 votes vote down vote up
/**
 * Return the value if present, otherwise invoke {@code other} and return
 * the result of that invocation.
 *
 * @param other a {@code LongSupplier} whose result is returned if no value
 * is present
 * @return the value if present otherwise the result of {@code other.getAsLong()}
 * @throws NullPointerException if value is not present and {@code other} is
 * null
 */
public long orElseGet(LongSupplier other) {
    return isPresent ? value : other.getAsLong();
}
 
Example 13
Source File: OptionalLong.java    From JDKSourceCode1.8 with MIT License 2 votes vote down vote up
/**
 * Return the value if present, otherwise invoke {@code other} and return
 * the result of that invocation.
 *
 * @param other a {@code LongSupplier} whose result is returned if no value
 * is present
 * @return the value if present otherwise the result of {@code other.getAsLong()}
 * @throws NullPointerException if value is not present and {@code other} is
 * null
 */
public long orElseGet(LongSupplier other) {
    return isPresent ? value : other.getAsLong();
}
 
Example 14
Source File: OptionalLong.java    From desugar_jdk_libs with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Return the value if present, otherwise invoke {@code other} and return
 * the result of that invocation.
 *
 * @param other a {@code LongSupplier} whose result is returned if no value
 * is present
 * @return the value if present otherwise the result of {@code other.getAsLong()}
 * @throws NullPointerException if value is not present and {@code other} is
 * null
 */
public long orElseGet(LongSupplier other) {
    return isPresent ? value : other.getAsLong();
}
 
Example 15
Source File: OptionalLong.java    From TencentKona-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Return the value if present, otherwise invoke {@code other} and return
 * the result of that invocation.
 *
 * @param other a {@code LongSupplier} whose result is returned if no value
 * is present
 * @return the value if present otherwise the result of {@code other.getAsLong()}
 * @throws NullPointerException if value is not present and {@code other} is
 * null
 */
public long orElseGet(LongSupplier other) {
    return isPresent ? value : other.getAsLong();
}
 
Example 16
Source File: OptionalLong.java    From dragonwell8_jdk with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Return the value if present, otherwise invoke {@code other} and return
 * the result of that invocation.
 *
 * @param other a {@code LongSupplier} whose result is returned if no value
 * is present
 * @return the value if present otherwise the result of {@code other.getAsLong()}
 * @throws NullPointerException if value is not present and {@code other} is
 * null
 */
public long orElseGet(LongSupplier other) {
    return isPresent ? value : other.getAsLong();
}
 
Example 17
Source File: OptionalLong.java    From openjdk-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Return the value if present, otherwise invoke {@code other} and return
 * the result of that invocation.
 *
 * @param other a {@code LongSupplier} whose result is returned if no value
 * is present
 * @return the value if present otherwise the result of {@code other.getAsLong()}
 * @throws NullPointerException if value is not present and {@code other} is
 * null
 */
public long orElseGet(LongSupplier other) {
    return isPresent ? value : other.getAsLong();
}
 
Example 18
Source File: OptionalLong.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * If a value is present, returns the value, otherwise returns the result
 * produced by the supplying function.
 *
 * @param supplier the supplying function that produces a value to be returned
 * @return the value, if present, otherwise the result produced by the
 *         supplying function
 * @throws NullPointerException if no value is present and the supplying
 *         function is {@code null}
 */
public long orElseGet(LongSupplier supplier) {
    return isPresent ? value : supplier.getAsLong();
}
 
Example 19
Source File: OptionalLong.java    From jdk8u-jdk with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Return the value if present, otherwise invoke {@code other} and return
 * the result of that invocation.
 *
 * @param other a {@code LongSupplier} whose result is returned if no value
 * is present
 * @return the value if present otherwise the result of {@code other.getAsLong()}
 * @throws NullPointerException if value is not present and {@code other} is
 * null
 */
public long orElseGet(LongSupplier other) {
    return isPresent ? value : other.getAsLong();
}
 
Example 20
Source File: AbstractCommandSizeValidator.java    From ditto with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Guard function that throws when a size limit is specified, and the given size supplier returns a size
 * greater than the limit.
 * Only calls the sizeSupplier, if the upper bound provided by the upperBoundSupplier exceeds the limit.
 *
 * @param upperBoundSupplier a calculation function that returns an upper bound for the size
 * (possibly {@link Long#MAX_VALUE})
 * @param sizeSupplier the length calc function (only called when limit is present)
 * @param headersSupplier the headersSupplier for the exception
 * @throws T if size limit is set and exceeded
 * @since 1.1.0
 */
public void ensureValidSize(final LongSupplier upperBoundSupplier, final LongSupplier sizeSupplier, final Supplier<DittoHeaders> headersSupplier) {
    if (null != maxSize && upperBoundSupplier.getAsLong() >= maxSize) {
        ensureValidSize(sizeSupplier, headersSupplier);
    }
}