com.apollographql.apollo.api.internal.Optional.or()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(11.3k)|赞(0)|评价(0)|浏览(95)

本文整理了Java中com.apollographql.apollo.api.internal.Optional.or()方法的一些代码示例,展示了Optional.or()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Optional.or()方法的具体详情如下:
包路径:com.apollographql.apollo.api.internal.Optional
类名称:Optional
方法名:or

Optional.or介绍

[英]Returns this Optional if it has a value present; secondChoice otherwise.

Comparison to java.util.Optional: this method has no equivalent in Java 8's Optionalclass; write thisOptional.isPresent() ? thisOptional : secondChoice instead.
[中]如果存在值,则返回此可选值;第二个选择是另一个。
与java的比较。util。可选:该方法在Java8的Optionalclass中没有等价物;写下这个选项。isPresent()?这是可选的:第二选择。

代码示例

代码示例来源: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

@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

/**
 * 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: awslabs/aws-mobile-appsync-sdk-android

@Nullable public Record loadRecord(@Nonnull final String key, @Nonnull final CacheHeaders cacheHeaders) {
 return selectRecordForKey(key)
   .apply(new Action<Record>() {
    @Override
    public void apply(@Nonnull Record record) {
     if (cacheHeaders.hasHeader(EVICT_AFTER_READ)) {
      deleteRecord(key);
     }
    }
   })
   .or(nextCache().flatMap(new Function<NormalizedCache, Optional<Record>>() {
    @Nonnull @Override
    public Optional<Record> apply(@Nonnull NormalizedCache cache) {
     return Optional.fromNullable(cache.loadRecord(key, cacheHeaders));
    }
   }))
   .orNull();
}

代码示例来源:origin: apollographql/apollo-android

@SuppressWarnings("unused") // compilation test
@Test
public void testSampleCodeFine1() {
 Optional<Number> optionalInt = Optional.of((Number) 1);
 Number value = optionalInt.or(0.5); // fine
}

代码示例来源:origin: com.amazonaws/aws-android-sdk-appsync-runtime

@Nullable @Override public Record loadRecord(@Nonnull final String key, @Nonnull final CacheHeaders cacheHeaders) {
 checkNotNull(key, "key == null");
 checkNotNull(cacheHeaders, "cacheHeaders == null");
 try {
  final Optional<Record> nonOptimisticRecord = nextCache()
    .flatMap(new Function<NormalizedCache, Optional<Record>>() {
     @Nonnull @Override public Optional<Record> apply(@Nonnull 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>() {
    @Nonnull @Override public Record apply(@Nonnull 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 testOrTAbsent() {
 assertEquals("default", Optional.absent().or("default"));
}

代码示例来源:origin: apollographql/apollo-android

@Test
public void testOrTPresent() {
 assertEquals("a", Optional.of("a").or("default"));
}

代码示例来源: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 testOrOptionalPresent() {
 assertEquals(Optional.of("a"), Optional.of("a").or(Optional.of("fallback")));
}

代码示例来源:origin: apollographql/apollo-android

@Test
public void testOrOptionalAbsent() {
 assertEquals(Optional.of("fallback"), Optional.absent().or(Optional.of("fallback")));
}

代码示例来源:origin: apollographql/apollo-android

if (subscriptionTransportFactory.isPresent()) {
 subscriptionManager = new RealSubscriptionManager(scalarTypeAdapters, subscriptionTransportFactory.get(),
   subscriptionConnectionParams.or(Collections.<String, Object>emptyMap()), dispatcher,
   subscriptionHeartbeatTimeout);

代码示例来源: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: 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;
}

代码示例来源: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);
 if (lruCache.getIfPresent(cacheKey.key()) != null) {
  lruCache.invalidate(cacheKey.key());
  result = true;
 }
 return result;
}

相关文章