下面列出了java.util.function.LongToIntFunction#java.util.function.LongUnaryOperator 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public final LongStream map(LongUnaryOperator mapper) {
Objects.requireNonNull(mapper);
return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
return new Sink.ChainedLong<Long>(sink) {
@Override
public void accept(long t) {
downstream.accept(mapper.applyAsLong(t));
}
};
}
};
}
@Override
public final LongStream map(LongUnaryOperator mapper) {
Objects.requireNonNull(mapper);
return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
return new Sink.ChainedLong<Long>(sink) {
@Override
public void accept(long t) {
downstream.accept(mapper.applyAsLong(t));
}
};
}
};
}
/**
* Returns an infinite sequential ordered {@code LongStream} produced by iterative
* application of a function {@code f} to an initial element {@code seed},
* producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
* {@code f(f(seed))}, etc.
*
* <p>The first element (position {@code 0}) in the {@code LongStream} will
* be the provided {@code seed}. For {@code n > 0}, the element at position
* {@code n}, will be the result of applying the function {@code f} to the
* element at position {@code n - 1}.
*
* @param seed the initial element
* @param f a function to be applied to the previous element to produce
* a new element
* @return a new sequential {@code LongStream}
*/
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
Objects.requireNonNull(f);
final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
long t = seed;
@Override
public boolean hasNext() {
return true;
}
@Override
public long nextLong() {
long v = t;
t = f.applyAsLong(t);
return v;
}
};
return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
iterator,
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
@Override
public final LongStream map(LongUnaryOperator mapper) {
Objects.requireNonNull(mapper);
return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
return new Sink.ChainedLong<Long>(sink) {
@Override
public void accept(long t) {
downstream.accept(mapper.applyAsLong(t));
}
};
}
};
}
@Override
public final LongStream map(LongUnaryOperator mapper) {
Objects.requireNonNull(mapper);
return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
return new Sink.ChainedLong<Long>(sink) {
@Override
public void accept(long t) {
downstream.accept(mapper.applyAsLong(t));
}
};
}
};
}
@Override
public final LongStream map(LongUnaryOperator mapper) {
Objects.requireNonNull(mapper);
return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
return new Sink.ChainedLong<Long>(sink) {
@Override
public void accept(long t) {
downstream.accept(mapper.applyAsLong(t));
}
};
}
};
}
/**
* Returns an infinite sequential ordered {@code LongStream} produced by iterative
* application of a function {@code f} to an initial element {@code seed},
* producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
* {@code f(f(seed))}, etc.
*
* <p>The first element (position {@code 0}) in the {@code LongStream} will
* be the provided {@code seed}. For {@code n > 0}, the element at position
* {@code n}, will be the result of applying the function {@code f} to the
* element at position {@code n - 1}.
*
* @param seed the initial element
* @param f a function to be applied to to the previous element to produce
* a new element
* @return a new sequential {@code LongStream}
*/
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
Objects.requireNonNull(f);
final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
long t = seed;
@Override
public boolean hasNext() {
return true;
}
@Override
public long nextLong() {
long v = t;
t = f.applyAsLong(t);
return v;
}
};
return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
iterator,
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
/**
* Returns an infinite sequential ordered {@code LongStream} produced by iterative
* application of a function {@code f} to an initial element {@code seed},
* producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
* {@code f(f(seed))}, etc.
*
* <p>The first element (position {@code 0}) in the {@code LongStream} will
* be the provided {@code seed}. For {@code n > 0}, the element at position
* {@code n}, will be the result of applying the function {@code f} to the
* element at position {@code n - 1}.
*
* @param seed the initial element
* @param f a function to be applied to to the previous element to produce
* a new element
* @return a new sequential {@code LongStream}
*/
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
Objects.requireNonNull(f);
final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
long t = seed;
@Override
public boolean hasNext() {
return true;
}
@Override
public long nextLong() {
long v = t;
t = f.applyAsLong(t);
return v;
}
};
return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
iterator,
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
/**
* Returns an infinite sequential ordered {@code LongStream} produced by iterative
* application of a function {@code f} to an initial element {@code seed},
* producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
* {@code f(f(seed))}, etc.
*
* <p>The first element (position {@code 0}) in the {@code LongStream} will
* be the provided {@code seed}. For {@code n > 0}, the element at position
* {@code n}, will be the result of applying the function {@code f} to the
* element at position {@code n - 1}.
*
* @param seed the initial element
* @param f a function to be applied to to the previous element to produce
* a new element
* @return a new sequential {@code LongStream}
*/
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
Objects.requireNonNull(f);
final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
long t = seed;
@Override
public boolean hasNext() {
return true;
}
@Override
public long nextLong() {
long v = t;
t = f.applyAsLong(t);
return v;
}
};
return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
iterator,
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
/**
* Returns an infinite sequential ordered {@code LongStream} produced by iterative
* application of a function {@code f} to an initial element {@code seed},
* producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
* {@code f(f(seed))}, etc.
*
* <p>The first element (position {@code 0}) in the {@code LongStream} will
* be the provided {@code seed}. For {@code n > 0}, the element at position
* {@code n}, will be the result of applying the function {@code f} to the
* element at position {@code n - 1}.
*
* @param seed the initial element
* @param f a function to be applied to the previous element to produce
* a new element
* @return a new sequential {@code LongStream}
*/
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
Objects.requireNonNull(f);
final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
long t = seed;
@Override
public boolean hasNext() {
return true;
}
@Override
public long nextLong() {
long v = t;
t = f.applyAsLong(t);
return v;
}
};
return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
iterator,
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
/**
* Returns an infinite sequential ordered {@code LongStream} produced by iterative
* application of a function {@code f} to an initial element {@code seed},
* producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
* {@code f(f(seed))}, etc.
*
* <p>The first element (position {@code 0}) in the {@code LongStream} will
* be the provided {@code seed}. For {@code n > 0}, the element at position
* {@code n}, will be the result of applying the function {@code f} to the
* element at position {@code n - 1}.
*
* <p>The action of applying {@code f} for one element
* <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a>
* the action of applying {@code f} for subsequent elements. For any given
* element the action may be performed in whatever thread the library
* chooses.
*
* @param seed the initial element
* @param f a function to be applied to the previous element to produce
* a new element
* @return a new sequential {@code LongStream}
*/
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
Objects.requireNonNull(f);
Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE,
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
long prev;
boolean started;
@Override
public boolean tryAdvance(LongConsumer action) {
Objects.requireNonNull(action);
long t;
if (started)
t = f.applyAsLong(prev);
else {
t = seed;
started = true;
}
action.accept(prev = t);
return true;
}
};
return StreamSupport.longStream(spliterator, false);
}
@Override
public final LongStream map(LongUnaryOperator mapper) {
Objects.requireNonNull(mapper);
return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
return new Sink.ChainedLong<Long>(sink) {
@Override
public void accept(long t) {
downstream.accept(mapper.applyAsLong(t));
}
};
}
};
}
/**
* Returns an infinite sequential ordered {@code LongStream} produced by iterative
* application of a function {@code f} to an initial element {@code seed},
* producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
* {@code f(f(seed))}, etc.
*
* <p>The first element (position {@code 0}) in the {@code LongStream} will
* be the provided {@code seed}. For {@code n > 0}, the element at position
* {@code n}, will be the result of applying the function {@code f} to the
* element at position {@code n - 1}.
*
* @param seed the initial element
* @param f a function to be applied to to the previous element to produce
* a new element
* @return a new sequential {@code LongStream}
*/
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
Objects.requireNonNull(f);
final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
long t = seed;
@Override
public boolean hasNext() {
return true;
}
@Override
public long nextLong() {
long v = t;
t = f.applyAsLong(t);
return v;
}
};
return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
iterator,
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
/** Read from the provided offset a size of the provided length, assuming this is enough bytes. */
private static int extractSizeFromLongItem(
final LongUnaryOperator getter, final long offset, final int sizeLength) {
if (sizeLength > 4) {
throw new RLPException(
"RLP item at offset "
+ offset
+ " with size value consuming "
+ sizeLength
+ " bytes exceeds max supported size of "
+ Integer.MAX_VALUE);
}
long res = 0;
int shift = 0;
for (int i = 0; i < sizeLength; i++) {
res |= (getter.applyAsLong(offset + (sizeLength - 1) - i) & 0xFF) << shift;
shift += 8;
}
try {
return Math.toIntExact(res);
} catch (final ArithmeticException e) {
throw new RLPException(
"RLP item at offset "
+ offset
+ " with size value consuming "
+ sizeLength
+ " bytes exceeds max supported size of "
+ Integer.MAX_VALUE,
e);
}
}
static RLPElementMetadata rlpElementMetadata(
final LongUnaryOperator byteGetter, final long size, final long elementStart) {
final int prefix = Math.toIntExact(byteGetter.applyAsLong(elementStart)) & 0xFF;
final Kind kind = Kind.of(prefix);
long payloadStart = 0;
int payloadSize = 0;
switch (kind) {
case BYTE_ELEMENT:
payloadStart = elementStart;
payloadSize = 1;
break;
case SHORT_ELEMENT:
payloadStart = elementStart + 1;
payloadSize = prefix - 0x80;
break;
case LONG_ELEMENT:
final int sizeLengthElt = prefix - 0xb7;
payloadStart = elementStart + 1 + sizeLengthElt;
payloadSize = readLongSize(byteGetter, size, elementStart, sizeLengthElt);
break;
case SHORT_LIST:
payloadStart = elementStart + 1;
payloadSize = prefix - 0xc0;
break;
case LONG_LIST:
final int sizeLengthList = prefix - 0xf7;
payloadStart = elementStart + 1 + sizeLengthList;
payloadSize = readLongSize(byteGetter, size, elementStart, sizeLengthList);
break;
}
return new RLPElementMetadata(kind, elementStart, payloadStart, payloadSize);
}
/** The size of the item payload for a "long" item, given the length in bytes of the said size. */
private static int readLongSize(
final LongUnaryOperator byteGetter,
final long sizeOfRlpEncodedByteString,
final long item,
final int sizeLength) {
// We will read sizeLength bytes from item + 1. There must be enough bytes for this or the input
// is corrupted.
if (sizeOfRlpEncodedByteString - (item + 1) < sizeLength) {
throw new CorruptedRLPInputException(
String.format(
"Invalid RLP item: value of size %d has not enough bytes to read the %d "
+ "bytes payload size",
sizeOfRlpEncodedByteString, sizeLength));
}
// That size (which is at least 1 byte by construction) shouldn't have leading zeros.
if (byteGetter.applyAsLong(item + 1) == 0) {
throw new MalformedRLPInputException("Malformed RLP item: size of payload has leading zeros");
}
final int res = RLPDecodingHelpers.extractSizeFromLongItem(byteGetter, item + 1, sizeLength);
// We should not have had the size written separately if it was less than 56 bytes long.
if (res < 56) {
throw new MalformedRLPInputException(
String.format("Malformed RLP item: written as a long item, but size %d < 56 bytes", res));
}
return res;
}
private static Block extractDate(Block block, LongUnaryOperator function)
{
BlockBuilder builder = INTEGER.createFixedSizeBlockBuilder(block.getPositionCount());
for (int position = 0; position < block.getPositionCount(); position++) {
if (block.isNull(position)) {
builder.appendNull();
continue;
}
long value = DATE.getLong(block, position);
value = function.applyAsLong(value);
INTEGER.writeLong(builder, value);
}
return builder.build();
}
private static Block extractTimestampWithTimeZone(Block block, LongUnaryOperator function)
{
BlockBuilder builder = INTEGER.createFixedSizeBlockBuilder(block.getPositionCount());
for (int position = 0; position < block.getPositionCount(); position++) {
if (block.isNull(position)) {
builder.appendNull();
continue;
}
long value = unpackMillisUtc(TIMESTAMP_WITH_TIME_ZONE.getLong(block, position));
value = function.applyAsLong(value);
INTEGER.writeLong(builder, value);
}
return builder.build();
}
/**
* Creates a new instance given user-defined sample size and seed methods.
*/
public SampleOperator(IntUnaryOperator sampleSizeFunction, DataSetType<Type> type, Methods sampleMethod, LongUnaryOperator seedFunction) {
super(type, type, true);
this.sampleSizeFunction = sampleSizeFunction;
this.sampleMethod = sampleMethod;
this.seedFunction = seedFunction;
}
/**
* cron任务
*
* @param command 任务
* @param cron 表达式
* @return ScheduledFuture
*/
public ScheduledFuture<Void> scheduleOnCron(Runnable command, String cron) {
start();
LongUnaryOperator operator = new CronExpression(cron).operator();
long deadline = operator.applyAsLong(0);
if (deadline == Long.MIN_VALUE) {
throw Exceptions.fail("cron doesn't have any match in the future");
}
PeriodTask task = new PeriodTask(deadline, command, operator, this);
addTaskInLock(task);
return task;
}
static void assertUpdate(RaftLogIndex index, BiFunction<RaftLogIndex, LongUnaryOperator, Boolean> update,
long oldValue, LongUnaryOperator op, boolean expectUpdate) {
Assert.assertEquals(oldValue, index.get());
final boolean updated = update.apply(index, op);
Assert.assertEquals(expectUpdate, updated);
Assert.assertEquals(expectUpdate? op.applyAsLong(oldValue): oldValue, index.get());
}
/**
* Creates a new instance.
*/
public FlinkSampleOperator(IntUnaryOperator sampleSizeFunction, DataSetType<Type> type, LongUnaryOperator seedFunction) {
super(sampleSizeFunction, type, Methods.RANDOM, seedFunction);
}
/**
* Creates a new instance.
*/
public SparkRandomPartitionSampleOperator(IntUnaryOperator sampleSizeFunction, DataSetType<Type> type, LongUnaryOperator seedFunction) {
super(sampleSizeFunction, type, Methods.RANDOM, seedFunction);
}
/**
* Creates a new instance.
*/
public JavaReservoirSampleOperator(IntUnaryOperator sampleSizeFunction, DataSetType<Type> type, LongUnaryOperator seed) {
super(sampleSizeFunction, type, Methods.RESERVOIR, seed);
}
public LongUnaryOperator operator() {
return new CronOperator(this);
}
PeriodTask(long deadline, Runnable runnable, LongUnaryOperator operator, WheelTimer timer) {
super(deadline, runnable);
this.operator = operator;
this.timer = timer;
}
@Nonnull
public RoleData updatePermissions(@Nonnull final LongUnaryOperator updater) {
return permissions(updater.applyAsLong(permissions == null ? 0 : permissions));
}
@Override
public LongStream map(LongUnaryOperator mapper) {
return s.map(mapper);
}
/**
* Atomically updates the current value with the results of
* applying the given function, returning the previous value. The
* function should be side-effect-free, since it may be re-applied
* when attempted updates fail due to contention among threads.
*
* @param updateFunction a side-effect-free function
* @return the previous value
* @since 1.8
*/
public final long getAndUpdate(LongUnaryOperator updateFunction) {
long prev, next;
do {
prev = get();
next = updateFunction.applyAsLong(prev);
} while (!compareAndSet(prev, next));
return prev;
}
/**
* Atomically updates the current value with the results of
* applying the given function, returning the updated value. The
* function should be side-effect-free, since it may be re-applied
* when attempted updates fail due to contention among threads.
*
* @param updateFunction a side-effect-free function
* @return the updated value
* @since 1.8
*/
public final long updateAndGet(LongUnaryOperator updateFunction) {
long prev, next;
do {
prev = get();
next = updateFunction.applyAsLong(prev);
} while (!compareAndSet(prev, next));
return next;
}