作者:z235235_795 | 来源:互联网 | 2024-11-14 15:03
本文详细介绍了com.apollographql.apollo.api.internal.Optional类中的orNull()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。
在 Java 开发中,com.apollographql.apollo.api.internal.Optional.orNull()
方法是一个非常实用的工具,用于处理可能为空的值。本文将详细介绍该方法的用法,并提供多个代码示例,帮助读者更好地理解其应用场景。
Optional.orNull 介绍
[英] Returns the contained instance if it is present; null otherwise. If the instance is known to be present, use #get() instead.
[中] 如果实例存在,则返回包含的实例;否则返回 null。如果已知实例存在,请使用 #get() 方法。
与 Java 的 java.util.Optional
类相比,Optional.orNull()
方法等同于 Java 8 中的 Optional.orElse(null)
。
代码示例
以下是一些从 Apollo Android 和 AWS AppSync SDK 中提取的实际代码示例,展示了 Optional.orNull()
方法的具体用法。
示例 1:
private synchronized void activate(Optional> callback) throws ApolloCanceledException {
switch (state.get()) {
case IDLE:
originalCallback.set(callback.orNull());
tracker.registerQueryWatcher(this);
break;
case CANCELED:
throw new ApolloCanceledException("Call is cancelled.");
case TERMINATED:
case ACTIVE:
throw new IllegalStateException("Already Executed");
default:
throw new IllegalStateException("Unknown state");
}
state.set(ACTIVE);
}
示例 2:
private synchronized void activate(Optional callback) throws ApolloCanceledException {
switch (state.get()) {
case IDLE:
originalCallback.set(callback.orNull());
tracker.registerPrefetchCall(this);
break;
case CANCELED:
throw new ApolloCanceledException("Call is cancelled.");
case TERMINATED:
case ACTIVE:
throw new IllegalStateException("Already Executed");
default:
throw new IllegalStateException("Unknown state");
}
state.set(ACTIVE);
}
示例 3:
@SuppressWarnings("ResultOfMethodCallIgnored")
private synchronized void activate(Optional> callback) {
switch (state.get()) {
case IDLE:
originalCallback.set(callback.orNull());
tracker.registerCall(this);
callback.apply(new Action>() {
@Override public void apply(@NotNull Callback callback) {
callback.onStatusEvent(StatusEvent.SCHEDULED);
}
});
break;
case CANCELED:
throw new ApolloCanceledException("Call is cancelled.");
case TERMINATED:
case ACTIVE:
throw new IllegalStateException("Already Executed");
default:
throw new IllegalStateException("Unknown state");
}
state.set(ACTIVE);
}
示例 4:
public Builder toBuilder() {
return new Builder(operation)
.cacheHeaders(cacheHeaders)
.fetchFromCache(fetchFromCache)
.optimisticUpdates(optimisticUpdates.orNull());
}
示例 5:
@Nullable @Override public Record loadRecord(@NotNull final String key, @NotNull final CacheHeaders cacheHeaders) {
checkNotNull(key, "key == null");
checkNotNull(cacheHeaders, "cacheHeaders == null");
try {
final Optional nOnOptimisticRecord= nextCache()
.flatMap(new Function>() {
@NotNull @Override public Optional apply(@NotNull NormalizedCache cache) {
return Optional.fromNullable(cache.loadRecord(key, cacheHeaders));
}
});
final RecordJournal journal = lruCache.getIfPresent(key);
if (journal != null) {
return nonOptimisticRecord.map(new Function() {
@NotNull @Override public Record apply(@NotNull Record record) {
Record result = record.clone();
result.mergeWith(journal.snapshot);
return result;
}
}).or(journal.snapshot.clone());
} else {
return nonOptimisticRecord.orNull();
}
} catch (Exception ignore) {
return null;
}
}
测试示例:
@Test
public void testOrNullPresent() {
assertEquals("a", Optional.of("a").orNull());
}
@Test
public void testOrNullAbsent() {
assertNull(Optional.absent().orNull());
}