Java Code Examples for java.util.function.LongUnaryOperator#applyAsLong()

The following examples show how to use java.util.function.LongUnaryOperator#applyAsLong() . 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: LongStream.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: PartitionTransforms.java    From presto with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 3
Source File: PartitionTransforms.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Block extractTimestamp(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 = TIMESTAMP.getLong(block, position);
        value = function.applyAsLong(value);
        INTEGER.writeLong(builder, value);
    }
    return builder.build();
}
 
Example 4
Source File: RLPDecodingHelpers.java    From besu with Apache License 2.0 5 votes vote down vote up
/** 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);
  }
}
 
Example 5
Source File: GenericLongFunction.java    From presto with Apache License 2.0 4 votes vote down vote up
public static long apply(LongUnaryOperator longUnaryOperator, long value)
{
    return longUnaryOperator.applyAsLong(value);
}
 
Example 6
Source File: AtomicLongArray.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the element at index {@code i} 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 i the index
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
public final long getAndUpdate(int i, LongUnaryOperator updateFunction) {
    long offset = checkedByteOffset(i);
    long prev, next;
    do {
        prev = getRaw(offset);
        next = updateFunction.applyAsLong(prev);
    } while (!compareAndSetRaw(offset, prev, next));
    return prev;
}
 
Example 7
Source File: AtomicLongArray.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the element at index {@code i} 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 i the index
 * @param updateFunction a side-effect-free function
 * @return the updated value
 * @since 1.8
 */
public final long updateAndGet(int i, LongUnaryOperator updateFunction) {
    long offset = checkedByteOffset(i);
    long prev, next;
    do {
        prev = getRaw(offset);
        next = updateFunction.applyAsLong(prev);
    } while (!compareAndSetRaw(offset, prev, next));
    return next;
}
 
Example 8
Source File: AtomicLongFieldUpdater.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Atomically updates the field of the given object managed by this updater
 * 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 obj An object whose field to get and set
 * @param updateFunction a side-effect-free function
 * @return the updated value
 * @since 1.8
 */
public final long updateAndGet(T obj, LongUnaryOperator updateFunction) {
    long prev, next;
    do {
        prev = get(obj);
        next = updateFunction.applyAsLong(prev);
    } while (!compareAndSet(obj, prev, next));
    return next;
}
 
Example 9
Source File: AtomicLongFieldUpdater.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the field of the given object managed by this updater
 * 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 obj An object whose field to get and set
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
public final long getAndUpdate(T obj, LongUnaryOperator updateFunction) {
    long prev, next;
    do {
        prev = get(obj);
        next = updateFunction.applyAsLong(prev);
    } while (!compareAndSet(obj, prev, next));
    return prev;
}
 
Example 10
Source File: AtomicLong.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates (with memory effects as specified by {@link
 * VarHandle#compareAndSet}) 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 = get(), next = 0L;
    for (boolean haveNext = false;;) {
        if (!haveNext)
            next = updateFunction.applyAsLong(prev);
        if (weakCompareAndSetVolatile(prev, next))
            return next;
        haveNext = (prev == (prev = get()));
    }
}
 
Example 11
Source File: AtomicLongFieldUpdater.java    From dragonwell8_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the field of the given object managed by this updater
 * 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 obj An object whose field to get and set
 * @param updateFunction a side-effect-free function
 * @return the updated value
 * @since 1.8
 */
public final long updateAndGet(T obj, LongUnaryOperator updateFunction) {
    long prev, next;
    do {
        prev = get(obj);
        next = updateFunction.applyAsLong(prev);
    } while (!compareAndSet(obj, prev, next));
    return next;
}
 
Example 12
Source File: AtomicLong.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
}
 
Example 13
Source File: AtomicLong.java    From Bytecoder with Apache License 2.0 3 votes vote down vote up
/**
 * Atomically updates (with memory effects as specified by {@link
 * VarHandle#compareAndSet}) 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 = get(), next = 0L;
    for (boolean haveNext = false;;) {
        if (!haveNext)
            next = updateFunction.applyAsLong(prev);
        if (weakCompareAndSetVolatile(prev, next))
            return next;
        haveNext = (prev == (prev = get()));
    }
}
 
Example 14
Source File: AtomicLongArray.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Atomically updates the element at index {@code i} 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 i the index
 * @param updateFunction a side-effect-free function
 * @return the updated value
 * @since 1.8
 */
public final long updateAndGet(int i, LongUnaryOperator updateFunction) {
    long offset = checkedByteOffset(i);
    long prev, next;
    do {
        prev = getRaw(offset);
        next = updateFunction.applyAsLong(prev);
    } while (!compareAndSetRaw(offset, prev, next));
    return next;
}
 
Example 15
Source File: AtomicLongArray.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Atomically updates the element at index {@code i} 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 i the index
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
public final long getAndUpdate(int i, LongUnaryOperator updateFunction) {
    long offset = checkedByteOffset(i);
    long prev, next;
    do {
        prev = getRaw(offset);
        next = updateFunction.applyAsLong(prev);
    } while (!compareAndSetRaw(offset, prev, next));
    return prev;
}
 
Example 16
Source File: AtomicLongFieldUpdater.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the field of the given object managed by this updater
 * 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 obj An object whose field to get and set
 * @param updateFunction a side-effect-free function
 * @return the updated value
 * @since 1.8
 */
public final long updateAndGet(T obj, LongUnaryOperator updateFunction) {
    long prev, next;
    do {
        prev = get(obj);
        next = updateFunction.applyAsLong(prev);
    } while (!compareAndSet(obj, prev, next));
    return next;
}
 
Example 17
Source File: AtomicLongFieldUpdater.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Atomically updates the field of the given object managed by this updater
 * 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 obj An object whose field to get and set
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
public final long getAndUpdate(T obj, LongUnaryOperator updateFunction) {
    long prev, next;
    do {
        prev = get(obj);
        next = updateFunction.applyAsLong(prev);
    } while (!compareAndSet(obj, prev, next));
    return prev;
}
 
Example 18
Source File: AtomicLongFieldUpdater.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the field of the given object managed by this updater
 * 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 obj An object whose field to get and set
 * @param updateFunction a side-effect-free function
 * @return the updated value
 * @since 1.8
 */
public final long updateAndGet(T obj, LongUnaryOperator updateFunction) {
    long prev, next;
    do {
        prev = get(obj);
        next = updateFunction.applyAsLong(prev);
    } while (!compareAndSet(obj, prev, next));
    return next;
}
 
Example 19
Source File: AtomicLongArray.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the element at index {@code i} 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 i the index
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
public final long getAndUpdate(int i, LongUnaryOperator updateFunction) {
    long offset = checkedByteOffset(i);
    long prev, next;
    do {
        prev = getRaw(offset);
        next = updateFunction.applyAsLong(prev);
    } while (!compareAndSetRaw(offset, prev, next));
    return prev;
}
 
Example 20
Source File: AtomicLongFieldUpdater.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the field of the given object managed by this updater
 * 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 obj An object whose field to get and set
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
public final long getAndUpdate(T obj, LongUnaryOperator updateFunction) {
    long prev, next;
    do {
        prev = get(obj);
        next = updateFunction.applyAsLong(prev);
    } while (!compareAndSet(obj, prev, next));
    return prev;
}