类java.util.function.LongBinaryOperator源码实例Demo

下面列出了怎么用java.util.function.LongBinaryOperator的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: dragonwell8_jdk   文件: PrimitiveSumMinMaxTest.java
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));
        }
    }
}
 
源代码2 项目: TencentKona-8   文件: PrimitiveSumMinMaxTest.java
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));
        }
    }
}
 
源代码3 项目: jdk8u60   文件: PrimitiveSumMinMaxTest.java
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));
        }
    }
}
 
源代码4 项目: openjdk-jdk8u   文件: PrimitiveSumMinMaxTest.java
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));
        }
    }
}
 
源代码5 项目: jdk1.8-source-analysis   文件: ReduceOps.java
/**
 * 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();
        }
    };
}
 
源代码6 项目: gyro   文件: NodeEvaluator.java
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());
    }
}
 
/** 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;
}
 
源代码8 项目: java-tutorial   文件: ConcurrentMapTest.java
/**
 * 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());
}
 
源代码9 项目: dragonwell8_jdk   文件: ReduceOps.java
/**
 * 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();
        }
    };
}
 
源代码10 项目: openjdk-jdk8u   文件: ParallelPrefix.java
@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));
}
 
源代码11 项目: dragonwell8_jdk   文件: ArrayPrefixHelpers.java
/** 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;
}
 
源代码12 项目: dragonwell8_jdk   文件: ArrayPrefixHelpers.java
/** 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;
}
 
源代码13 项目: dragonwell8_jdk   文件: Serial.java
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");
}
 
源代码14 项目: openjdk-jdk8u   文件: ArrayPrefixHelpers.java
/** 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;
}
 
源代码15 项目: TencentKona-8   文件: ArrayPrefixHelpers.java
/** 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;
}
 
源代码16 项目: TencentKona-8   文件: ArrayPrefixHelpers.java
/** 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;
}
 
源代码17 项目: TencentKona-8   文件: ParallelPrefix.java
@DataProvider
public static Object[][] longSet(){
    return genericData(size -> LongStream.range(0, size).toArray(),
            new LongBinaryOperator[]{
                Long::sum,
                Long::min});
}
 
源代码18 项目: TencentKona-8   文件: ParallelPrefix.java
@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));
}
 
源代码19 项目: TencentKona-8   文件: Serial.java
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");
}
 
源代码20 项目: native-obfuscator   文件: Serial.java
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");
}
 
源代码21 项目: jdk8u60   文件: ReduceOps.java
/**
 * 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();
        }
    };
}
 
源代码22 项目: jdk8u60   文件: ArrayPrefixHelpers.java
/** 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;
}
 
源代码23 项目: jdk8u60   文件: ArrayPrefixHelpers.java
/** 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;
}
 
源代码24 项目: jdk8u60   文件: ParallelPrefix.java
@DataProvider
public static Object[][] longSet(){
    return genericData(size -> LongStream.range(0, size).toArray(),
            new LongBinaryOperator[]{
                Long::sum,
                Long::min});
}
 
源代码25 项目: desugar_jdk_libs   文件: ReduceOps.java
/**
 * 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();
        }
    };
}
 
源代码26 项目: jdk8u60   文件: Serial.java
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");
}
 
源代码27 项目: JDKSourceCode1.8   文件: ArrayPrefixHelpers.java
/** 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;
}
 
源代码28 项目: jdk1.8-source-analysis   文件: LongPipeline.java
@Override
public final long reduce(long identity, LongBinaryOperator op) {
    return evaluate(ReduceOps.makeLong(identity, op));
}
 
源代码29 项目: jdk1.8-source-analysis   文件: LongPipeline.java
@Override
public final OptionalLong reduce(LongBinaryOperator op) {
    return evaluate(ReduceOps.makeLong(op));
}
 
源代码30 项目: dragonwell8_jdk   文件: ReduceOps.java
/**
 * 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();
        }
    };
}
 
 类所在包
 类方法
 同包方法