public String getBodyTemplate(QuotaThreshold quotaThreshold) { return OptionalUtils.or( Optional.ofNullable( toRenderingInformation.get(quotaThreshold)) .flatMap(RenderingInformation::getBodyTemplate), bodyTemplate) .orElse(DEFAULT_BODY_TEMPLATE); }
public Keywords asKeywords() { return Keywords.factory() .fromSet(StreamUtils .flatten( OptionalUtils.toStream(isAnswered.filter(b -> b).map(b -> Keyword.ANSWERED)), OptionalUtils.toStream(isDraft.filter(b -> b).map(b -> Keyword.DRAFT)), OptionalUtils.toStream(isForwarded.filter(b -> b).map(b -> Keyword.FORWARDED)), OptionalUtils.toStream(isFlagged.filter(b -> b).map(b -> Keyword.FLAGGED)), OptionalUtils.toStream(isUnread.filter(b -> !b).map(b -> Keyword.SEEN))) .collect(Guavate.toImmutableSet())); }
public static Optional<Right> forRight(MailboxACL.Right right) { return OptionalUtils.executeIfEmpty( Arrays.stream(values()) .filter(jmapRight -> jmapRight.right == right) .findAny(), () -> LOGGER.warn("Non handled right '{}'", right)); }
@Override protected Mappings mapAddress(String user, Domain domain) { return OptionalUtils.orSuppliers( () -> retrieveMappings(MappingSource.fromUser(user, domain)), () -> retrieveMappings(MappingSource.fromDomain(domain))) .orElse(MappingsImpl.empty()); }
@Test public void containsDifferentShouldReturnFalseWhenEmpty() throws Exception { assertThat(OptionalUtils.containsDifferent(Optional.empty(), "any")).isFalse(); }
boolean matchesIgnoreCase(AddressHeader other) { boolean sameAddress = OptionalUtils.matches(address, other.address, String::equalsIgnoreCase); boolean samePersonal = OptionalUtils.matches(personal, other.personal, String::equalsIgnoreCase); boolean personalMatchesAddress = OptionalUtils.matches(personal, other.address, String::equalsIgnoreCase); boolean addressMatchesPersonal = OptionalUtils.matches(address, other.personal, String::equalsIgnoreCase); return fullAddress.equalsIgnoreCase(other.fullAddress) || (sameAddress && samePersonal) || (sameAddress && !personal.isPresent()) || (samePersonal && !address.isPresent()) || (personalMatchesAddress && sameAddress) || (addressMatchesPersonal && samePersonal); } }
@SafeVarargs public static <T> Optional<T> or(Optional<T>... optionals) { return orStream(Arrays.stream(optionals)); }
@Test void orSuppliersShouldReturnFirstValueWhenBothValues() { assertThat( OptionalUtils.orSuppliers( () -> Optional.of(1), () -> Optional.of(2))) .contains(1); }
@Test public void containsDifferentShouldReturnTrueWhenDifferentValue() throws Exception { assertThat(OptionalUtils.containsDifferent(Optional.of("any"), "other")).isTrue(); }
@Test void matchesShouldReturnFalseWhenConditionIsNotMatching() { assertThat( OptionalUtils.matches( Optional.of(42), Optional.of(43), Integer::equals)) .isFalse(); }
@SafeVarargs public static <T> Optional<T> or(Optional<T>... optionals) { return orStream(Arrays.stream(optionals)); }
public String getSubjectTemplate(QuotaThreshold quotaThreshold) { return OptionalUtils.or( Optional.ofNullable( toRenderingInformation.get(quotaThreshold)) .flatMap(RenderingInformation::getSubjectTemplate), subjectTemplate) .orElse(DEFAULT_SUBJECT_TEMPLATE); }
@Override public Optional<QuotaSize> getMaxStorage(QuotaRoot quotaRoot) { return Stream .of((Supplier<Optional<QuotaSize>>) () -> dao.getMaxStorage(quotaRoot), () -> quotaRoot.getDomain().flatMap(this::getDomainMaxStorage), this::getGlobalMaxStorage) .flatMap(supplier -> OptionalUtils.toStream(supplier.get())) .findFirst(); }
@Test void orSuppliersShouldReturnEmptyWhenBothEmpty() { assertThat( OptionalUtils.orSuppliers( () -> Optional.empty(), () -> Optional.empty())) .isEmpty(); }
private CompletableFuture<Optional<ComposedMessageIdWithMetaData>> retrieveComposedId(CassandraId mailboxId, MessageUid uid) { return messageIdDAO.retrieve(mailboxId, uid) .thenApply(optional -> OptionalUtils.executeIfEmpty(optional, () -> LOGGER.warn("Could not retrieve message {} {}", mailboxId, uid))); }
@Test public void containsDifferentShouldReturnFalseWhenSameValue() throws Exception { assertThat(OptionalUtils.containsDifferent(Optional.of("any"), "any")).isFalse(); }
@Test void matchesShouldReturnTrueWhenConditionIsMatching() { assertThat( OptionalUtils.matches( Optional.of(42), Optional.of(42), Integer::equals)) .isTrue(); }
@SafeVarargs public static <T> Optional<T> or(Optional<T>... optionals) { return orStream(Arrays.stream(optionals)); }
public Keywords computeKeywords(Optional<Keywords> keywords, Optional<OldKeyword> oldKeywords) { Preconditions.checkArgument(!(keywords.isPresent() && oldKeywords.isPresent()), "Does not support keyword and is* at the same time"); return OptionalUtils .or(keywords, oldKeywords.map(OldKeyword::asKeywords)) .orElse(Keywords.DEFAULT_VALUE); }
@Override public Optional<QuotaCount> getMaxMessage(QuotaRoot quotaRoot) { return Stream .of((Supplier<Optional<QuotaCount>>) () -> dao.getMaxMessage(quotaRoot), () -> quotaRoot.getDomain().flatMap(this::getDomainMaxMessage), this::getGlobalMaxMessage) .flatMap(supplier -> OptionalUtils.toStream(supplier.get())) .findFirst(); }