下面列出了java.util.function.LongToIntFunction#java.util.function.LongFunction 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private List<Bytes> readList(LongFunction<Bytes> bytesSupplier) {
ensureBytes(4, () -> "SSZ encoded data is not a list");
int originalIndex = this.index;
List<Bytes> elements;
try {
// use a long to simulate reading unsigned
long listSize = consumeBytes(4).toLong(LITTLE_ENDIAN);
elements = new ArrayList<>();
while (listSize > 0) {
Bytes bytes = bytesSupplier.apply(listSize);
elements.add(bytes);
listSize -= bytes.size();
listSize -= 4;
if (listSize < 0) {
throw new InvalidSSZTypeException("SSZ encoded list length does not align with lengths of its elements");
}
}
} catch (Exception e) {
this.index = originalIndex;
throw e;
}
return elements;
}
private <T> List<T> readFixedList(int listSize, LongFunction<byte[]> bytesSupplier, Function<byte[], T> converter) {
int originalIndex = this.index;
List<T> elements;
try {
elements = new ArrayList<>();
while (listSize > 0) {
byte[] bytes = bytesSupplier.apply(listSize);
elements.add(converter.apply(bytes));
// When lists have lengths passed in, the listSize argument is the number of
// elements in the list, instead of the number of bytes in the list, so
// we only subtract one each time an element is processed in this case.
listSize -= 1;
if (listSize < 0) {
throw new InvalidSSZTypeException("SSZ encoded list length does not align with lengths of its elements");
}
}
} catch (Exception e) {
this.index = originalIndex;
throw e;
}
return elements;
}
private static <T> void readNonNullLongColumn(Object[] vals, int fieldIdx, LongColumnVector vector,
int childCount, LongFunction<T> reader) {
if (vector.isRepeating) { // fill complete column with first value
T repeatingValue = reader.apply(vector.vector[0]);
fillColumnWithRepeatingValue(vals, fieldIdx, repeatingValue, childCount);
} else {
if (fieldIdx == -1) { // set as an object
for (int i = 0; i < childCount; i++) {
vals[i] = reader.apply(vector.vector[i]);
}
} else { // set as a field of Row
Row[] rows = (Row[]) vals;
for (int i = 0; i < childCount; i++) {
rows[i].setField(fieldIdx, reader.apply(vector.vector[i]));
}
}
}
}
@Override
public final <U> Stream<U> mapToObj(LongFunction<? extends U> mapper) {
Objects.requireNonNull(mapper);
return new ReferencePipeline.StatelessOp<Long, U>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Long> opWrapSink(int flags, Sink<U> sink) {
return new Sink.ChainedLong<U>(sink) {
@Override
public void accept(long t) {
downstream.accept(mapper.apply(t));
}
};
}
};
}
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
return new Sink.ChainedLong<Long>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(long t) {
try (LongStream result = mapper.apply(t)) {
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
if (result != null)
result.sequential().forEach(i -> downstream.accept(i));
}
}
};
}
};
}
CompletableFuture<RaftClientReply> send(RaftClientRequest.Type type, Message message, RaftPeerId server) {
if (!type.is(TypeCase.WATCH) && !type.is(TypeCase.STREAM)) {
Objects.requireNonNull(message, "message == null");
}
try {
requestSemaphore.acquire();
} catch (InterruptedException e) {
return JavaUtils.completeExceptionally(IOUtils.toInterruptedIOException(
"Interrupted when sending " + type + ", message=" + message, e));
}
final long callId = RaftClientImpl.nextCallId();
final LongFunction<PendingOrderedRequest> constructor = seqNum -> new PendingOrderedRequest(callId, seqNum,
slidingWindowEntry -> client.newRaftClientRequest(server, callId, message, type, slidingWindowEntry));
return getSlidingWindow(server).submitNewRequest(constructor, this::sendRequestWithRetry
).getReplyFuture(
).thenApply(reply -> RaftClientImpl.handleRaftException(reply, CompletionException::new)
).whenComplete((r, e) -> requestSemaphore.release());
}
private static <T> void readNonNullLongColumn(Object[] vals, int fieldIdx, LongColumnVector vector,
int childCount, LongFunction<T> reader) {
if (vector.isRepeating) { // fill complete column with first value
T repeatingValue = reader.apply(vector.vector[0]);
fillColumnWithRepeatingValue(vals, fieldIdx, repeatingValue, childCount);
} else {
if (fieldIdx == -1) { // set as an object
for (int i = 0; i < childCount; i++) {
vals[i] = reader.apply(vector.vector[i]);
}
} else { // set as a field of Row
Row[] rows = (Row[]) vals;
for (int i = 0; i < childCount; i++) {
rows[i].setField(fieldIdx, reader.apply(vector.vector[i]));
}
}
}
}
public <N extends Number> N[] numberProvider(LongFunction<N> boxer, int bits, N... extras) {
List<N> numbers = new ArrayList<>();
for(int bitmag = 0; bitmag < bits; bitmag++) {
long value = 1L << bitmag;
numbers.add(boxer.apply(value));
numbers.add(boxer.apply(value - 1));
numbers.add(boxer.apply(value + 1));
numbers.add(boxer.apply(-value));
for(int divisor = 0; divisor < SOME_PRIMES.length && value < SOME_PRIMES[divisor]; divisor++) {
numbers.add(boxer.apply(value - SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value + SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value * SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value / SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value | SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value & SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value ^ SOME_PRIMES[divisor]));
}
}
numbers.addAll(Arrays.asList(extras));
return (N[]) numbers.toArray(new Number[numbers.size()]);
}
private SingleOutputStreamOperator<Void> feedbackOperator(
SingleOutputStreamOperator<Message> functionOut) {
LongFunction<Message> toMessage = new CheckpointToMessage(universe.messageFactoryType());
FeedbackSinkOperator<Message> sinkOperator =
new FeedbackSinkOperator<>(FEEDBACK_KEY, toMessage);
return functionOut
.keyBy(new MessageKeySelector())
.transform(
StatefulFunctionsJobConstants.WRITE_BACK_OPERATOR_NAME,
TypeInformation.of(Void.class),
sinkOperator)
.uid(StatefulFunctionsJobConstants.WRITE_BACK_OPERATOR_UID);
}
@Override
public final <U> Stream<U> mapToObj(LongFunction<? extends U> mapper) {
Objects.requireNonNull(mapper);
return new ReferencePipeline.StatelessOp<Long, U>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Long> opWrapSink(int flags, Sink<U> sink) {
return new Sink.ChainedLong<U>(sink) {
@Override
public void accept(long t) {
downstream.accept(mapper.apply(t));
}
};
}
};
}
/**
* Creates a new iterator over an index.
*
* <p>The returned iterator is a {@link ConfigurableRustIter}
* wrapped in a {@link RustIterAdapter}.
*
* @param nativeHandle nativeHandle of this iterator
* @param nextFunction a function to call to get the next item
* @param disposeOperation an operation to call to destroy the corresponding native iterator
* @param collectionAccess a database access of the collection over which to iterate
* @param modificationCounter a modification counter of the collection
* @param transformingFunction a function to apply to elements returned by native iterator
* (usually, to an array of bytes)
*/
static <ElementT, NativeT> Iterator<ElementT> createIterator(
long nativeHandle,
LongFunction<NativeT> nextFunction,
LongConsumer disposeOperation,
AbstractAccess collectionAccess,
ModificationCounter modificationCounter,
Function<? super NativeT, ? extends ElementT> transformingFunction) {
// Register the destructor first.
NativeHandle handle = new NativeHandle(nativeHandle);
Cleaner cleaner = collectionAccess.getCleaner();
cleaner.add(new ProxyDestructor(handle, RustIter.class, disposeOperation));
Iterator<NativeT> iterator = new RustIterAdapter<>(
new ConfigurableRustIter<>(
handle,
nextFunction,
modificationCounter
)
);
return Iterators.transform(iterator, transformingFunction::apply);
}
public <N extends Number> N[] numberProvider(LongFunction<N> boxer, int bits, N... extras) {
List<N> numbers = new ArrayList<>();
for(int bitmag = 0; bitmag < bits; bitmag++) {
long value = 1L << bitmag;
numbers.add(boxer.apply(value));
numbers.add(boxer.apply(value - 1));
numbers.add(boxer.apply(value + 1));
numbers.add(boxer.apply(-value));
for(int divisor = 0; divisor < SOME_PRIMES.length && value < SOME_PRIMES[divisor]; divisor++) {
numbers.add(boxer.apply(value - SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value + SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value * SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value / SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value | SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value & SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value ^ SOME_PRIMES[divisor]));
}
}
numbers.addAll(Arrays.asList(extras));
return (N[]) numbers.toArray(new Number[numbers.size()]);
}
@Override
public final <U> Stream<U> mapToObj(LongFunction<? extends U> mapper) {
Objects.requireNonNull(mapper);
return new ReferencePipeline.StatelessOp<Long, U>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Long> opWrapSink(int flags, Sink<U> sink) {
return new Sink.ChainedLong<U>(sink) {
@Override
public void accept(long t) {
downstream.accept(mapper.apply(t));
}
};
}
};
}
private SingleOutputStreamOperator<Void> feedbackOperator(
SingleOutputStreamOperator<Message> functionOut) {
LongFunction<Message> toMessage = new CheckpointToMessage(universe.messageFactoryType());
FeedbackSinkOperator<Message> sinkOperator =
new FeedbackSinkOperator<>(FEEDBACK_KEY, toMessage);
return functionOut
.keyBy(new MessageKeySelector())
.transform(
StatefulFunctionsJobConstants.WRITE_BACK_OPERATOR_NAME,
TypeInformation.of(Void.class),
sinkOperator)
.uid(StatefulFunctionsJobConstants.WRITE_BACK_OPERATOR_UID);
}
@Override
public final <U> Stream<U> mapToObj(LongFunction<? extends U> mapper) {
Objects.requireNonNull(mapper);
return new ReferencePipeline.StatelessOp<Long, U>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Long> opWrapSink(int flags, Sink<U> sink) {
return new Sink.ChainedLong<U>(sink) {
@Override
public void accept(long t) {
downstream.accept(mapper.apply(t));
}
};
}
};
}
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
return new Sink.ChainedLong<Long>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(long t) {
try (LongStream result = mapper.apply(t)) {
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
if (result != null)
result.sequential().forEach(i -> downstream.accept(i));
}
}
};
}
};
}
public <N extends Number> N[] numberProvider(LongFunction<N> boxer, int bits, N... extras) {
List<N> numbers = new ArrayList<>();
for(int bitmag = 0; bitmag < bits; bitmag++) {
long value = 1L << bitmag;
numbers.add(boxer.apply(value));
numbers.add(boxer.apply(value - 1));
numbers.add(boxer.apply(value + 1));
numbers.add(boxer.apply(-value));
for(int divisor = 0; divisor < SOME_PRIMES.length && value < SOME_PRIMES[divisor]; divisor++) {
numbers.add(boxer.apply(value - SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value + SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value * SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value / SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value | SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value & SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value ^ SOME_PRIMES[divisor]));
}
}
numbers.addAll(Arrays.asList(extras));
return (N[]) numbers.toArray(new Number[numbers.size()]);
}
private <T> List<T> readList(LongFunction<byte[]> bytesSupplier, Function<byte[], T> converter) {
ensureBytes(4, () -> "SSZ encoded data is not a list");
int originalIndex = this.index;
List<T> elements;
try {
// use a long to simulate reading unsigned
long listSize = consumeBytes(4).toLong(LITTLE_ENDIAN);
elements = new ArrayList<>();
while (listSize > 0) {
byte[] bytes = bytesSupplier.apply(listSize);
elements.add(converter.apply(bytes));
listSize -= bytes.length;
listSize -= 4;
if (listSize < 0) {
throw new InvalidSSZTypeException("SSZ encoded list length does not align with lengths of its elements");
}
}
} catch (Exception e) {
this.index = originalIndex;
throw e;
}
return elements;
}
@Override
public final <U> Stream<U> mapToObj(LongFunction<? extends U> mapper) {
Objects.requireNonNull(mapper);
return new ReferencePipeline.StatelessOp<Long, U>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Long> opWrapSink(int flags, Sink<U> sink) {
return new Sink.ChainedLong<U>(sink) {
@Override
public void accept(long t) {
downstream.accept(mapper.apply(t));
}
};
}
};
}
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
return new Sink.ChainedLong<Long>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(long t) {
try (LongStream result = mapper.apply(t)) {
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
if (result != null)
result.sequential().forEach(i -> downstream.accept(i));
}
}
};
}
};
}
public <N extends Number> N[] numberProvider(LongFunction<N> boxer, int bits, N... extras) {
List<N> numbers = new ArrayList<>();
for(int bitmag = 0; bitmag < bits; bitmag++) {
long value = 1L << bitmag;
numbers.add(boxer.apply(value));
numbers.add(boxer.apply(value - 1));
numbers.add(boxer.apply(value + 1));
numbers.add(boxer.apply(-value));
for(int divisor = 0; divisor < SOME_PRIMES.length && value < SOME_PRIMES[divisor]; divisor++) {
numbers.add(boxer.apply(value - SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value + SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value * SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value / SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value | SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value & SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value ^ SOME_PRIMES[divisor]));
}
}
numbers.addAll(Arrays.asList(extras));
return (N[]) numbers.toArray(new Number[numbers.size()]);
}
@Override
public final <U> Stream<U> mapToObj(LongFunction<? extends U> mapper) {
Objects.requireNonNull(mapper);
return new ReferencePipeline.StatelessOp<Long, U>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Long> opWrapSink(int flags, Sink<U> sink) {
return new Sink.ChainedLong<U>(sink) {
@Override
public void accept(long t) {
downstream.accept(mapper.apply(t));
}
};
}
};
}
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
return new Sink.ChainedLong<Long>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(long t) {
try (LongStream result = mapper.apply(t)) {
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
if (result != null)
result.sequential().forEach(i -> downstream.accept(i));
}
}
};
}
};
}
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
Objects.requireNonNull(mapper);
return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
return new Sink.ChainedLong<Long>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(long t) {
try (LongStream result = mapper.apply(t)) {
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
if (result != null)
result.sequential().forEach(i -> downstream.accept(i));
}
}
};
}
};
}
@Override
public final LongStream flatMap(LongFunction<? extends LongStream> mapper) {
return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
return new Sink.ChainedLong<Long>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(long t) {
try (LongStream result = mapper.apply(t)) {
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
if (result != null)
result.sequential().forEach(i -> downstream.accept(i));
}
}
};
}
};
}
@Override
public final <U> Stream<U> mapToObj(LongFunction<? extends U> mapper) {
Objects.requireNonNull(mapper);
return new ReferencePipeline.StatelessOp<Long, U>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Long> opWrapSink(int flags, Sink<U> sink) {
return new Sink.ChainedLong<U>(sink) {
@Override
public void accept(long t) {
downstream.accept(mapper.apply(t));
}
};
}
};
}
public <N extends Number> N[] numberProvider(LongFunction<N> boxer, int bits, N... extras) {
List<N> numbers = new ArrayList<>();
for(int bitmag = 0; bitmag < bits; bitmag++) {
long value = 1L << bitmag;
numbers.add(boxer.apply(value));
numbers.add(boxer.apply(value - 1));
numbers.add(boxer.apply(value + 1));
numbers.add(boxer.apply(-value));
for(int divisor = 0; divisor < SOME_PRIMES.length && value < SOME_PRIMES[divisor]; divisor++) {
numbers.add(boxer.apply(value - SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value + SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value * SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value / SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value | SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value & SOME_PRIMES[divisor]));
numbers.add(boxer.apply(value ^ SOME_PRIMES[divisor]));
}
}
numbers.addAll(Arrays.asList(extras));
return (N[]) numbers.toArray(new Number[numbers.size()]);
}
/**
* A new request arrives, create it with {@link #nextSeqNum}
* and then try sending it to the server.
*
* @param requestConstructor use seqNum to create a new request.
* @return the new request.
*/
public synchronized REQUEST submitNewRequest(
LongFunction<REQUEST> requestConstructor, Consumer<REQUEST> sendMethod) {
if (!requests.isEmpty()) {
Preconditions.assertTrue(nextSeqNum == requests.lastSeqNum() + 1,
() -> "nextSeqNum=" + nextSeqNum + " but " + this);
}
final long seqNum = nextSeqNum++;
final REQUEST r = requestConstructor.apply(seqNum);
if (exception != null) {
alreadyClosed(r, exception);
return r;
}
requests.putNewRequest(r);
final boolean submitted = sendOrDelayRequest(r, sendMethod);
LOG.debug("{}: submitting a new request {} in {}? {}",
requests.getName(), r, this, submitted? "submitted": "delayed");
return r;
}
/**
* Creates an LONG function that wraps to function provided
* @param function the function to wrap
* @param <O> the output type
* @return the newly created function wrapper
*/
public static <O> Function2<Long,O> fromLong(LongFunction<O> function) {
return new Function2<Long,O>(FunctionStyle.LONG) {
@Override
public final O apply(long input) {
return function.apply(input);
}
};
}
private <U> Stream<U> mapToObj(LongFunction<? extends U> mapper, int opFlags) {
return new ReferencePipeline.StatelessOp<Long, U>(this, StreamShape.LONG_VALUE, opFlags) {
@Override
Sink<Long> opWrapSink(int flags, Sink<U> sink) {
return new Sink.ChainedLong<U>(sink) {
@Override
public void accept(long t) {
downstream.accept(mapper.apply(t));
}
};
}
};
}