Java Code Examples for java.util.OptionalDouble#empty()

The following examples show how to use java.util.OptionalDouble#empty() . 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: DoublePipeline.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @implNote The {@code double} format can represent all
 * consecutive integers in the range -2<sup>53</sup> to
 * 2<sup>53</sup>. If the pipeline has more than 2<sup>53</sup>
 * values, the divisor in the average computation will saturate at
 * 2<sup>53</sup>, leading to additional numerical errors.
 */
@Override
public final OptionalDouble average() {
    /*
     * In the arrays allocated for the collect operation, index 0
     * holds the high-order bits of the running sum, index 1 holds
     * the low-order bits of the sum computed via compensated
     * summation, index 2 holds the number of values seen, index 3
     * holds the simple sum.
     */
    double[] avg = collect(() -> new double[4],
                           (ll, d) -> {
                               ll[2]++;
                               Collectors.sumWithCompensation(ll, d);
                               ll[3] += d;
                           },
                           (ll, rr) -> {
                               Collectors.sumWithCompensation(ll, rr[0]);
                               Collectors.sumWithCompensation(ll, rr[1]);
                               ll[2] += rr[2];
                               ll[3] += rr[3];
                           });
    return avg[2] > 0
        ? OptionalDouble.of(Collectors.computeFinalSum(avg) / avg[2])
        : OptionalDouble.empty();
}
 
Example 2
Source File: DoublePipeline.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @implNote The {@code double} format can represent all
 * consecutive integers in the range -2<sup>53</sup> to
 * 2<sup>53</sup>. If the pipeline has more than 2<sup>53</sup>
 * values, the divisor in the average computation will saturate at
 * 2<sup>53</sup>, leading to additional numerical errors.
 */
@Override
public final OptionalDouble average() {
    /*
     * In the arrays allocated for the collect operation, index 0
     * holds the high-order bits of the running sum, index 1 holds
     * the low-order bits of the sum computed via compensated
     * summation, index 2 holds the number of values seen, index 3
     * holds the simple sum.
     */
    double[] avg = collect(() -> new double[4],
                           (ll, d) -> {
                               ll[2]++;
                               Collectors.sumWithCompensation(ll, d);
                               ll[3] += d;
                           },
                           (ll, rr) -> {
                               Collectors.sumWithCompensation(ll, rr[0]);
                               Collectors.sumWithCompensation(ll, rr[1]);
                               ll[2] += rr[2];
                               ll[3] += rr[3];
                           });
    return avg[2] > 0
        ? OptionalDouble.of(Collectors.computeFinalSum(avg) / avg[2])
        : OptionalDouble.empty();
}
 
Example 3
Source File: DoublePipeline.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @implNote The {@code double} format can represent all
 * consecutive integers in the range -2<sup>53</sup> to
 * 2<sup>53</sup>. If the pipeline has more than 2<sup>53</sup>
 * values, the divisor in the average computation will saturate at
 * 2<sup>53</sup>, leading to additional numerical errors.
 */
@Override
public final OptionalDouble average() {
    /*
     * In the arrays allocated for the collect operation, index 0
     * holds the high-order bits of the running sum, index 1 holds
     * the low-order bits of the sum computed via compensated
     * summation, index 2 holds the number of values seen, index 3
     * holds the simple sum.
     */
    double[] avg = collect(() -> new double[4],
                           (ll, d) -> {
                               ll[2]++;
                               Collectors.sumWithCompensation(ll, d);
                               ll[3] += d;
                           },
                           (ll, rr) -> {
                               Collectors.sumWithCompensation(ll, rr[0]);
                               Collectors.sumWithCompensation(ll, rr[1]);
                               ll[2] += rr[2];
                               ll[3] += rr[3];
                           });
    return avg[2] > 0
        ? OptionalDouble.of(Collectors.computeFinalSum(avg) / avg[2])
        : OptionalDouble.empty();
}
 
Example 4
Source File: BasicDouble.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(groups = "unit")
public void testEmpty() {
    OptionalDouble empty = OptionalDouble.empty();
    OptionalDouble present = OptionalDouble.of(1.0);

    // empty
    assertTrue(empty.equals(empty));
    assertTrue(empty.equals(OptionalDouble.empty()));
    assertTrue(!empty.equals(present));
    assertTrue(0 == empty.hashCode());
    assertTrue(!empty.toString().isEmpty());
    assertTrue(!empty.isPresent());
    empty.ifPresent(v -> { fail(); });
    assertEquals(2.0, empty.orElse(2.0));
    assertEquals(2.0, empty.orElseGet(()-> 2.0));
}
 
