下面列出了怎么用java.util.function.LongBinaryOperator的API类实例代码及写法,或者点击链接到github查看源代码。
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));
}
}
}
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));
}
}
}
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));
}
}
}
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));
}
}
}
/**
* 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();
}
};
}
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;
}
/**
* 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());
}
/**
* 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();
}
};
}
@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));
}
/** 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;
}
/** 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;
}
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");
}
/** 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;
}
/** 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;
}
/** 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;
}
@DataProvider
public static Object[][] longSet(){
return genericData(size -> LongStream.range(0, size).toArray(),
new LongBinaryOperator[]{
Long::sum,
Long::min});
}
@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));
}
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");
}
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");
}
/**
* 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();
}
};
}
/** 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;
}
/** 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;
}
@DataProvider
public static Object[][] longSet(){
return genericData(size -> LongStream.range(0, size).toArray(),
new LongBinaryOperator[]{
Long::sum,
Long::min});
}
/**
* 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();
}
};
}
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");
}
/** 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;
}
@Override
public final long reduce(long identity, LongBinaryOperator op) {
return evaluate(ReduceOps.makeLong(identity, op));
}
@Override
public final OptionalLong reduce(LongBinaryOperator op) {
return evaluate(ReduceOps.makeLong(op));
}
/**
* 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();
}
};
}