Example usage for com.google.common.util.concurrent SettableFuture set

List of usage examples for com.google.common.util.concurrent SettableFuture set

Introduction

In this page you can find the example usage for com.google.common.util.concurrent SettableFuture set.

Prototype

@Override
    public boolean set(@Nullable V value) 

Source Link

Usage

From source file:org.testfx.util.WaitForAsyncUtils.java

/**
 * Waits for given observable {@link ObservableBooleanValue} to return (push) {@code true},
 * otherwise times out with {@link TimeoutException}.
 *
 * @param timeout the timeout//from ww  w. ja va  2s  .c  o m
 * @param timeUnit the time unit
 * @param booleanValue the observable
 * @throws TimeoutException
 */
public static void waitFor(long timeout, TimeUnit timeUnit, ObservableBooleanValue booleanValue)
        throws TimeoutException {
    SettableFuture<Void> future = SettableFuture.create();
    ChangeListener<Boolean> changeListener = (observable, oldValue, newValue) -> {
        if (newValue) {
            future.set(null);
        }
    };
    booleanValue.addListener(changeListener);
    if (!booleanValue.get()) {
        waitFor(timeout, timeUnit, future);
    }
    booleanValue.removeListener(changeListener);
}

From source file:io.datakernel.service.ServiceAdapters.java

private static CompletionCallback toCompletionCallback(final SettableFuture<?> future) {
    return new CompletionCallback() {
        @Override//from ww  w  . j  a  va2s .c  om
        public void onComplete() {
            future.set(null);
        }

        @Override
        public void onException(Exception exception) {
            future.setException(exception);
        }
    };
}

From source file:io.druid.java.util.common.concurrent.ListenableFutures.java

/**
 * Guava 19 changes the Futures.transform signature so that the async form is different. This is here as a
 * compatability layer until such a time as druid only supports Guava 19 or later, in which case
 * Futures.transformAsync should be used
 *
 * This is NOT copied from guava./*from www  . j  a  v a2  s .co m*/
 */
public static <I, O> ListenableFuture<O> transformAsync(final ListenableFuture<I> inFuture,
        final Function<I, ListenableFuture<O>> transform) {
    final SettableFuture<O> finalFuture = SettableFuture.create();
    Futures.addCallback(inFuture, new FutureCallback<I>() {
        @Override
        public void onSuccess(@Nullable I result) {
            final ListenableFuture<O> transformFuture = transform.apply(result);
            Futures.addCallback(transformFuture, new FutureCallback<O>() {
                @Override
                public void onSuccess(@Nullable O result) {
                    finalFuture.set(result);
                }

                @Override
                public void onFailure(Throwable t) {
                    finalFuture.setException(t);
                }
            });
        }

        @Override
        public void onFailure(Throwable t) {
            finalFuture.setException(t);
        }
    });
    return finalFuture;
}

From source file:org.opendaylight.controller.cluster.datastore.admin.ClusterAdminRpcService.java

private static void onMessageFailure(String msg, final SettableFuture<RpcResult<Void>> returnFuture,
        Throwable failure) {/*from   w  w w .j  a va 2 s .  c  om*/
    LOG.error(msg, failure);
    returnFuture.set(ClusterAdminRpcService
            .<Void>newFailedRpcResultBuilder(String.format("%s: %s", msg, failure.getMessage())).build());
}

From source file:com.continuuity.loom.common.zookeeper.ZKClientExt.java

/**
 * Acts as {@link ZKClient#create(String, byte[], org.apache.zookeeper.CreateMode, boolean)
 * create(path, null, CreateMode.PERSISTENT, true)} if node doesn't exist. Otherwise has no affect.
 * In latter case sets {@code null} in returned future.
 *//*from   w  w w .j av  a 2  s  .c  om*/
public static ListenableFuture<String> ensureExists(final ZKClient zkClient, final String path) {
    final SettableFuture<String> resultFuture = SettableFuture.create();
    OperationFuture<String> createFuture = zkClient.create(path, null, CreateMode.PERSISTENT, true);
    Futures.addCallback(createFuture, new FutureCallback<String>() {
        @Override
        public void onSuccess(String result) {
            resultFuture.set(result);
        }

        @Override
        public void onFailure(Throwable t) {
            if (causedBy(t, KeeperException.NodeExistsException.class)) {
                resultFuture.set(path);
            } else {
                resultFuture.setException(t);
            }
        }
    });

    return resultFuture;
}