Example 5
Source File: DoubleStreamEx.java    From streamex with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the minimum element of this stream according to the provided key
 * extractor function.
 *
 * <p>
 * This is a terminal operation.
 *
 * @param <V> the type of the {@code Comparable} sort key
 * @param keyExtractor a non-interfering, stateless function
 * @return an {@code OptionalDouble} describing the first element of this
 *         stream for which the lowest value was returned by key extractor,
 *         or an empty {@code OptionalDouble} if the stream is empty
 * @since 0.1.2
 */
public <V extends Comparable<? super V>> OptionalDouble minBy(DoubleFunction<V> keyExtractor) {
    ObjDoubleBox<V> result = collect(() -> new ObjDoubleBox<>(null, 0), (box, i) -> {
        V val = Objects.requireNonNull(keyExtractor.apply(i));
        if (box.a == null || box.a.compareTo(val) > 0) {
            box.a = val;
            box.b = i;
        }
    }, (box1, box2) -> {
        if (box2.a != null && (box1.a == null || box1.a.compareTo(box2.a) > 0)) {
            box1.a = box2.a;
            box1.b = box2.b;
        }
    });
    return result.a == null ? OptionalDouble.empty() : OptionalDouble.of(result.b);
}
 
Example 6
Source File: DoublePipeline.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @implNote The {@code double} format can represent all
 * consecutive integers in the range -2<sup>53</sup> to
 * 2<sup>53</sup>. If the pipeline has more than 2<sup>53</sup>
 * values, the divisor in the average computation will saturate at
 * 2<sup>53</sup>, leading to additional numerical errors.
 */
@Override
public final OptionalDouble average() {
    /*
     * In the arrays allocated for the collect operation, index 0
     * holds the high-order bits of the running sum, index 1 holds
     * the low-order bits of the sum computed via compensated
     * summation, index 2 holds the number of values seen, index 3
     * holds the simple sum.
     */
    double[] avg = collect(() -> new double[4],
                           (ll, d) -> {
                               ll[2]++;
                               Collectors.sumWithCompensation(ll, d);
                               ll[3] += d;
                           },
                           (ll, rr) -> {
                               Collectors.sumWithCompensation(ll, rr[0]);
                               Collectors.sumWithCompensation(ll, rr[1]);
                               ll[2] += rr[2];
                               ll[3] += rr[3];
                           });
    return avg[2] > 0
        ? OptionalDouble.of(Collectors.computeFinalSum(avg) / avg[2])
        : OptionalDouble.empty();
}
 
Example 7
Source File: DoubleStreamEx.java    From streamex with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the minimum element of this stream according to the provided key
 * extractor function.
 *
 * <p>
 * This is a terminal operation.
 *
 * @param keyExtractor a non-interfering, stateless function
 * @return an {@code OptionalDouble} describing the first element of this
 *         stream for which the lowest value was returned by key extractor,
 *         or an empty {@code OptionalDouble} if the stream is empty
 * @since 0.1.2
 */
public OptionalDouble minByDouble(DoubleUnaryOperator keyExtractor) {
    double[] result = collect(() -> new double[3], (acc, d) -> {
        double key = keyExtractor.applyAsDouble(d);
        if (acc[2] == 0 || Double.compare(acc[1], key) > 0) {
            acc[0] = d;
            acc[1] = key;
            acc[2] = 1;
        }
    }, (acc1, acc2) -> {
        if (acc2[2] == 1 && (acc1[2] == 0 || Double.compare(acc1[1], acc2[1]) > 0))
            System.arraycopy(acc2, 0, acc1, 0, 3);
    });
    return result[2] == 1 ? OptionalDouble.of(result[0]) : OptionalDouble.empty();
}
 
Example 8
Source File: LongPipeline.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final OptionalDouble average() {
    long[] avg = collect(() -> new long[2],
                         (ll, i) -> {
                             ll[0]++;
                             ll[1] += i;
                         },
                         (ll, rr) -> {
                             ll[0] += rr[0];
                             ll[1] += rr[1];
                         });
    return avg[0] > 0
           ? OptionalDouble.of((double) avg[1] / avg[0])
           : OptionalDouble.empty();
}
 
