当前位置: 首页>>代码示例>>Java>>正文


Java OptionalDouble.of方法代码示例

本文整理汇总了Java中java.util.OptionalDouble.of方法的典型用法代码示例。如果您正苦于以下问题:Java OptionalDouble.of方法的具体用法?Java OptionalDouble.of怎么用?Java OptionalDouble.of使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.OptionalDouble的用法示例。


在下文中一共展示了OptionalDouble.of方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: average

import java.util.OptionalDouble; //导入方法依赖的package包/类
/**
 * {@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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:35,代码来源:DoublePipeline.java

示例2: testEmpty

import java.util.OptionalDouble; //导入方法依赖的package包/类
@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));
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:17,代码来源:BasicDouble.java

示例3: serializeOptionalPrimitive

import java.util.OptionalDouble; //导入方法依赖的package包/类
@Test
public void serializeOptionalPrimitive() {
	final OptionalPrimitiveTestEntity entity = new OptionalPrimitiveTestEntity();
	entity.d = OptionalDouble.of(69.5);
	entity.i = OptionalInt.of(69);
	entity.l = OptionalLong.of(69L);

	final VPackSlice vpack = vp.serialize(entity);
	assertThat(vpack, is(notNullValue()));
	assertThat(vpack.isObject(), is(true));
	assertThat(vpack.get("d").isDouble(), is(true));
	assertThat(vpack.get("d").getAsDouble(), is(69.5));
	assertThat(vpack.get("i").isInteger(), is(true));
	assertThat(vpack.get("i").getAsInt(), is(69));
	assertThat(vpack.get("l").isInteger(), is(true));
	assertThat(vpack.get("l").getAsLong(), is(69L));
}
 
开发者ID:arangodb,项目名称:java-velocypack-module-jdk8,代码行数:18,代码来源:VPackOptionalTest.java

示例4: from

import java.util.OptionalDouble; //导入方法依赖的package包/类
@Encoding.Of
static OptionalDouble from(Object ddd) {
  if (Double.isNaN((Double) ddd)) {
    return OptionalDouble.empty();
  }
  return OptionalDouble.of((Double) ddd);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:8,代码来源:CompactOptionalDouble.java

示例5: average

import java.util.OptionalDouble; //导入方法依赖的package包/类
@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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:IntPipeline.java

示例6: parseDouble

import java.util.OptionalDouble; //导入方法依赖的package包/类
@Nonnull
public static OptionalDouble parseDouble(@Nonnull String s) {
    try {
        return OptionalDouble.of(Double.parseDouble(s));
    } catch (NumberFormatException e) {
        return OptionalDouble.empty();
    }
}
 
开发者ID:lucko,项目名称:helper,代码行数:9,代码来源:Numbers.java

示例7: getDoubleProperty

import java.util.OptionalDouble; //导入方法依赖的package包/类
default OptionalDouble getDoubleProperty(String name) {
    Optional<String> optValue = getProperty(name);
    if (optValue.isPresent()) {
        return OptionalDouble.of(Double.parseDouble(optValue.get()));
    } else {
        return OptionalDouble.empty();
    }
}
 
开发者ID:gchq,项目名称:stroom-stats,代码行数:9,代码来源:StroomPropertyService.java

示例8: toOptionalDouble

import java.util.OptionalDouble; //导入方法依赖的package包/类
private static OptionalDouble toOptionalDouble(final String value) {
	try {
		return OptionalDouble.of(Double.parseDouble(value));
	} catch (NumberFormatException e) {
		return OptionalDouble.empty();
	}
}
 
开发者ID:jenetics,项目名称:prngine,代码行数:8,代码来源:DieHarder.java

示例9: getValue

import java.util.OptionalDouble; //导入方法依赖的package包/类
@Override
public OptionalDouble getValue(final JavaType type, final ResultSet rs, final int columnIndex,
		final PropertyMapperManager mapperManager)
		throws SQLException {
	double value = rs.getDouble(columnIndex);
	if (!rs.wasNull()) {
		return OptionalDouble.of(value);
	} else {
		return OptionalDouble.empty();
	}
}
 
开发者ID:future-architect,项目名称:uroborosql,代码行数:12,代码来源:OptionalDoublePropertyMapper.java

示例10: testPresent

import java.util.OptionalDouble; //导入方法依赖的package包/类
@Test(groups = "unit")
    public void testPresent() {
    OptionalDouble empty = OptionalDouble.empty();
    OptionalDouble present = OptionalDouble.of(1.0);

    // present
    assertTrue(present.equals(present));
    assertFalse(present.equals(OptionalDouble.of(0.0)));
    assertTrue(present.equals(OptionalDouble.of(1.0)));
    assertTrue(!present.equals(empty));
    assertTrue(Double.hashCode(1.0) == present.hashCode());
    assertFalse(present.toString().isEmpty());
    assertTrue(-1 != present.toString().indexOf(Double.toString(present.getAsDouble()).toString()));
    assertEquals(1.0, present.getAsDouble());
    try {
        present.ifPresent(v -> { throw new ObscureException(); });
        fail();
    } catch(ObscureException expected) {

    }
    assertEquals(1.0, present.orElse(2.0));
    assertEquals(1.0, present.orElseGet(null));
    assertEquals(1.0, present.orElseGet(()-> 2.0));
    assertEquals(1.0, present.orElseGet(()-> 3.0));
    assertEquals(1.0, present.<RuntimeException>orElseThrow(null));
    assertEquals(1.0, present.<RuntimeException>orElseThrow(ObscureException::new));
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:28,代码来源:BasicDouble.java

示例11: get

import java.util.OptionalDouble; //导入方法依赖的package包/类
@Override
public OptionalDouble get() {
    return hasValue ? OptionalDouble.of(value) : null;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:5,代码来源:FindOps.java

示例12: get

import java.util.OptionalDouble; //导入方法依赖的package包/类
@Encoding.Expose
OptionalDouble get() {
  return present
      ? OptionalDouble.of(value)
      : OptionalDouble.empty();
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:7,代码来源:CompactOptionalDouble.java

示例13: makeDouble

import java.util.OptionalDouble; //导入方法依赖的package包/类
/**
 * 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();
        }
    };
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:50,代码来源:ReduceOps.java

示例14: findLast

import java.util.OptionalDouble; //导入方法依赖的package包/类
/**
 * Returns the last element of the specified stream, or {@link OptionalDouble#empty} if the stream
 * is empty.
 *
 * <p>Equivalent to {@code stream.reduce((a, b) -> b)}, but may perform significantly better. This
 * method's runtime will be between O(log n) and O(n), performing better on <a
 * href="http://gee.cs.oswego.edu/dl/html/StreamParallelGuidance.html">efficiently splittable</a>
 * streams.
 *
 * @see DoubleStream#findFirst()
 * @throws NullPointerException if the last element of the stream is null
 */
public static OptionalDouble findLast(DoubleStream stream) {
  // findLast(Stream) does some allocation, so we might as well box some more
  java.util.Optional<Double> boxedLast = findLast(stream.boxed());
  return boxedLast.isPresent() ? OptionalDouble.of(boxedLast.get()) : OptionalDouble.empty();
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:18,代码来源:Streams.java


注:本文中的java.util.OptionalDouble.of方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。