java.util.function.LongBinaryOperator Java Examples

The following examples show how to use java.util.function.LongBinaryOperator. 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 Project: dragonwell8_jdk   Author: alibaba   File: PrimitiveSumMinMaxTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void testLongMethods() {
    BinaryOperator<Long> sum1 = Long::sum;
    LongBinaryOperator sum2 = Long::sum;
    BinaryOperator<Long> max1 = Long::max;
    LongBinaryOperator max2 = Long::max;
    BinaryOperator<Long> min1 = Long::min;
    LongBinaryOperator min2 = Long::min;
    Comparator<Long> cmp = Long::compare;

    long[] numbers = { -1, 0, 1, 100, Long.MAX_VALUE, Long.MIN_VALUE };
    for (long i : numbers) {
        for (long j : numbers) {
            assertEquals(i+j, (long) sum1.apply(i, j));
            assertEquals(i+j, sum2.applyAsLong(i, j));
            assertEquals(Math.max(i,j), (long) max1.apply(i, j));
            assertEquals(Math.max(i,j), max2.applyAsLong(i, j));
            assertEquals(Math.min(i,j), (long) min1.apply(i, j));
            assertEquals(Math.min(i,j), min2.applyAsLong(i, j));
            assertEquals(((Long) i).compareTo(j), cmp.compare(i, j));
        }
    }
}
 
Example #2
Source Project: TencentKona-8   Author: Tencent   File: PrimitiveSumMinMaxTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void testLongMethods() {
    BinaryOperator<Long> sum1 = Long::sum;
    LongBinaryOperator sum2 = Long::sum;
    BinaryOperator<Long> max1 = Long::max;
    LongBinaryOperator max2 = Long::max;
    BinaryOperator<Long> min1 = Long::min;
    LongBinaryOperator min2 = Long::min;
    Comparator<Long> cmp = Long::compare;

    long[] numbers = { -1, 0, 1, 100, Long.MAX_VALUE, Long.MIN_VALUE };
    for (long i : numbers) {
        for (long j : numbers) {
            assertEquals(i+j, (long) sum1.apply(i, j));
            assertEquals(i+j, sum2.applyAsLong(i, j));
            assertEquals(Math.max(i,j), (long) max1.apply(i, j));
            assertEquals(Math.max(i,j), max2.applyAsLong(i, j));
            assertEquals(Math.min(i,j), (long) min1.apply(i, j));
            assertEquals(Math.min(i,j), min2.applyAsLong(i, j));
            assertEquals(((Long) i).compareTo(j), cmp.compare(i, j));
        }
    }
}
 
Example #3
Source Project: jdk8u60   Author: chenghanpeng   File: PrimitiveSumMinMaxTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void testLongMethods() {
    BinaryOperator<Long> sum1 = Long::sum;
    LongBinaryOperator sum2 = Long::sum;
    BinaryOperator<Long> max1 = Long::max;
    LongBinaryOperator max2 = Long::max;
    BinaryOperator<Long> min1 = Long::min;
    LongBinaryOperator min2 = Long::min;
    Comparator<Long> cmp = Long::compare;

    long[] numbers = { -1, 0, 1, 100, Long.MAX_VALUE, Long.MIN_VALUE };
    for (long i : numbers) {
        for (long j : numbers) {
            assertEquals(i+j, (long) sum1.apply(i, j));
            assertEquals(i+j, sum2.applyAsLong(i, j));
            assertEquals(Math.max(i,j), (long) max1.apply(i, j));
            assertEquals(Math.max(i,j), max2.applyAsLong(i, j));
            assertEquals(Math.min(i,j), (long) min1.apply(i, j));
            assertEquals(Math.min(i,j), min2.applyAsLong(i, j));
            assertEquals(((Long) i).compareTo(j), cmp.compare(i, j));
        }
    }
}
 
Example #4
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: PrimitiveSumMinMaxTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void testLongMethods() {
    BinaryOperator<Long> sum1 = Long::sum;
    LongBinaryOperator sum2 = Long::sum;
    BinaryOperator<Long> max1 = Long::max;
    LongBinaryOperator max2 = Long::max;
    BinaryOperator<Long> min1 = Long::min;
    LongBinaryOperator min2 = Long::min;
    Comparator<Long> cmp = Long::compare;

    long[] numbers = { -1, 0, 1, 100, Long.MAX_VALUE, Long.MIN_VALUE };
    for (long i : numbers) {
        for (long j : numbers) {
            assertEquals(i+j, (long) sum1.apply(i, j));
            assertEquals(i+j, sum2.applyAsLong(i, j));
            assertEquals(Math.max(i,j), (long) max1.apply(i, j));
            assertEquals(Math.max(i,j), max2.applyAsLong(i, j));
            assertEquals(Math.min(i,j), (long) min1.apply(i, j));
            assertEquals(Math.min(i,j), min2.applyAsLong(i, j));
            assertEquals(((Long) i).compareTo(j), cmp.compare(i, j));
        }
    }
}
 
Example #5
Source Project: jdk1.8-source-analysis   Author: raysonfang   File: ReduceOps.java    License: Apache License 2.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 Project: gyro   Author: perfectsense   File: NodeEvaluator.java    License: 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 #7
Source Project: jdk1.8-source-analysis   Author: raysonfang   File: ArrayPrefixHelpers.java    License: Apache License 2.0 5 votes vote down vote up
/** Root task constructor */
public LongCumulateTask(LongCumulateTask parent,
                        LongBinaryOperator function,
                        long[] array, int lo, int hi) {
    super(parent);
    this.function = function; this.array = array;
    this.lo = this.origin = lo; this.hi = this.fence = hi;
    int p;
    this.threshold =
            (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
            <= MIN_PARTITION ? MIN_PARTITION : p;
}
 
Example #8
Source Project: java-tutorial   Author: jarvisqi   File: ConcurrentMapTest.java    License: MIT License 5 votes vote down vote up
/**
 * LongAccumulator以类型为LongBinaryOperatorlambda表达式构建
 * 而不是仅仅执行加法操作
 */
public void longAccumulatorDemo() {
    ExecutorService executor = Executors.newFixedThreadPool(2);

    LongBinaryOperator op = (x, y) -> 2 * x + y;
    LongAccumulator accumulator = new LongAccumulator(op, 1L);
    IntStream.range(0, 10).forEach(i -> executor.submit(() -> accumulator.accumulate(i)));

    SynchronizationLocks.stopExecutor(executor);
    // => 2539
    System.out.println(accumulator.getThenReset());
}
 
Example #9
Source Project: dragonwell8_jdk   Author: alibaba   File: ReduceOps.java    License: 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 #10
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: ParallelPrefix.java    License: 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 #11
Source Project: dragonwell8_jdk   Author: alibaba   File: ArrayPrefixHelpers.java    License: GNU General Public License v2.0 5 votes vote down vote up
/** Root task constructor */
public LongCumulateTask(LongCumulateTask parent,
                        LongBinaryOperator function,
                        long[] array, int lo, int hi) {
    super(parent);
    this.function = function; this.array = array;
    this.lo = this.origin = lo; this.hi = this.fence = hi;
    int p;
    this.threshold =
            (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
            <= MIN_PARTITION ? MIN_PARTITION : p;
}
 
Example #12
Source Project: dragonwell8_jdk   Author: alibaba   File: ArrayPrefixHelpers.java    License: GNU General Public License v2.0 5 votes vote down vote up
/** Subtask constructor */
LongCumulateTask(LongCumulateTask parent, LongBinaryOperator function,
                 long[] array, int origin, int fence, int threshold,
                 int lo, int hi) {
    super(parent);
    this.function = function; this.array = array;
    this.origin = origin; this.fence = fence;
    this.threshold = threshold;
    this.lo = lo; this.hi = hi;
}
 
Example #13
Source Project: dragonwell8_jdk   Author: alibaba   File: Serial.java    License: GNU General Public License v2.0 5 votes vote down vote up
static void testLongAccumulator() {
    LongBinaryOperator plus = (LongBinaryOperator & Serializable) (x, y) -> x + y;
    LongAccumulator a = new LongAccumulator(plus, -2);
    a.accumulate(34);
    LongAccumulator result = echo(a);
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value");
    a.reset();
    result.reset();
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value after reset");

    checkSerialClassName(a, "java.util.concurrent.atomic.LongAccumulator$SerializationProxy");
}
 
Example #14
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: ArrayPrefixHelpers.java    License: GNU General Public License v2.0 5 votes vote down vote up
/** Root task constructor */
public LongCumulateTask(LongCumulateTask parent,
                        LongBinaryOperator function,
                        long[] array, int lo, int hi) {
    super(parent);
    this.function = function; this.array = array;
    this.lo = this.origin = lo; this.hi = this.fence = hi;
    int p;
    this.threshold =
            (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
            <= MIN_PARTITION ? MIN_PARTITION : p;
}
 
Example #15
Source Project: TencentKona-8   Author: Tencent   File: ArrayPrefixHelpers.java    License: GNU General Public License v2.0 5 votes vote down vote up
/** Root task constructor */
public LongCumulateTask(LongCumulateTask parent,
                        LongBinaryOperator function,
                        long[] array, int lo, int hi) {
    super(parent);
    this.function = function; this.array = array;
    this.lo = this.origin = lo; this.hi = this.fence = hi;
    int p;
    this.threshold =
            (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
            <= MIN_PARTITION ? MIN_PARTITION : p;
}
 
Example #16
Source Project: TencentKona-8   Author: Tencent   File: ArrayPrefixHelpers.java    License: GNU General Public License v2.0 5 votes vote down vote up
/** Subtask constructor */
LongCumulateTask(LongCumulateTask parent, LongBinaryOperator function,
                 long[] array, int origin, int fence, int threshold,
                 int lo, int hi) {
    super(parent);
    this.function = function; this.array = array;
    this.origin = origin; this.fence = fence;
    this.threshold = threshold;
    this.lo = lo; this.hi = hi;
}
 
Example #17
Source Project: TencentKona-8   Author: Tencent   File: ParallelPrefix.java    License: GNU General Public License v2.0 5 votes vote down vote up
@DataProvider
public static Object[][] longSet(){
    return genericData(size -> LongStream.range(0, size).toArray(),
            new LongBinaryOperator[]{
                Long::sum,
                Long::min});
}
 
Example #18
Source Project: TencentKona-8   Author: Tencent   File: ParallelPrefix.java    License: 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 #19
Source Project: TencentKona-8   Author: Tencent   File: Serial.java    License: GNU General Public License v2.0 5 votes vote down vote up
static void testLongAccumulator() {
    LongBinaryOperator plus = (LongBinaryOperator & Serializable) (x, y) -> x + y;
    LongAccumulator a = new LongAccumulator(plus, -2);
    a.accumulate(34);
    LongAccumulator result = echo(a);
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value");
    a.reset();
    result.reset();
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value after reset");

    checkSerialClassName(a, "java.util.concurrent.atomic.LongAccumulator$SerializationProxy");
}
 
Example #20
Source Project: native-obfuscator   Author: radioegor146   File: Serial.java    License: GNU General Public License v3.0 5 votes vote down vote up
static void testLongAccumulator() {
    LongBinaryOperator plus = (LongBinaryOperator & Serializable) (x, y) -> x + y;
    LongAccumulator a = new LongAccumulator(plus, -2);
    a.accumulate(34);
    LongAccumulator result = echo(a);
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value");
    a.reset();
    result.reset();
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value after reset");

    checkSerialClassName(a, "java.util.concurrent.atomic.LongAccumulator$SerializationProxy");
}
 
Example #21
Source Project: jdk8u60   Author: chenghanpeng   File: ReduceOps.java    License: 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 #22
Source Project: jdk8u60   Author: chenghanpeng   File: ArrayPrefixHelpers.java    License: GNU General Public License v2.0 5 votes vote down vote up
/** Root task constructor */
public LongCumulateTask(LongCumulateTask parent,
                        LongBinaryOperator function,
                        long[] array, int lo, int hi) {
    super(parent);
    this.function = function; this.array = array;
    this.lo = this.origin = lo; this.hi = this.fence = hi;
    int p;
    this.threshold =
            (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
            <= MIN_PARTITION ? MIN_PARTITION : p;
}
 
Example #23
Source Project: jdk8u60   Author: chenghanpeng   File: ArrayPrefixHelpers.java    License: GNU General Public License v2.0 5 votes vote down vote up
/** Subtask constructor */
LongCumulateTask(LongCumulateTask parent, LongBinaryOperator function,
                 long[] array, int origin, int fence, int threshold,
                 int lo, int hi) {
    super(parent);
    this.function = function; this.array = array;
    this.origin = origin; this.fence = fence;
    this.threshold = threshold;
    this.lo = lo; this.hi = hi;
}
 
Example #24
Source Project: jdk8u60   Author: chenghanpeng   File: ParallelPrefix.java    License: GNU General Public License v2.0 5 votes vote down vote up
@DataProvider
public static Object[][] longSet(){
    return genericData(size -> LongStream.range(0, size).toArray(),
            new LongBinaryOperator[]{
                Long::sum,
                Long::min});
}
 
Example #25
Source Project: desugar_jdk_libs   Author: google   File: ReduceOps.java    License: 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 #26
Source Project: jdk8u60   Author: chenghanpeng   File: Serial.java    License: GNU General Public License v2.0 5 votes vote down vote up
static void testLongAccumulator() {
    LongBinaryOperator plus = (LongBinaryOperator & Serializable) (x, y) -> x + y;
    LongAccumulator a = new LongAccumulator(plus, -2);
    a.accumulate(34);
    LongAccumulator result = echo(a);
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value");
    a.reset();
    result.reset();
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value after reset");

    checkSerialClassName(a, "java.util.concurrent.atomic.LongAccumulator$SerializationProxy");
}
 
Example #27
Source Project: JDKSourceCode1.8   Author: wupeixuan   File: ArrayPrefixHelpers.java    License: MIT License 5 votes vote down vote up
/** Subtask constructor */
LongCumulateTask(LongCumulateTask parent, LongBinaryOperator function,
                 long[] array, int origin, int fence, int threshold,
                 int lo, int hi) {
    super(parent);
    this.function = function; this.array = array;
    this.origin = origin; this.fence = fence;
    this.threshold = threshold;
    this.lo = lo; this.hi = hi;
}
 
Example #28
Source Project: jdk1.8-source-analysis   Author: raysonfang   File: LongPipeline.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public final long reduce(long identity, LongBinaryOperator op) {
    return evaluate(ReduceOps.makeLong(identity, op));
}
 
Example #29
Source Project: jdk1.8-source-analysis   Author: raysonfang   File: LongPipeline.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public final OptionalLong reduce(LongBinaryOperator op) {
    return evaluate(ReduceOps.makeLong(op));
}
 
Example #30
Source Project: dragonwell8_jdk   Author: alibaba   File: ReduceOps.java    License: 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();
        }
    };
}