Java Code Examples for java.util.OptionalLong#of()
The following examples show how to use
java.util.OptionalLong#of() .
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: AbstractProducerMetricsListener.java From hollow with Apache License 2.0 | 6 votes |
/** * On cycle completion this method reports cycle metrics. * @param status Whether the cycle succeeded or failed * @param readState Hollow data state published by cycle, not used here because data size is known from when announcement completed * @param version Version of data that was published in this cycle * @param elapsed Cycle start to end duration */ @Override public void onCycleComplete(com.netflix.hollow.api.producer.Status status, HollowProducer.ReadState readState, long version, Duration elapsed) { boolean isCycleSuccess; long cycleEndTimeNano = System.nanoTime(); if (status.getType() == com.netflix.hollow.api.producer.Status.StatusType.SUCCESS) { isCycleSuccess = true; consecutiveFailures = 0l; lastCycleSuccessTimeNanoOptional = OptionalLong.of(cycleEndTimeNano); } else { isCycleSuccess = false; consecutiveFailures ++; } CycleMetrics.Builder cycleMetricsBuilder = new CycleMetrics.Builder() .setConsecutiveFailures(consecutiveFailures) .setCycleDurationMillis(elapsed.toMillis()) .setIsCycleSuccess(isCycleSuccess); lastCycleSuccessTimeNanoOptional.ifPresent(cycleMetricsBuilder::setLastCycleSuccessTimeNano); cycleMetricsReporting(cycleMetricsBuilder.build()); }
Example 2
Source File: BasicLong.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
@Test(groups = "unit") public void testEmpty() { OptionalLong empty = OptionalLong.empty(); OptionalLong present = OptionalLong.of(1); // empty assertTrue(empty.equals(empty)); assertTrue(empty.equals(OptionalLong.empty())); assertTrue(!empty.equals(present)); assertTrue(0 == empty.hashCode()); assertTrue(!empty.toString().isEmpty()); assertTrue(!empty.isPresent()); empty.ifPresent(v -> { fail(); }); assertEquals(2, empty.orElse(2)); assertEquals(2, empty.orElseGet(()-> 2)); }
Example 3
Source File: BasicLong.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
@Test(groups = "unit") public void testEmpty() { OptionalLong empty = OptionalLong.empty(); OptionalLong present = OptionalLong.of(1); // empty assertTrue(empty.equals(empty)); assertTrue(empty.equals(OptionalLong.empty())); assertTrue(!empty.equals(present)); assertTrue(0 == empty.hashCode()); assertTrue(!empty.toString().isEmpty()); assertTrue(!empty.isPresent()); empty.ifPresent(v -> { fail(); }); assertEquals(2, empty.orElse(2)); assertEquals(2, empty.orElseGet(()-> 2)); }
Example 4
Source File: AbstractProducerMetricsListenerTest.java From hollow with Apache License 2.0 | 6 votes |
@Test public void testCycleCompleteWithSuccess() { final class TestProducerMetricsListener extends AbstractProducerMetricsListener { @Override public void cycleMetricsReporting(CycleMetrics cycleMetrics) { Assert.assertNotNull(cycleMetrics); Assert.assertEquals(0l, cycleMetrics.getConsecutiveFailures()); Assert.assertEquals(Optional.of(true), cycleMetrics.getIsCycleSuccess()); Assert.assertEquals(OptionalLong.of(TEST_CYCLE_DURATION_MILLIS.toMillis()), cycleMetrics.getCycleDurationMillis()); Assert.assertNotEquals(OptionalLong.of(TEST_LAST_CYCLE_NANOS), cycleMetrics.getLastCycleSuccessTimeNano()); Assert.assertNotEquals(OptionalLong.empty(), cycleMetrics.getLastCycleSuccessTimeNano()); } } AbstractProducerMetricsListener concreteProducerMetricsListener = new TestProducerMetricsListener(); concreteProducerMetricsListener.lastCycleSuccessTimeNanoOptional = OptionalLong.of(TEST_LAST_CYCLE_NANOS); concreteProducerMetricsListener.onCycleStart(TEST_VERSION); concreteProducerMetricsListener.onCycleComplete(TEST_STATUS_SUCCESS, mockReadState, TEST_VERSION, TEST_CYCLE_DURATION_MILLIS); }
Example 5
Source File: PrestoSystemRequirements.java From presto with Apache License 2.0 | 5 votes |
private static OptionalLong getMaxFileDescriptorCount() { try { MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); Object maxFileDescriptorCount = mbeanServer.getAttribute(ObjectName.getInstance(OPERATING_SYSTEM_MXBEAN_NAME), "MaxFileDescriptorCount"); return OptionalLong.of(((Number) maxFileDescriptorCount).longValue()); } catch (Exception e) { return OptionalLong.empty(); } }
Example 6
Source File: BasicLong.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
@Test(groups = "unit") public void testPresent() { OptionalLong empty = OptionalLong.empty(); OptionalLong present = OptionalLong.of(1L); // present assertTrue(present.equals(present)); assertFalse(present.equals(OptionalLong.of(0L))); assertTrue(present.equals(OptionalLong.of(1L))); assertFalse(present.equals(empty)); assertTrue(Long.hashCode(1) == present.hashCode()); assertFalse(present.toString().isEmpty()); assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString())); assertEquals(1L, present.getAsLong()); try { present.ifPresent(v -> { throw new ObscureException(); }); fail(); } catch(ObscureException expected) { } assertEquals(1, present.orElse(2)); assertEquals(1, present.orElseGet(null)); assertEquals(1, present.orElseGet(()-> 2)); assertEquals(1, present.orElseGet(()-> 3)); assertEquals(1, present.<RuntimeException>orElseThrow(null)); assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new)); }
Example 7
Source File: BasicLong.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
@Test(groups = "unit") public void testPresent() { OptionalLong empty = OptionalLong.empty(); OptionalLong present = OptionalLong.of(1L); // present assertTrue(present.equals(present)); assertFalse(present.equals(OptionalLong.of(0L))); assertTrue(present.equals(OptionalLong.of(1L))); assertFalse(present.equals(empty)); assertTrue(Long.hashCode(1) == present.hashCode()); assertFalse(present.toString().isEmpty()); assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString())); assertEquals(1L, present.getAsLong()); try { present.ifPresent(v -> { throw new ObscureException(); }); fail(); } catch(ObscureException expected) { } assertEquals(1, present.orElse(2)); assertEquals(1, present.orElseGet(null)); assertEquals(1, present.orElseGet(()-> 2)); assertEquals(1, present.orElseGet(()-> 3)); assertEquals(1, present.<RuntimeException>orElseThrow(null)); assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new)); }
Example 8
Source File: BasicLong.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
@Test(groups = "unit") public void testPresent() { OptionalLong empty = OptionalLong.empty(); OptionalLong present = OptionalLong.of(1L); // present assertTrue(present.equals(present)); assertFalse(present.equals(OptionalLong.of(0L))); assertTrue(present.equals(OptionalLong.of(1L))); assertFalse(present.equals(empty)); assertTrue(Long.hashCode(1) == present.hashCode()); assertFalse(present.toString().isEmpty()); assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString())); assertEquals(1L, present.getAsLong()); try { present.ifPresent(v -> { throw new ObscureException(); }); fail(); } catch(ObscureException expected) { } assertEquals(1, present.orElse(2)); assertEquals(1, present.orElseGet(null)); assertEquals(1, present.orElseGet(()-> 2)); assertEquals(1, present.orElseGet(()-> 3)); assertEquals(1, present.<RuntimeException>orElseThrow(null)); assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new)); }
Example 9
Source File: Numbers.java From helper with MIT License | 5 votes |
@Nonnull public static OptionalLong parseLong(@Nonnull String s) { try { return OptionalLong.of(Long.parseLong(s)); } catch (NumberFormatException e) { return OptionalLong.empty(); } }
Example 10
Source File: ReduceOps.java From desugar_jdk_libs with GNU General Public License v2.0 | 4 votes |
/** * 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 11
Source File: CaffeineConfiguration.java From caffeine with Apache License 2.0 | 4 votes |
/** * Returns the expire after access in nanoseconds. * * @return the duration in nanoseconds */ public OptionalLong getExpireAfterAccess() { return (expireAfterAccessNanos == null) ? OptionalLong.empty() : OptionalLong.of(expireAfterAccessNanos); }
Example 12
Source File: ReduceOps.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
/** * 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 13
Source File: FindOps.java From JDKSourceCode1.8 with MIT License | 4 votes |
@Override public OptionalLong get() { return hasValue ? OptionalLong.of(value) : null; }
Example 14
Source File: ReduceOps.java From JDKSourceCode1.8 with MIT License | 4 votes |
/** * 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 15
Source File: CommandOriginData.java From Nukkit with GNU General Public License v3.0 | 4 votes |
public OptionalLong getVarLong() { if (varlong == null) { return OptionalLong.empty(); } return OptionalLong.of(varlong); }
Example 16
Source File: MockServerRequest.java From java-technology-stack with MIT License | 4 votes |
private OptionalLong toOptionalLong(long value) { return value != -1 ? OptionalLong.of(value) : OptionalLong.empty(); }
Example 17
Source File: FindOps.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
@Override public OptionalLong get() { return hasValue ? OptionalLong.of(value) : null; }
Example 18
Source File: CooldownImpl.java From helper with MIT License | 4 votes |
@Override public OptionalLong getLastTested() { return this.lastTested == 0 ? OptionalLong.empty() : OptionalLong.of(this.lastTested); }
Example 19
Source File: StubGenesisConfigOptions.java From besu with Apache License 2.0 | 4 votes |
public StubGenesisConfigOptions eip158Block(final long blockNumber) { spuriousDragonBlockNumber = OptionalLong.of(blockNumber); return this; }
Example 20
Source File: FindOps.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
@Override public OptionalLong get() { return hasValue ? OptionalLong.of(value) : null; }