本文整理匯總了Java中java.util.OptionalLong.of方法的典型用法代碼示例。如果您正苦於以下問題:Java OptionalLong.of方法的具體用法?Java OptionalLong.of怎麽用?Java OptionalLong.of使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.OptionalLong
的用法示例。
在下文中一共展示了OptionalLong.of方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testEmpty
import java.util.OptionalLong; //導入方法依賴的package包/類
@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));
}
示例2: serialOf
import java.util.OptionalLong; //導入方法依賴的package包/類
@Override
public OptionalLong serialOf(UUID l) {
try (Context time = seqLookupLatency.time();) {
List<Long> res = jdbcTemplate.query(PGConstants.SELECT_SER_BY_ID, new Object[] {
"{\"id\":\"" + l + "\"}" }, this::extractSerFromResultSet);
if (res.size() > 1) {
throw new IllegalStateException("Event ID appeared twice!?");
} else if (res.isEmpty()) {
return OptionalLong.empty();
}
Long ser = res.get(0);
if (ser != null && ser.longValue() > 0) {
return OptionalLong.of(ser.longValue());
} else {
return OptionalLong.empty();
}
}
}
示例3: getLongProperty
import java.util.OptionalLong; //導入方法依賴的package包/類
default OptionalLong getLongProperty(String name) {
Optional<String> optValue = getProperty(name);
if (optValue.isPresent()) {
return OptionalLong.of(Long.parseLong(optValue.get()));
} else {
return OptionalLong.empty();
}
}
示例4: testPresent
import java.util.OptionalLong; //導入方法依賴的package包/類
@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));
}
示例5: testEmpty
import java.util.OptionalLong; //導入方法依賴的package包/類
@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(); });
AtomicBoolean emptyCheck = new AtomicBoolean();
empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
assertTrue(emptyCheck.get());
try {
empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
fail();
} catch (ObscureException expected) {
} catch (AssertionError e) {
throw e;
} catch (Throwable t) {
fail();
}
assertEquals(2, empty.orElse(2));
assertEquals(2, empty.orElseGet(()-> 2));
}
示例6: fromProto
import java.util.OptionalLong; //導入方法依賴的package包/類
@NonNull
public OptionalLong fromProto(@NonNull MSG_OptionalSerial serialOf) {
// note that an unsigned is used to transport the serial. Serials MUST be >0
if (serialOf.getPresent() && serialOf.getSerial() > 0) {
return OptionalLong.of(serialOf.getSerial());
} else {
return OptionalLong.empty();
}
}
示例7: setMillisBehindLatestAndContinuationToken
import java.util.OptionalLong; //導入方法依賴的package包/類
private void setMillisBehindLatestAndContinuationToken() {
final Map<String, String> tagNameToTagValueMap = getTagNameToValueMap();
//Do not overwrite an existing value with Optional.absent
String millisBehindString = tagNameToTagValueMap.get(MILLIS_BEHIND_NOW_KEY);
if (millisBehindString != null) {
millisBehindNow = (OptionalLong.of(Long.parseLong(millisBehindString)));
}
String continutationTokenString = tagNameToTagValueMap.get(CONTINUATION_TOKEN_KEY);
if (continutationTokenString != null) {
continuationToken = Optional.of(continutationTokenString);
}
}
開發者ID:aws,項目名稱:amazon-kinesis-video-streams-parser-library,代碼行數:14,代碼來源:FragmentMetadataVisitor.java
示例8: PropertyMapperTestEntity
import java.util.OptionalLong; //導入方法依賴的package包/類
public PropertyMapperTestEntity(final int id) {
this.id = id;
this.name = Optional.of("name");
this.intValue = OptionalInt.of(2);
this.longValue = OptionalLong.of(3);
this.doubleValue = OptionalDouble.of(4.5);
this.dateValue = LocalDate.now();
this.datetimeValue = LocalDateTime.now();
this.enumValue = TesEnum.B_VALUE;
this.bigIntValue = BigInteger.TEN;
}
示例9: OptionalLongExample
import java.util.OptionalLong; //導入方法依賴的package包/類
public OptionalLongExample(Long value) {
this.value = OptionalLong.of(value);
}
示例10: get
import java.util.OptionalLong; //導入方法依賴的package包/類
@Override
public OptionalLong get() {
return hasValue ? OptionalLong.of(value) : null;
}
示例11: payloadLen
import java.util.OptionalLong; //導入方法依賴的package包/類
@Override
public void payloadLen(long value) {
checkAndSetOrder(p -> p == 5 || p == 6, n -> 7);
payloadLen = OptionalLong.of(value);
}
示例12: makeLong
import java.util.OptionalLong; //導入方法依賴的package包/類
/**
* 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();
}
};
}
示例13: test_optionalLong
import java.util.OptionalLong; //導入方法依賴的package包/類
public void test_optionalLong() throws Exception {
OptionalLong val = OptionalLong.of(3);
String text = JSON.toJSONString(val);
Assert.assertEquals("3", text);
OptionalLong val2 = JSON.parseObject(text, OptionalLong.class);
Assert.assertEquals(val.getAsLong(), val2.getAsLong());
}
示例14: getLastTested
import java.util.OptionalLong; //導入方法依賴的package包/類
/**
* Return the time in milliseconds when this cooldown was last {@link #test()}ed.
*
* @return the last call time
*/
@Override
public OptionalLong getLastTested() {
return lastTested == 0 ? OptionalLong.empty() : OptionalLong.of(lastTested);
}
示例15: findLast
import java.util.OptionalLong; //導入方法依賴的package包/類
/**
* Returns the last element of the specified stream, or {@link OptionalLong#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 LongStream#findFirst()
* @throws NullPointerException if the last element of the stream is null
*/
public static OptionalLong findLast(LongStream stream) {
// findLast(Stream) does some allocation, so we might as well box some more
java.util.Optional<Long> boxedLast = findLast(stream.boxed());
return boxedLast.isPresent() ? OptionalLong.of(boxedLast.get()) : OptionalLong.empty();
}