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

The following examples show how to use java.util.function.LongBinaryOperator#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: ReduceOps.java    From desugar_jdk_libs with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a {@code TerminalOp} that implements a functional reduce on
 * {@code long} values.
 *
 * @param identity the identity for the combining function
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Long, Long>
makeLong(long identity, LongBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Long, Long, ReducingSink>, Sink.OfLong {
        private long state;

        @Override
        public void begin(long size) {
            state = identity;
        }

        @Override
        public void accept(long t) {
            state = operator.applyAsLong(state, t);
        }

        @Override
        public Long get() {
            return state;
        }

        @Override
        public void combine(ReducingSink other) {
            accept(other.state);
        }
    }
    return new ReduceOp<Long, Long, ReducingSink>(StreamShape.LONG_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example 2
Source File: ReduceOps.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a {@code TerminalOp} that implements a functional reduce on
 * {@code long} values.
 *
 * @param identity the identity for the combining function
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Long, Long>
makeLong(long identity, LongBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Long, Long, ReducingSink>, Sink.OfLong {
        private long state;

        @Override
        public void begin(long size) {
            state = identity;
        }

        @Override
        public void accept(long t) {
            state = operator.applyAsLong(state, t);
        }

        @Override
        public Long get() {
            return state;
        }

        @Override
        public void combine(ReducingSink other) {
            accept(other.state);
        }
    }
    return new ReduceOp<Long, Long, ReducingSink>(StreamShape.LONG_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example 3
Source File: ParallelPrefix.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="longSet")
public void testParallelPrefixForLong(long[] data, int fromIndex, int toIndex, LongBinaryOperator op) {
    long[] sequentialResult = data.clone();
    for (int index = fromIndex + 1; index < toIndex; index++) {
        sequentialResult[index ] = op.applyAsLong(sequentialResult[index  - 1], sequentialResult[index]);
    }

    long[] parallelResult = data.clone();
    Arrays.parallelPrefix(parallelResult, fromIndex, toIndex, op);
    assertEquals(parallelResult, sequentialResult);

    long[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex);
    Arrays.parallelPrefix(parallelRangeResult, op);
    assertEquals(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex));
}
 
Example 4
Source File: NodeEvaluator.java    From gyro with Apache License 2.0 5 votes vote down vote up
private static Object doArithmetic(
    Object left,
    Object right,
    DoubleBinaryOperator doubleOperator,
    LongBinaryOperator longOperator) {
    if (left == null || right == null) {
        throw new GyroException("Can't do arithmetic with a null!");
    }

    Number leftNumber = NumberUtils.createNumber(left.toString());

    if (leftNumber == null) {
        throw new GyroException(String.format(
            "Can't do arithmetic on @|bold %s|@, an instance of @|bold %s|@, because it's not a number!",
            left,
            left.getClass().getName()));
    }

    Number rightNumber = NumberUtils.createNumber(right.toString());

    if (rightNumber == null) {
        throw new GyroException(String.format(
            "Can't do arithmetic on @|bold %s|@, an instance of @|bold %s|@, because it's not a number!",
            right,
            right.getClass().getName()));
    }

    if (leftNumber instanceof Float
        || leftNumber instanceof Double
        || rightNumber instanceof Float
        || rightNumber instanceof Double) {

        return doubleOperator.applyAsDouble(leftNumber.doubleValue(), rightNumber.doubleValue());

    } else {
        return longOperator.applyAsLong(leftNumber.longValue(), rightNumber.longValue());
    }
}
 
Example 5
Source File: ReduceOps.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a {@code TerminalOp} that implements a functional reduce on
 * {@code long} values.
 *
 * @param identity the identity for the combining function
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Long, Long>
makeLong(long identity, LongBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Long, Long, ReducingSink>, Sink.OfLong {
        private long state;

        @Override
        public void begin(long size) {
            state = identity;
        }

        @Override
        public void accept(long t) {
            state = operator.applyAsLong(state, t);
        }

        @Override
        public Long get() {
            return state;
        }

        @Override
        public void combine(ReducingSink other) {
            accept(other.state);
        }
    }
    return new ReduceOp<Long, Long, ReducingSink>(StreamShape.LONG_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example 6
Source File: ReduceOps.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs a {@code TerminalOp} that implements a functional reduce on
 * {@code long} values, producing an optional long result.
 *
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Long, OptionalLong>
makeLong(LongBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Long, OptionalLong, ReducingSink>, Sink.OfLong {
        private boolean empty;
        private long state;

        public void begin(long size) {
            empty = true;
            state = 0;
        }

        @Override
        public void accept(long t) {
            if (empty) {
                empty = false;
                state = t;
            }
            else {
                state = operator.applyAsLong(state, t);
            }
        }

        @Override
        public OptionalLong get() {
            return empty ? OptionalLong.empty() : OptionalLong.of(state);
        }

        @Override
        public void combine(ReducingSink other) {
            if (!other.empty)
                accept(other.state);
        }
    }
    return new ReduceOp<Long, OptionalLong, ReducingSink>(StreamShape.LONG_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example 7
Source File: AtomicLong.java    From jdk8u60 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 to the current and given values,
 * 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.  The function
 * is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the updated value
 * @since 1.8
 */
public final long accumulateAndGet(long x,
                                   LongBinaryOperator accumulatorFunction) {
    long prev, next;
    do {
        prev = get();
        next = accumulatorFunction.applyAsLong(prev, x);
    } while (!compareAndSet(prev, next));
    return next;
}
 
Example 8
Source File: AtomicLongArray.java    From TencentKona-8 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 to the current and
 * given values, 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.  The function is
 * applied with the current value at index {@code i} as its first
 * argument, and the given update as the second argument.
 *
 * @param i the index
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the updated value
 * @since 1.8
 */
public final long accumulateAndGet(int i, long x,
                                  LongBinaryOperator accumulatorFunction) {
    long offset = checkedByteOffset(i);
    long prev, next;
    do {
        prev = getRaw(offset);
        next = accumulatorFunction.applyAsLong(prev, x);
    } while (!compareAndSetRaw(offset, prev, next));
    return next;
}
 
Example 9
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 to the
 * current and given values, 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.  The
 * function is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param obj An object whose field to get and set
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the updated value
 * @since 1.8
 */
public final long accumulateAndGet(T obj, long x,
                                   LongBinaryOperator accumulatorFunction) {
    long prev, next;
    do {
        prev = get(obj);
        next = accumulatorFunction.applyAsLong(prev, x);
    } while (!compareAndSet(obj, prev, next));
    return next;
}
 
Example 10
Source File: AtomicLongArray.java    From dragonwell8_jdk 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 to the current and
 * given values, 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.  The function is
 * applied with the current value at index {@code i} as its first
 * argument, and the given update as the second argument.
 *
 * @param i the index
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the previous value
 * @since 1.8
 */
public final long getAndAccumulate(int i, long x,
                                  LongBinaryOperator accumulatorFunction) {
    long offset = checkedByteOffset(i);
    long prev, next;
    do {
        prev = getRaw(offset);
        next = accumulatorFunction.applyAsLong(prev, x);
    } while (!compareAndSetRaw(offset, prev, next));
    return prev;
}
 
Example 11
Source File: AtomicLong.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Atomically updates the current value with the results of
 * applying the given function to the current and given values,
 * 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.  The function
 * is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the previous value
 * @since 1.8
 */
public final long getAndAccumulate(long x,
                                   LongBinaryOperator accumulatorFunction) {
    long prev, next;
    do {
        prev = get();
        next = accumulatorFunction.applyAsLong(prev, x);
    } while (!compareAndSet(prev, next));
    return prev;
}
 
Example 12
Source File: AtomicLong.java    From jdk8u60 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 to the current and given values,
 * 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.  The function
 * is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the previous value
 * @since 1.8
 */
public final long getAndAccumulate(long x,
                                   LongBinaryOperator accumulatorFunction) {
    long prev, next;
    do {
        prev = get();
        next = accumulatorFunction.applyAsLong(prev, x);
    } while (!compareAndSet(prev, next));
    return prev;
}
 
Example 13
Source File: AtomicLong.java    From TencentKona-8 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 to the current and given values,
 * 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.  The function
 * is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the previous value
 * @since 1.8
 */
public final long getAndAccumulate(long x,
                                   LongBinaryOperator accumulatorFunction) {
    long prev, next;
    do {
        prev = get();
        next = accumulatorFunction.applyAsLong(prev, x);
    } while (!compareAndSet(prev, next));
    return prev;
}
 
Example 14
Source File: AtomicLong.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Atomically updates the current value with the results of
 * applying the given function to the current and given values,
 * 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.  The function
 * is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the updated value
 * @since 1.8
 */
public final long accumulateAndGet(long x,
                                   LongBinaryOperator accumulatorFunction) {
    long prev, next;
    do {
        prev = get();
        next = accumulatorFunction.applyAsLong(prev, x);
    } while (!compareAndSet(prev, next));
    return next;
}
 
Example 15
Source File: DesugarAtomicLong.java    From desugar_jdk_libs 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 to the current and given values,
 * 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.  The function
 * is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the previous value
 * @since 1.8
 */
// For desugar: made static so it can exist outside original class
public static long getAndAccumulate(AtomicLong atomic, long x,
                                    LongBinaryOperator accumulatorFunction) {
    long prev, next;
    do {
        prev = atomic.get();
        next = accumulatorFunction.applyAsLong(prev, x);
    } while (!atomic.compareAndSet(prev, next));
    return prev;
}
 
Example 16
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 to the current and
 * given values, 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.  The function is
 * applied with the current value at index {@code i} as its first
 * argument, and the given update as the second argument.
 *
 * @param i the index
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the updated value
 * @since 1.8
 */
public final long accumulateAndGet(int i, long x,
                                  LongBinaryOperator accumulatorFunction) {
    long offset = checkedByteOffset(i);
    long prev, next;
    do {
        prev = getRaw(offset);
        next = accumulatorFunction.applyAsLong(prev, x);
    } while (!compareAndSetRaw(offset, prev, next));
    return next;
}
 
Example 17
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 to the
 * current and given values, 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.  The
 * function is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param obj An object whose field to get and set
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the previous value
 * @since 1.8
 */
public final long getAndAccumulate(T obj, long x,
                                   LongBinaryOperator accumulatorFunction) {
    long prev, next;
    do {
        prev = get(obj);
        next = accumulatorFunction.applyAsLong(prev, x);
    } while (!compareAndSet(obj, prev, next));
    return prev;
}
 
Example 18
Source File: AtomicLong.java    From TencentKona-8 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 to the current and given values,
 * 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.  The function
 * is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the updated value
 * @since 1.8
 */
public final long accumulateAndGet(long x,
                                   LongBinaryOperator accumulatorFunction) {
    long prev, next;
    do {
        prev = get();
        next = accumulatorFunction.applyAsLong(prev, x);
    } while (!compareAndSet(prev, next));
    return next;
}
 
Example 19
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 to the
 * current and given values, 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.  The
 * function is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param obj An object whose field to get and set
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the updated value
 * @since 1.8
 */
public final long accumulateAndGet(T obj, long x,
                                   LongBinaryOperator accumulatorFunction) {
    long prev, next;
    do {
        prev = get(obj);
        next = accumulatorFunction.applyAsLong(prev, x);
    } while (!compareAndSet(obj, prev, next));
    return next;
}
 
Example 20
Source File: DesugarAtomicLong.java    From desugar_jdk_libs 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 to the current and given values,
 * 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.  The function
 * is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the updated value
 * @since 1.8
 */
// For desugar: made static so it can exist outside original class
public static long accumulateAndGet(AtomicLong atomic, long x,
                                    LongBinaryOperator accumulatorFunction) {
    long prev, next;
    do {
        prev = atomic.get();
        next = accumulatorFunction.applyAsLong(prev, x);
    } while (!atomic.compareAndSet(prev, next));
    return next;
}