Example 9
Source File: IntPipeline.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final OptionalDouble average() {
    long[] avg = collect(() -> new long[2],
                         (ll, i) -> {
                             ll[0]++;
                             ll[1] += i;
                         },
                         (ll, rr) -> {
                             ll[0] += rr[0];
                             ll[1] += rr[1];
                         });
    return avg[0] > 0
           ? OptionalDouble.of((double) avg[1] / avg[0])
           : OptionalDouble.empty();
}
 
Example 10
Source File: IntPipeline.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final OptionalDouble average() {
    long[] avg = collect(() -> new long[2],
                         (ll, i) -> {
                             ll[0]++;
                             ll[1] += i;
                         },
                         (ll, rr) -> {
                             ll[0] += rr[0];
                             ll[1] += rr[1];
                         });
    return avg[0] > 0
           ? OptionalDouble.of((double) avg[1] / avg[0])
           : OptionalDouble.empty();
}
 
Example 11
Source File: Configuration.java    From rheem with Apache License 2.0 5 votes vote down vote up
public OptionalDouble getOptionalDoubleProperty(String key) {
    final Optional<String> optionalDouble = this.properties.optionallyProvideFor(key);
    if (optionalDouble.isPresent()) {
        return OptionalDouble.of(Double.valueOf(optionalDouble.get()));
    } else {
        return OptionalDouble.empty();
    }
}
 
Example 12
Source File: BasicDouble.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void testEmptyOrElseThrowNull() throws Throwable {
    OptionalDouble empty = OptionalDouble.empty();

    double got = empty.orElseThrow(null);
}
 
Example 13
Source File: ReduceOps.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs a {@code TerminalOp} that implements a functional reduce on
 * {@code double} values, producing an optional double result.
 *
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Double, OptionalDouble>
makeDouble(DoubleBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Double, OptionalDouble, ReducingSink>, Sink.OfDouble {
        private boolean empty;
        private double state;

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

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

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

        @Override
        public void combine(ReducingSink other) {
            if (!other.empty)
                accept(other.state);
        }
    }
    return new ReduceOp<Double, OptionalDouble, ReducingSink>(StreamShape.DOUBLE_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example 14
Source File: ReduceOps.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs a {@code TerminalOp} that implements a functional reduce on
 * {@code double} values, producing an optional double result.
 *
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Double, OptionalDouble>
makeDouble(DoubleBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Double, OptionalDouble, ReducingSink>, Sink.OfDouble {
        private boolean empty;
        private double state;

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

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

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

        @Override
        public void combine(ReducingSink other) {
            if (!other.empty)
                accept(other.state);
        }
    }
    return new ReduceOp<Double, OptionalDouble, ReducingSink>(StreamShape.DOUBLE_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example 15
Source File: BasicDouble.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void testEmptyOrElseThrowNull() throws Throwable {
    OptionalDouble empty = OptionalDouble.empty();

    double got = empty.orElseThrow(null);
}
 
Example 16
Source File: BasicDouble.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void testEmptyOrElseThrowNull() throws Throwable {
    OptionalDouble empty = OptionalDouble.empty();

    double got = empty.orElseThrow(null);
}
 
Example 17
Source File: BasicDouble.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void testEmptyOrElseGetNull() {
    OptionalDouble empty = OptionalDouble.empty();

    double got = empty.orElseGet(null);
}
 
Example 18
Source File: ReduceOps.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Constructs a {@code TerminalOp} that implements a functional reduce on
 * {@code double} values, producing an optional double result.
 *
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Double, OptionalDouble>
makeDouble(DoubleBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Double, OptionalDouble, ReducingSink>, Sink.OfDouble {
        private boolean empty;
        private double state;

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

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

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

        @Override
        public void combine(ReducingSink other) {
            if (!other.empty)
                accept(other.state);
        }
    }
    return new ReduceOp<Double, OptionalDouble, ReducingSink>(StreamShape.DOUBLE_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example 19
Source File: CompactOptionalDouble.java    From immutables with Apache License 2.0 4 votes vote down vote up
@Encoding.Expose
OptionalDouble get() {
  return present
      ? OptionalDouble.of(value)
      : OptionalDouble.empty();
}
 
Example 20
Source File: IborCapletFloorletPeriod.java    From Strata with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the optional floorlet strike.
 * <p>
 * This defines the strike value of a floorlet.
 * <p>
 * If the period is not a floorlet, this field will be absent.
 * @return the optional value of the property, not null
 */
public OptionalDouble getFloorlet() {
  return floorlet != null ? OptionalDouble.of(floorlet) : OptionalDouble.empty();
}