下面列出了java.util.OptionalLong#empty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static OptionalLong getOptionalLong(String propNameFormat, Class<?> clientIntf) {
Optional<Config> c = config();
if (c.isPresent()) {
String propertyName = String.format(propNameFormat, clientIntf.getName());
Long value = c.get().getOptionalValue(propertyName, Long.class).orElseGet(() -> {
RegisterRestClient anno = clientIntf.getAnnotation(RegisterRestClient.class);
if (anno != null && !StringUtils.isEmpty(anno.configKey())) {
String configKeyPropName = String.format(propNameFormat, anno.configKey());
return c.get().getOptionalValue(configKeyPropName, Long.class).orElse(null);
}
return null;
});
return value == null ? OptionalLong.empty() : OptionalLong.of(value);
}
return OptionalLong.empty();
}
@Test
public void testInformationSchemaSerialize()
throws Exception
{
InformationSchemaTableHandle informationSchemaTableHandle = new InformationSchemaTableHandle(
"information_schema_catalog",
COLUMNS,
ImmutableSet.of(new QualifiedTablePrefix("abc", INFORMATION_SCHEMA)),
Optional.of(ImmutableSet.of("role")),
Optional.of(ImmutableSet.of("grantee")),
OptionalLong.empty());
assertTrue(objectMapper.canSerialize(InformationSchemaTableHandle.class));
String json = objectMapper.writeValueAsString(informationSchemaTableHandle);
testJsonEquals(json, SCHEMA_AS_MAP);
}
@Override
public OptionalLong executeDelete(ConnectorSession session, ConnectorTableHandle deleteHandle)
{
HiveTableHandle handle = (HiveTableHandle) deleteHandle;
Optional<Table> table = metastore.getTable(new HiveIdentity(session), handle.getSchemaName(), handle.getTableName());
if (table.isEmpty()) {
throw new TableNotFoundException(handle.getSchemaTableName());
}
if (table.get().getPartitionColumns().isEmpty()) {
metastore.truncateUnpartitionedTable(session, handle.getSchemaName(), handle.getTableName());
}
else {
for (HivePartition hivePartition : partitionManager.getOrLoadPartitions(metastore, new HiveIdentity(session), handle)) {
metastore.dropPartition(session, handle.getSchemaName(), handle.getTableName(), toPartitionValues(hivePartition.getPartitionId()), true);
}
}
// it is too expensive to determine the exact number of deleted rows
return OptionalLong.empty();
}
@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));
}
@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));
}
public CollectionCreated(int vbucket, long seqno, int version, ByteBuf buffer) {
super(Type.COLLECTION_CREATED, vbucket, seqno, version);
collectionName = MessageUtil.getKeyAsString(buffer);
ByteBuf value = MessageUtil.getContent(buffer);
newManifestId = value.readLong();
scopeId = value.readUnsignedInt();
collectionId = value.readUnsignedInt();
// absent in version 0
maxTtl = value.isReadable() ? OptionalLong.of(value.readUnsignedInt()) : OptionalLong.empty();
}
private OptionalLong fetchGeneratedKey(PreparedStatement statement) throws SQLException {
try (ResultSet keys = statement.getGeneratedKeys()) {
if (keys.next()) {
return OptionalLong.of(keys.getLong(1));
}
}
return OptionalLong.empty();
}
@Override
public OptionalLong firstValueAsLong(String name) {
List<String> l = headers.get(name);
if (l == null) {
return OptionalLong.empty();
} else {
String v = l.get(0);
return OptionalLong.of(Long.parseLong(v));
}
}
@Override public OptionalLong weightedSize() {
if (cache.evicts() && isWeighted()) {
cache.evictionLock.lock();
try {
return OptionalLong.of(Math.max(0, cache.weightedSize()));
} finally {
cache.evictionLock.unlock();
}
}
return OptionalLong.empty();
}
public OptionalLong getOptionalLongProperty(String key) {
final Optional<String> longValue = this.properties.optionallyProvideFor(key);
if (longValue.isPresent()) {
return OptionalLong.of(Long.valueOf(longValue.get()));
} else {
return OptionalLong.empty();
}
}
@Override
default OptionalLong indexOf(K key) {
OptionalLong idx = floorIndex(key);
return idx.isPresent() && comparator().compare(key, nth(idx.getAsLong()).key()) == 0
? idx
: OptionalLong.empty();
}
@Test(expectedExceptions=ObscureException.class)
public void testEmptyOrElseThrow() throws Exception {
OptionalLong empty = OptionalLong.empty();
long got = empty.orElseThrow(ObscureException::new);
}
@Test(expectedExceptions=NullPointerException.class)
public void testEmptyOrElseThrowNull() throws Throwable {
OptionalLong empty = OptionalLong.empty();
long got = empty.orElseThrow(null);
}
/**
* 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();
}
};
}
@Override
public OptionalLong largeBlobSizeBytes() {
return OptionalLong.empty();
}
public static HiveBasicStatistics createEmptyStatistics()
{
return new HiveBasicStatistics(OptionalLong.empty(), OptionalLong.empty(), OptionalLong.empty(), OptionalLong.empty());
}
@Test(expectedExceptions=ObscureException.class)
public void testEmptyOrElseThrow() throws Exception {
OptionalLong empty = OptionalLong.empty();
long got = empty.orElseThrow(ObscureException::new);
}
public SeqnoAndVbucketUuid(long seqno, Long vbucketUuid) {
this.seqno = seqno;
this.vbucketUuid = vbucketUuid == null ? OptionalLong.empty() : OptionalLong.of(vbucketUuid);
}
/**
* Returns an estimation of the number of features in this set, or empty if unknown.
* The default implementation returns an empty value.
*
* @return estimation of the number of features.
*/
protected OptionalLong getFeatureCount() {
return OptionalLong.empty();
}
/**
* Constructs a {@code TerminalOp} for streams of longs.
*
* @param mustFindFirst whether the {@code TerminalOp} must produce the
* first element in the encounter order
* @return a {@code TerminalOp} implementing the find operation
*/
public static TerminalOp<Long, OptionalLong> makeLong(boolean mustFindFirst) {
return new FindOp<>(mustFindFirst, StreamShape.LONG_VALUE, OptionalLong.empty(),
OptionalLong::isPresent, FindSink.OfLong::new);
}