下面列出了java.util.function.LongSupplier#getAsLong ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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);
}
}
}
/**
* 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;
}
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();
}
private void record(LongSupplier sampleSupplier) {
rotate();
long sample = sampleSupplier.getAsLong();
for (AtomicLong max : ringBuffer) {
updateMax(max, sample);
}
}
/**
* 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());
}
}
}
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();
}
/**
* 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();
}
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();
}
/**
* 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();
}
/**
* 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();
}
/**
* 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();
}
/**
* 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();
}
/**
* 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();
}
/**
* 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();
}
/**
* 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();
}
/**
* 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();
}
/**
* 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();
}
/**
* 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();
}
/**
* 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);
}
}