Example usage for java.util.concurrent CompletableFuture completedFuture

List of usage examples for java.util.concurrent CompletableFuture completedFuture

Introduction

In this page you can find the example usage for java.util.concurrent CompletableFuture completedFuture.

Prototype

public static <U> CompletableFuture<U> completedFuture(U value) 

Source Link

Document

Returns a new CompletableFuture that is already completed with the given value.

Usage

From source file:org.apache.james.backends.cassandra.migration.CassandraMigrationServiceTest.java

@Test
public void upgradeToVersionShouldThrowOnMissingVersion() {
    Map<SchemaVersion, Migration> allMigrationClazz = ImmutableMap.<SchemaVersion, Migration>builder()
            .put(OLDER_VERSION, successfulMigration).put(LATEST_VERSION, successfulMigration).build();
    testee = new CassandraMigrationService(schemaVersionDAO, allMigrationClazz, LATEST_VERSION);
    when(schemaVersionDAO.getCurrentSchemaVersion())
            .thenReturn(CompletableFuture.completedFuture(Optional.of(OLDER_VERSION)));

    expectedException.expect(NotImplementedException.class);

    testee.upgradeToVersion(LATEST_VERSION).run();
}

From source file:org.apache.james.backends.cassandra.migration.CassandraMigrationServiceTest.java

@Test
public void upgradeToVersionShouldUpdateIntermediarySuccessfulMigrationsInCaseOfError() {
    try {/*  w  ww.j a v a  2 s.  c  o  m*/
        Map<SchemaVersion, Migration> allMigrationClazz = ImmutableMap.<SchemaVersion, Migration>builder()
                .put(OLDER_VERSION, successfulMigration)
                .put(INTERMEDIARY_VERSION, () -> Migration.Result.PARTIAL)
                .put(LATEST_VERSION, successfulMigration).build();
        testee = new CassandraMigrationService(schemaVersionDAO, allMigrationClazz, LATEST_VERSION);
        when(schemaVersionDAO.getCurrentSchemaVersion())
                .thenReturn(CompletableFuture.completedFuture(Optional.of(OLDER_VERSION)));

        expectedException.expect(RuntimeException.class);

        testee.upgradeToVersion(LATEST_VERSION).run();
    } finally {
        verify(schemaVersionDAO).updateVersion(CURRENT_VERSION);
    }
}

From source file:org.apache.james.blob.memory.MemoryBlobStore.java

@Override
public CompletableFuture<BlobId> save(byte[] data) {
    Preconditions.checkNotNull(data);//  w w  w . j  ava  2s  . c  om
    BlobId blobId = factory.forPayload(data);

    blobs.put(blobId, data);

    return CompletableFuture.completedFuture(blobId);
}

From source file:org.apache.james.blob.memory.MemoryBlobStore.java

@Override
public CompletableFuture<byte[]> readBytes(BlobId blobId) {
    try {/* w  ww .j a  v  a2  s . c o  m*/
        return CompletableFuture.completedFuture(retrieveStoredValue(blobId));
    } catch (ObjectStoreException e) {
        return CompletableFutureUtil.exceptionallyFuture(e);
    }
}

From source file:org.apache.james.jmap.memory.access.MemoryAccessTokenRepository.java

@Override
public CompletableFuture<Void> addToken(String username, AccessToken accessToken) {
    Preconditions.checkNotNull(username);
    Preconditions.checkArgument(!username.isEmpty(), "Username should not be empty");
    Preconditions.checkNotNull(accessToken);
    synchronized (tokensExpirationDates) {
        tokensExpirationDates.put(accessToken, username);
    }/*from w  w  w . jav  a 2 s. c  o  m*/
    return CompletableFuture.completedFuture(null);
}

From source file:org.apache.james.jmap.memory.access.MemoryAccessTokenRepository.java

@Override
public CompletableFuture<Void> removeToken(AccessToken accessToken) {
    Preconditions.checkNotNull(accessToken);
    synchronized (tokensExpirationDates) {
        tokensExpirationDates.remove(accessToken);
    }//from   w  w  w .ja  va 2 s. c o  m
    return CompletableFuture.completedFuture(null);
}

From source file:org.apache.james.jmap.memory.access.MemoryAccessTokenRepository.java

@Override
public CompletableFuture<String> getUsernameFromToken(AccessToken accessToken) throws InvalidAccessToken {
    Preconditions.checkNotNull(accessToken);
    synchronized (tokensExpirationDates) {
        return CompletableFuture.completedFuture(
                Optional.ofNullable(tokensExpirationDates.get(accessToken)).<CompletionException>orElseThrow(
                        () -> new CompletionException(new InvalidAccessToken(accessToken))));
    }//w  ww. j av a 2  s . com
}

From source file:org.apache.james.mailbox.cassandra.mail.AttachmentLoader.java

public CompletableFuture<Stream<SimpleMailboxMessage>> addAttachmentToMessages(
        Stream<Pair<MessageWithoutAttachment, Stream<MessageAttachmentRepresentation>>> messageRepresentations,
        MessageMapper.FetchType fetchType) {

    if (fetchType == MessageMapper.FetchType.Body || fetchType == MessageMapper.FetchType.Full) {
        return FluentFutureStream
                .<SimpleMailboxMessage>of(messageRepresentations
                        .map(pair -> getAttachments(pair.getRight().collect(Guavate.toImmutableList()))
                                .thenApply(attachments -> pair.getLeft().toMailboxMessage(attachments))))
                .completableFuture();//w  w w . ja  v  a2  s  . c o m
    } else {
        return CompletableFuture.completedFuture(
                messageRepresentations.map(pair -> pair.getLeft().toMailboxMessage(ImmutableList.of())));
    }
}

From source file:org.apache.james.mailbox.cassandra.mail.AttachmentLoader.java

@VisibleForTesting
CompletableFuture<Map<AttachmentId, Attachment>> attachmentsById(Set<AttachmentId> attachmentIds) {
    if (attachmentIds.isEmpty()) {
        return CompletableFuture.completedFuture(ImmutableMap.of());
    }/* w  w  w .jav  a 2 s  .co  m*/
    return attachmentMapper.getAttachmentsAsFuture(attachmentIds).thenApply(attachments -> attachments.stream()
            .collect(Guavate.toImmutableMap(Attachment::getAttachmentId, Function.identity())));
}

From source file:org.apache.james.mailbox.cassandra.mail.CassandraMailboxMapper.java

@Override
public Mailbox findMailboxByPath(MailboxPath path) throws MailboxException {
    try {//w  w  w.  j a  v a  2  s . c  om
        return mailboxPathDAO.retrieveId(path).thenCompose(cassandraIdOptional -> cassandraIdOptional
                .map(CassandraMailboxPathDAO.CassandraIdAndPath::getCassandraId)
                .map(mailboxDAO::retrieveMailbox).orElse(CompletableFuture.completedFuture(Optional.empty())))
                .join().orElseThrow(() -> new MailboxNotFoundException(path));
    } catch (CompletionException e) {
        if (e.getCause() instanceof InvalidQueryException) {
            if (StringUtils.containsIgnoreCase(e.getCause().getMessage(), VALUES_MAY_NOT_BE_LARGER_THAN_64_K)) {
                throw new TooLongMailboxNameException("too long mailbox name");
            }
            throw new MailboxException("It has error with cassandra storage", e.getCause());
        }
        throw e;
    }
}