本文整理了Java中com.apollographql.apollo.api.internal.Optional.map()
方法的一些代码示例,展示了Optional.map()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Optional.map()
方法的具体详情如下:
包路径:com.apollographql.apollo.api.internal.Optional
类名称:Optional
方法名:map
暂无
代码示例来源:origin: apollographql/apollo-android
/**
* @param record The {@link Record} to merge.
* @param cacheHeaders The {@link CacheHeaders} associated with the request which generated this record.
* @return A set of record field keys that have changed. This set is returned by {@link Record#mergeWith(Record)}.
*/
@NotNull public Set<String> merge(@NotNull final Record record, @NotNull final CacheHeaders cacheHeaders) {
checkNotNull(record, "apolloRecord == null");
checkNotNull(cacheHeaders, "cacheHeaders == null");
if (cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE)) {
return Collections.emptySet();
}
Set<String> nextCacheChangedKeys = nextCache().map(new Function<NormalizedCache, Set<String>>() {
@NotNull @Override public Set<String> apply(@NotNull NormalizedCache cache) {
return cache.merge(record, cacheHeaders);
}
}).or(Collections.<String>emptySet());
Set<String> currentCacheChangedKeys = performMerge(record, cacheHeaders);
Set<String> changedKeys = new HashSet<>();
changedKeys.addAll(nextCacheChangedKeys);
changedKeys.addAll(currentCacheChangedKeys);
return changedKeys;
}
代码示例来源:origin: apollographql/apollo-android
Set<String> cacheResponse(final InterceptorResponse networkResponse,
final InterceptorRequest request) {
final Optional<List<Record>> records = networkResponse.cacheRecords.map(
new Function<Collection<Record>, List<Record>>() {
@NotNull @Override public List<Record> apply(@NotNull Collection<Record> records) {
final List<Record> result = new ArrayList<>(records.size());
for (Record record : records) {
result.add(record.toBuilder().mutationId(request.uniqueId).build());
}
return result;
}
}
);
if (!records.isPresent()) {
return Collections.emptySet();
}
try {
return apolloStore.writeTransaction(new Transaction<WriteableStore, Set<String>>() {
@Nullable @Override public Set<String> execute(WriteableStore cache) {
return cache.merge(records.get(), request.cacheHeaders);
}
});
} catch (Exception e) {
logger.e("Failed to cache operation response", e);
return Collections.emptySet();
}
}
代码示例来源:origin: apollographql/apollo-android
/**
* Calls through to {@link NormalizedCache#merge(Record, CacheHeaders)}. Implementations should override this method
* if the underlying storage technology can offer an optimized manner to store multiple records.
*
* @param recordSet The set of Records to merge.
* @param cacheHeaders The {@link CacheHeaders} associated with the request which generated this record.
* @return A set of record field keys that have changed. This set is returned by {@link Record#mergeWith(Record)}.
*/
@NotNull
public Set<String> merge(@NotNull final Collection<Record> recordSet, @NotNull final CacheHeaders cacheHeaders) {
checkNotNull(recordSet, "recordSet == null");
checkNotNull(cacheHeaders, "cacheHeaders == null");
if (cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE)) {
return Collections.emptySet();
}
//noinspection ResultOfMethodCallIgnored
Set<String> nextCacheChangedKeys = nextCache().map(new Function<NormalizedCache, Set<String>>() {
@NotNull @Override public Set<String> apply(@NotNull NormalizedCache cache) {
return cache.merge(recordSet, cacheHeaders);
}
}).or(Collections.<String>emptySet());
Set<String> currentCacheChangedKeys = new HashSet<>();
for (Record record : recordSet) {
currentCacheChangedKeys.addAll(performMerge(record, cacheHeaders));
}
Set<String> changedKeys = new HashSet<>();
changedKeys.addAll(nextCacheChangedKeys);
changedKeys.addAll(currentCacheChangedKeys);
return changedKeys;
}
代码示例来源:origin: apollographql/apollo-android
public final NormalizedCache createChain(final RecordFieldJsonAdapter recordFieldAdapter) {
if (nextFactory.isPresent()) {
return create(recordFieldAdapter)
.chain(nextFactory.map(new Function<NormalizedCacheFactory, NormalizedCache>() {
@NotNull @Override public NormalizedCache apply(@NotNull NormalizedCacheFactory factory) {
return factory.createChain(recordFieldAdapter);
}
}).get());
} else {
return create(recordFieldAdapter);
}
}
代码示例来源:origin: apollographql/apollo-android
@Nullable @Override public Record loadRecord(@NotNull final String key, @NotNull final CacheHeaders cacheHeaders) {
checkNotNull(key, "key == null");
checkNotNull(cacheHeaders, "cacheHeaders == null");
try {
final Optional<Record> nonOptimisticRecord = nextCache()
.flatMap(new Function<NormalizedCache, Optional<Record>>() {
@NotNull @Override public Optional<Record> 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<Record, Record>() {
@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;
}
}
代码示例来源:origin: apollographql/apollo-android
@Test
public void testMapPresentFunctionReturnsNull() {
try {
Optional<String> unused =
Optional.of("a")
.map(
new Function<String, String>() {
@Override
public String apply(String input) {
return null;
}
});
fail("Should throw if Function returns null.");
} catch (NullPointerException ignore) {
}
}
代码示例来源:origin: apollographql/apollo-android
@Test
public void testMapAbssentFunctionReturnsNull() {
assertEquals(Optional.absent(),
Optional.absent().map(
new Function<Object, Object>() {
@Override public Object apply(Object input) {
return null;
}
}));
}
代码示例来源:origin: apollographql/apollo-android
@Override public boolean remove(@NotNull final CacheKey cacheKey, final boolean cascade) {
checkNotNull(cacheKey, "cacheKey == null");
boolean result = nextCache().map(new Function<NormalizedCache, Boolean>() {
@NotNull @Override public Boolean apply(@NotNull NormalizedCache cache) {
return cache.remove(cacheKey, cascade);
}
}).or(Boolean.FALSE);
RecordJournal recordJournal = lruCache.getIfPresent(cacheKey.key());
if (recordJournal != null) {
lruCache.invalidate(cacheKey.key());
result = true;
if (cascade) {
for (CacheReference cacheReference : recordJournal.snapshot.referencedFields()) {
result = result & remove(CacheKey.from(cacheReference.key()), true);
}
}
}
return result;
}
代码示例来源:origin: apollographql/apollo-android
@Override public boolean remove(@NotNull final CacheKey cacheKey, final boolean cascade) {
checkNotNull(cacheKey, "cacheKey == null");
boolean result;
result = nextCache().map(new Function<NormalizedCache, Boolean>() {
@NotNull @Override public Boolean apply(@NotNull NormalizedCache cache) {
return cache.remove(cacheKey, cascade);
}
}).or(Boolean.FALSE);
Record record = lruCache.getIfPresent(cacheKey.key());
if (record != null) {
lruCache.invalidate(cacheKey.key());
result = true;
if (cascade) {
for (CacheReference cacheReference : record.referencedFields()) {
result = result & remove(CacheKey.from(cacheReference.key()), true);
}
}
}
return result;
}
代码示例来源:origin: apollographql/apollo-android
@Test
public void testMapAbsent() {
assertEquals(Optional.absent(), Optional.absent().map(Functions.identity()));
assertEquals(Optional.absent(), Optional.absent().map(Functions.toStringFunction()));
}
代码示例来源:origin: apollographql/apollo-android
@Test
public void testMapPresentToString() {
assertEquals(Optional.of("42"), Optional.of(42).map(Functions.toStringFunction()));
}
代码示例来源:origin: apollographql/apollo-android
@Test
public void testMapPresentIdentity() {
assertEquals(Optional.of("a"), Optional.of("a").map(Functions.identity()));
}
代码示例来源:origin: com.amazonaws/aws-android-sdk-appsync-runtime
@Nonnull @Override public Set<String> merge(@Nonnull final Record record, @Nonnull final CacheHeaders cacheHeaders) {
checkNotNull(record, "record == null");
checkNotNull(cacheHeaders, "cacheHeaders == null");
return nextCache().map(new Function<NormalizedCache, Set<String>>() {
@Nonnull @Override public Set<String> apply(@Nonnull NormalizedCache cache) {
return cache.merge(record, cacheHeaders);
}
}).or(Collections.<String>emptySet());
}
代码示例来源:origin: awslabs/aws-mobile-appsync-sdk-android
@Nonnull @Override public Set<String> merge(@Nonnull final Record record, @Nonnull final CacheHeaders cacheHeaders) {
checkNotNull(record, "record == null");
checkNotNull(cacheHeaders, "cacheHeaders == null");
return nextCache().map(new Function<NormalizedCache, Set<String>>() {
@Nonnull @Override public Set<String> apply(@Nonnull NormalizedCache cache) {
return cache.merge(record, cacheHeaders);
}
}).or(Collections.<String>emptySet());
}
代码示例来源:origin: awslabs/aws-mobile-appsync-sdk-android
@Override
public boolean remove(@Nonnull final CacheKey cacheKey) {
checkNotNull(cacheKey, "cacheKey == null");
boolean result;
result = nextCache().map(new Function<NormalizedCache, Boolean>() {
@Nonnull @Override
public Boolean apply(@Nonnull NormalizedCache cache) {
return cache.remove(cacheKey);
}
}).or(Boolean.FALSE);
return result | deleteRecord(cacheKey.key());
}
代码示例来源:origin: com.amazonaws/aws-android-sdk-appsync-runtime
@Override public boolean remove(@Nonnull final CacheKey cacheKey) {
checkNotNull(cacheKey, "cacheKey == null");
boolean result = nextCache().map(new Function<NormalizedCache, Boolean>() {
@Nonnull @Override public Boolean apply(@Nonnull NormalizedCache cache) {
return cache.remove(cacheKey);
}
}).or(Boolean.FALSE);
if (lruCache.getIfPresent(cacheKey.key()) != null) {
lruCache.invalidate(cacheKey.key());
result = true;
}
return result;
}
代码示例来源:origin: com.amazonaws/aws-android-sdk-appsync-runtime
@Override public boolean remove(@Nonnull final CacheKey cacheKey) {
checkNotNull(cacheKey, "cacheKey == null");
boolean result;
result = nextCache().map(new Function<NormalizedCache, Boolean>() {
@Nonnull @Override public Boolean apply(@Nonnull NormalizedCache cache) {
return cache.remove(cacheKey);
}
}).or(Boolean.FALSE);
if (lruCache.getIfPresent(cacheKey.key()) != null) {
lruCache.invalidate(cacheKey.key());
result = true;
}
return result;
}
代码示例来源:origin: com.amazonaws/aws-android-sdk-appsync-runtime
public final NormalizedCache createChain(final RecordFieldJsonAdapter recordFieldAdapter) {
if (nextFactory.isPresent()) {
return create(recordFieldAdapter)
.chain(nextFactory.map(new Function<NormalizedCacheFactory, NormalizedCache>() {
@Nonnull @Override public NormalizedCache apply(@Nonnull NormalizedCacheFactory factory) {
return factory.createChain(recordFieldAdapter);
}
}).get());
} else {
return create(recordFieldAdapter);
}
}
代码示例来源:origin: awslabs/aws-mobile-appsync-sdk-android
public final NormalizedCache createChain(final RecordFieldJsonAdapter recordFieldAdapter) {
if (nextFactory.isPresent()) {
return create(recordFieldAdapter)
.chain(nextFactory.map(new Function<NormalizedCacheFactory, NormalizedCache>() {
@Nonnull @Override public NormalizedCache apply(@Nonnull NormalizedCacheFactory factory) {
return factory.createChain(recordFieldAdapter);
}
}).get());
} else {
return create(recordFieldAdapter);
}
}
代码示例来源:origin: awslabs/aws-mobile-appsync-sdk-android
@Override public boolean remove(@Nonnull final CacheKey cacheKey) {
checkNotNull(cacheKey, "cacheKey == null");
boolean result = nextCache().map(new Function<NormalizedCache, Boolean>() {
@Nonnull @Override public Boolean apply(@Nonnull NormalizedCache cache) {
return cache.remove(cacheKey);
}
}).or(Boolean.FALSE);
if (lruCache.getIfPresent(cacheKey.key()) != null) {
lruCache.invalidate(cacheKey.key());
result = true;
}
return result;
}
内容来源于网络,如有侵权,请联系作者删除!