From source file:com.microsoft.office365.http.SharepointCookieCredentials.java

public static ListenableFuture<CookieCredentials> requestCredentials(String sharepointSiteUrl,
        Activity activity) {//from  w w w .  ja  va 2  s . com
    final SettableFuture<CookieCredentials> future = SettableFuture.create();

    ListenableFuture<String> login = showLoginForCookies(activity, sharepointSiteUrl);

    Futures.addCallback(login, new FutureCallback<String>() {
        @Override
        public void onFailure(Throwable t) {
            future.setException(t);
        }

        @Override
        public void onSuccess(String cookies) {
            future.set(new CookieCredentials(cookies));
        }
    });

    return future;
}

From source file:com.microsoft.sharepointservices.http.SharepointCookieCredentials.java

/**
 * Request credentials.//from w ww.ja v a  2  s.  c  o m
 *
 * @param sharepointSiteUrl the sharepoint site url
 * @param activity the activity
 * @return the listenable future
 */
public static ListenableFuture<CookieCredentials> requestCredentials(String sharepointSiteUrl,
        Activity activity) {
    final SettableFuture<CookieCredentials> future = SettableFuture.create();

    ListenableFuture<String> login = showLoginForCookies(activity, sharepointSiteUrl);

    Futures.addCallback(login, new FutureCallback<String>() {
        @Override
        public void onFailure(Throwable t) {
            future.setException(t);
        }

        @Override
        public void onSuccess(String cookies) {
            future.set(new CookieCredentials(cookies));
        }
    });

    return future;
}

From source file:org.opendaylight.controller.cluster.datastore.admin.ClusterAdminRpcService.java

private static void saveSnapshotsToFile(DatastoreSnapshotList snapshots, String fileName,
        SettableFuture<RpcResult<Void>> returnFuture) {
    try (FileOutputStream fos = new FileOutputStream(fileName)) {
        SerializationUtils.serialize(snapshots, fos);

        returnFuture.set(newSuccessfulResult());
        LOG.info("Successfully backed up datastore to file {}", fileName);
    } catch (Exception e) {
        onDatastoreBackupFailure(fileName, returnFuture, e);
    }//from  ww  w . j a v  a 2s.  c  o m
}

From source file:org.opendaylight.openflowplugin.openflow.md.util.RoleUtil.java

/**
 * @param sessionContext switch session context
 * @return generationId from future RpcResult
 *///from  www.ja  v a2  s .  com
public static Future<BigInteger> readGenerationIdFromDevice(SessionContext sessionContext) {
    Future<RpcResult<RoleRequestOutput>> roleReply = sendRoleChangeRequest(sessionContext, OfpRole.NOCHANGE,
            BigInteger.ZERO);
    final SettableFuture<BigInteger> result = SettableFuture.create();

    Futures.addCallback(JdkFutureAdapters.listenInPoolThread(roleReply),
            new FutureCallback<RpcResult<RoleRequestOutput>>() {
                @Override
                public void onSuccess(RpcResult<RoleRequestOutput> input) {
                    if (input != null && input.getResult() != null) {
                        result.set(input.getResult().getGenerationId());
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    //TODO
                }
            });
    return result;
}

From source file:com.google.devtools.kythe.extractors.shared.ExtractorUtils.java

public static List<FileData> processRequiredInputs(Iterable<String> files) throws ExtractionException {
    final SettableFuture<ExtractionException> exception = SettableFuture.create();

    List<FileData> result = Lists.newArrayList(Iterables.transform(files, new Function<String, FileData>() {
        @Override// w w w  .  java2 s. c om
        public FileData apply(String path) {
            byte[] content = new byte[0];
            try {
                content = Files.toByteArray(new File(path));
            } catch (IOException e) {
                exception.set(new ExtractionException(e, false));
            }
            if (content == null) {
                exception.set(new ExtractionException(String.format("Unable to locate required input %s", path),
                        false));
                return null;
            }
            String digest = getContentDigest(content);
            return createFileData(path, digest, content);
        }
    }));
    if (exception.isDone()) {
        try {
            throw exception.get();
        } catch (InterruptedException e) {
            throw new ExtractionException(e, true);
        } catch (ExecutionException e) {
            throw new ExtractionException(e, false);
        }
    }
    return result;
}