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 File: PrimitiveSumMinMaxTest.java    From dragonwell8_jdk with 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 File: PrimitiveSumMinMaxTest.java    From openjdk-jdk8u with 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 File: PrimitiveSumMinMaxTest.java    From TencentKona-8 with 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 File: PrimitiveSumMinMaxTest.java    From jdk8u60 with 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 File: Serial.java    From dragonwell8_jdk with 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 #6
Source File: ConcurrentMapTest.java    From java-tutorial with 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 #7
Source File: ArrayPrefixHelpers.java    From jdk8u60 with 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 #8
Source File: ReduceOps.java    From dragonwell8_jdk 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 #9
Source File: ArrayPrefixHelpers.java    From jdk8u60 with 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 #10
Source File: ParallelPrefix.java    From jdk8u60 with 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 #11
Source File: Serial.java    From jdk8u60 with 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 #12
Source File: ArrayPrefixHelpers.java    From jdk1.8-source-analysis with 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 #13
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 #14
Source File: ArrayPrefixHelpers.java    From JDKSourceCode1.8 with 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 #15
Source File: ReduceOps.java    From jdk1.8-source-analysis with 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 #16
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 #17
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 #18
Source File: Serial.java    From native-obfuscator with 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 #19
Source File: ArrayPrefixHelpers.java    From dragonwell8_jdk with 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 #20
Source File: ArrayPrefixHelpers.java    From dragonwell8_jdk with 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 #21
Source File: Serial.java    From TencentKona-8 with 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 #22
Source File: ParallelPrefix.java    From TencentKona-8 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 #23
Source File: ParallelPrefix.java    From TencentKona-8 with 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 #24
Source File: ArrayPrefixHelpers.java    From TencentKona-8 with 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 #25
Source File: ArrayPrefixHelpers.java    From TencentKona-8 with 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 #26
Source File: ArrayPrefixHelpers.java    From openjdk-jdk8u with 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 #27
Source File: ParallelPrefix.java    From openjdk-jdk8u 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 #28
Source File: LongPipeline.java    From TencentKona-8 with GNU General Public License v2.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 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 #30
Source File: LongPipeline.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
@Override
public final OptionalLong reduce(LongBinaryOperator op) {
    return evaluate(ReduceOps.makeLong(op));
}