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())); }
@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(); }
@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(); }
private Set<MailboxId> listTargetMailboxIds(UpdateMessagePatch updateMessagePatch) { return OptionalUtils.toStream(updateMessagePatch.getMailboxIds()) .flatMap(Collection::stream) .map(mailboxIdFactory::fromString) .collect(Guavate.toImmutableSet()); }
private Stream<String> getMessageSubjects(Mail mail) throws MessagingException { MimeMessage message = mail.getMessage(); if (message != null) { return OptionalUtils.toStream( Optional.ofNullable(message.getSubject())); } return Stream.of(); }
private static List<Right> fromACL(MailboxACL.Rfc4314Rights rights) { return rights.list() .stream() .flatMap(right -> OptionalUtils.toStream(Right.forRight(right))) .collect(Guavate.toImmutableList()); }
@GET @Path(ROOT_PATH) @ApiOperation(value = "getting groups list") @ApiResponses(value = { @ApiResponse(code = HttpStatus.OK_200, message = "OK", response = List.class), @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.") }) public Set<String> listGroups(Request request, Response response) throws RecipientRewriteTableException { return Optional.ofNullable(recipientRewriteTable.getAllMappings()) .map(mappings -> mappings.entrySet().stream() .filter(e -> e.getValue().contains(Mapping.Type.Group)) .map(Map.Entry::getKey) .flatMap(source -> OptionalUtils.toStream(source.asMailAddress())) .map(MailAddress::asString) .collect(Guavate.toImmutableSortedSet())) .orElse(ImmutableSortedSet.of()); }
@GET @Path(ROOT_PATH) @ApiOperation(value = "getting forwards list") @ApiResponses(value = { @ApiResponse(code = HttpStatus.OK_200, message = "OK", response = List.class), @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.") }) public Set<String> listForwards(Request request, Response response) throws RecipientRewriteTableException { return Optional.ofNullable(recipientRewriteTable.getAllMappings()) .map(mappings -> mappings.entrySet().stream() .filter(e -> e.getValue().contains(Mapping.Type.Forward)) .map(Map.Entry::getKey) .flatMap(source -> OptionalUtils.toStream(source.asMailAddress())) .map(MailAddress::asString) .collect(Guavate.toImmutableSortedSet())) .orElse(ImmutableSortedSet.of()); }
@Override public Optional<QuotaSize> getMaxStorage(QuotaRoot quotaRoot) { Supplier<Optional<QuotaSize>> domainQuotaSupplier = Throwing.supplier(() -> quotaRoot.getDomain() .flatMap(this::getDomainMaxStorage)).sneakyThrow(); Supplier<Optional<QuotaSize>> globalDomainSupplier = Throwing.supplier(this::getGlobalMaxStorage).sneakyThrow(); return Stream .of(() -> perUserQuota.getMaxStorage(quotaRoot), domainQuotaSupplier, globalDomainSupplier) .flatMap(supplier -> OptionalUtils.toStream(supplier.get())) .findFirst(); }
@Override public Optional<QuotaCount> getMaxMessage(QuotaRoot quotaRoot) { Supplier<Optional<QuotaCount>> domainQuotaSupplier = Throwing.supplier(() -> quotaRoot.getDomain() .flatMap(this::getDomainMaxMessage)).sneakyThrow(); Supplier<Optional<QuotaCount>> globalDomainSupplier = Throwing.supplier(this::getGlobalMaxMessage).sneakyThrow(); return Stream .of(() -> perUserQuota.getMaxMessage(quotaRoot), domainQuotaSupplier, globalDomainSupplier) .flatMap(supplier -> OptionalUtils.toStream(supplier.get())) .findFirst(); }
private Stream<String> toAttachmentContent(Attachment attachment) { try { return OptionalUtils.toStream( textExtractor .extractContent( attachment.getStream(), attachment.getType()) .getTextualContent()); } catch (Exception e) { LOGGER.error("Error while parsing attachment content", e); return Stream.of(); } }
@Test void toStreamShouldConvertFullOptionalToStream() { long value = 18L; assertThat(OptionalUtils.toStream(Optional.of(value))) .containsExactly(value); }
@Override public Stream<MessageManager> getMailboxByRole(Role aRole, MailboxSession session) { if (aRole.equals(Role.OUTBOX)) { return OptionalUtils.toStream(outboxSupplier.get()); } else if (aRole.equals(Role.DRAFTS)) { return OptionalUtils.toStream(draftsSupplier.get()); } return Stream.empty(); } }
@Test void toStreamShouldConvertEmptyOptionalToEmptyStream() { assertThat(OptionalUtils.toStream(Optional.empty())) .isEmpty(); }
@Test public void toStreamShouldConvertEmptyOptionalToEmptyStream() { assertThat( OptionalUtils.toStream(Optional.empty()) .collect(Guavate.toImmutableList())) .isEmpty(); }
@Test public void toStreamShouldConvertFullOptionalToStream() { long value = 18L; assertThat( OptionalUtils.toStream(Optional.of(value)) .collect(Guavate.toImmutableList())) .containsExactly(value); }