Guava ListenableFuture - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Guava ListenableFuture class.

This code example shows how to use the following methods:get, isDone
   * 
   * @return true if the specified future has been resolved without throwing an exception or being 
   *     cancelled. 
   */ 
  public static boolean isSuccess(ListenableFuture<?> future) throws InterruptedException {
    if (!future.isDone()) {
      return false; 
    } 
 
    try { 
      // Try to get the future, but ignore (and preserve) the thread interrupted state. 
      // This should be fast because we know the future is already complete. 
      future.get();
      return true; 
    } catch (ExecutionException e) {
      // The computation threw an exception, so it did not complete successfully. 
      return false; 
    } catch (CancellationException e) {
      // The computation was cancelled, so it did not complete successfully. 
      return false; 
6
CodeRank
This code example shows how to use the following methods:get
   private static <T> Iterable<T> unwrap(Iterable<ListenableFuture<? extends T>> values) { 
      return transform(values, new Function<ListenableFuture<? extends T>, T>() { 
         @Override 
         public T apply(ListenableFuture<? extends T> from) { 
            try { 
               return from.get(); 
            } catch (InterruptedException e) { 
               propagate(e); 
            } catch (ExecutionException e) { 
               propagate(e); 
            } 
            return null; 
         } 
          
         @Override 
         public String toString() { 
            return "callGetOnFuture()"; 
         } 
      }); 
   } 
5
CodeRank
This code example shows how to use the following methods:isDone
    } 
 
    public void reserveMemory(long bytes)
    { 
        ListenableFuture<?> future = driverContext.reserveMemory(bytes);
        if (!future.isDone()) {
            SettableFuture<?> currentMemoryFuture = memoryFuture.get();
            while (currentMemoryFuture.isDone()) {
                SettableFuture<?> settableFuture = SettableFuture.create();
                // We can't replace one that's not done, because the task may be blocked on that future 
                if (memoryFuture.compareAndSet(currentMemoryFuture, settableFuture)) {
                    currentMemoryFuture = settableFuture;
                } 
                else { 
                    currentMemoryFuture = memoryFuture.get();
                } 
            } 
 
            SettableFuture<?> finalMemoryFuture = currentMemoryFuture;
            // Create a new future, so that this operator can un-block before the pool does, if it's moved to a new pool 
4
CodeRank
This code example shows how to use the following methods:isDone
        { 
            @Override 
            public Boolean call()
                    throws Exception
            { 
                return driver.processFor(new Duration(1, TimeUnit.MILLISECONDS)).isDone();
            } 
        }); 
        brokenOperator.waitForLocked();
 
        driver.close();
        assertTrue(driver.isFinished());
 
        try { 
            driverProcessFor.get(1, TimeUnit.SECONDS);
            fail("Expected InterruptedException"); 
        } 
        catch (ExecutionException e) {
            checkArgument(getRootCause(e) instanceof InterruptedException, "Expected root cause exception to be an instance of InterruptedException");
        } 
4
CodeRank
This code example shows how to use the following methods:isDone
        assertTrue(sink.waitForWriting().isDone());
 
        // this will be ignored 
        sink.addPage(createPage(0));
        assertTrue(sink.isFinished());
        assertTrue(sink.waitForWriting().isDone());
    } 
 
    private static void assertExchangeTotalBufferedBytes(LocalExchange exchange, int pageCount)
    { 
        assertEquals(exchange.getBufferedBytes(), retainedSizeOfPages(pageCount));
    } 
 
    private static void assertExchangeTotalBufferedPages(LocalExchange exchange, int pageCount)
    { 
        assertEquals(exchange, retainedSizeOfPages(pageCount));
    } 
 
    private static Page createPage(int i)
    { 
4
CodeRank
This code example shows how to use the following methods:isDone
    } 
 
    private static ListenableFuture<?> enqueuePage(SharedOutputBuffer sharedBuffer, Page page, int partition)
    { 
        ListenableFuture<?> future = sharedBuffer.enqueue(partition, page);
        assertFalse(future.isDone());
        return future;
    } 
 
    private static void addPage(SharedOutputBuffer sharedBuffer, Page page)
    { 
        addPage(sharedBuffer, page, DEFAULT_PARTITION);
    } 
 
    private static void addPage(SharedOutputBuffer sharedBuffer, Page page, int partition)
    { 
        assertTrue(sharedBuffer.enqueue(partition, page).isDone());
    } 
 
    private static void assertQueueState( 
4
CodeRank
This code example shows how to use the following methods:isDone
        return state == State.RUNNING && blocked == NOT_BLOCKED;
    } 
 
    private void updateBlockedIfNecessary() 
    { 
        if (blocked != NOT_BLOCKED && blocked.isDone()) {
            blocked = NOT_BLOCKED;
        } 
    } 
 
    @Override 
    public void addInput(Page page)
    { 
        requireNonNull(page, "page is null");
        checkState(needsInput(), "Operator does not need input");
 
        Block[] blocks = new Block[inputChannels.size()];
        for (int outputChannel = 0; outputChannel < inputChannels.size(); outputChannel++) {
            blocks[outputChannel] = page.getBlock(inputChannels.get(outputChannel));
        } 
4
CodeRank
This code example shows how to use the following methods:isDone
    } 
 
    @Override 
    public boolean isFinished() 
    { 
        if (blocked != NOT_BLOCKED && blocked.isDone()) {
            blocked = NOT_BLOCKED;
        } 
 
        return finished && blocked == NOT_BLOCKED;
    } 
 
    @Override 
    public ListenableFuture<?> isBlocked()
    { 
        if (blocked != NOT_BLOCKED && blocked.isDone()) {
            blocked = NOT_BLOCKED;
        } 
        return blocked;
    } 
4
CodeRank
This code example shows how to use the following methods:isDone
This code example shows how to use the following methods:isDone
    } 
 
    @Override 
    public boolean needsInput() 
    { 
        return !isFinished() && isBlocked().isDone();
    } 
 
    @Override 
    public void addInput(Page page)
    { 
        requireNonNull(page, "page is null");
        page = pagePreprocessor.apply(page);
        sink.addPage(page);
        operatorContext.recordGeneratedOutput(page.getSizeInBytes(), page.getPositionCount());
    } 
 
    @Override 
    public Page getOutput()
    { 
4
CodeRank
See Code Examples for Guava ListenableFuture Methods: