Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License"). You may not
   * use this file except in compliance with the License. A copy of the License is
   * located at
   * 
   * http://aws.amazon.com/apache2.0
   * 
  * or in the "license" file accompanying this file. This file is distributed on
  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
  */
 package com.amazonaws.services.simpleworkflow.flow.core;
 
 import java.util.List;

It is an implementation of Promise, which exposes an additional set(java.lang.Object) and chain(com.amazonaws.services.simpleworkflow.flow.core.Promise) methods. Calling set(java.lang.Object) puts it in ready state, with value retrievable through get() method. chain(com.amazonaws.services.simpleworkflow.flow.core.Promise) links state of this Settable to another Promise. When another promise changes its state to ready the chained one is also becomes ready.

Use settable.set(null) to set Settable<Void> to the ready state.

Settable is frequently used to return data from code contained in anonymous classes. For example:

 
 Promise<Integer> foo() {
     final Settable<Integer> result = new Settable<Integer>();
     new TryCatch() {
                            
         protected void doTry() throws Throwable {
             Promise<Integer> activity1Result = activities.callActivity1();
             result.chain(activity1Result);
         }
                            
         protected void doCatch(Throwable e) throws Throwable {
             Promise<Void> handled = handleFailure(e);
             rethrow(e, handled);
         }
      };
      return result;
 }
 
 

Parameters:
<V> The type of value accepted and returned by this Settable.Use java.lang.Void to represent Promise that indicates completion of operation that doesn't return a value.
 
 public class Settable<V> extends Promise<V> {
 
     private final List<Runnablecallbacks = new ArrayList<Runnable>();
 
     private Runnable chainCallback;
 
     private Promise<V> chainedTo;
 
     private V value;
 
     private boolean ready;
 
     private String description;
 
     public Settable(V value) {
         set(value);
     }
 
     public Settable() {
     }

    

Returns:
The value passed in when set() is called
Throws:
java.lang.IllegalStateException If set() is never called for this instance of Settable
 
     @Override
     public V get() {
         if (!) {
             throw new IllegalStateException("not ready");
         }
         return ;
     }

    

Returns:
true if set() is called for this Settable
 
     @Override
    public boolean isReady() {
        return ;
    }

    

Parameters:
value Object to return when get() is called for this Settable. Use null to set Settable<Void> to the ready state.
Throws:
java.lang.IllegalStateException if the Promise is already in ready state
    public void set(V value) {
        if () {
            throw new IllegalStateException("already set to " + this.);
        }
        this. = value;
        this. = true;
        for (Runnable callback : ) {
            callback.run();
        }
    }

    
Used to chain this Settable with the passed in Promise. This allows the Settable to be ready whenever the passed in Promise is ready and the value for the chained Promise can be retrieved by calling get() method on this Settable.

Parameters:
chainTo Promise object to chain this Settable to. Chaining to null equates calling set(java.lang.Object) with null argument.
Throws:
java.lang.IllegalStateException if Settable is already in ready state or it is already chained to some other Promise
See also:
unchain()
    public void chain(final Promise<V> chainTo) {
        if () {
            throw new IllegalStateException("already ready");
        }
        if ( != null) {
            throw new IllegalStateException("Already chained. Call unchain() to get rid of the previous chaining.");
        }
        if (chainTo == null) {
            set(null);
            return;
        }
         = new Runnable() {
            @Override
            public void run() {
                set(chainTo.get());
            }
        };
        chainTo.addCallback();
         = chainTo;
    }

    
Used to unchain this Settable from the Promise object passed in last invocation of chain. There is no requirement to unchain unless there is a need to chain the Settable to another Promise. Such need usually appears when implementing recursive functions or in TryCatchFinally.doCatch(java.lang.Throwable) blocks. It is safe to call unchain if chain is never called for this Settable.

Throws:
java.lang.IllegalStateException If the Promise it is chained to is already in the ready state
    public void unchain() {
        if ( == null) {
            return;
        }
        if (.isReady()) {
            throw new IllegalStateException("Cannot unchain from a value which is ready");
        }
        if ( != null) {
            .removeCallback();
             = null;
             = null;
        }
    }
    protected void addCallback(Runnable callback) {
        if () {
            callback.run();
        }
        else {
            .add(callback);
        }
    }
    @Override
    protected void removeCallback(Runnable callback) {
        .remove(callback);
    }
    @Override
    public String getDescription() {
        if ( == null &&  != null) {
            return .getDescription();
        }
        return ;
    }

    

Parameters:
description human readable description of what this Promise represents.
See also:
Promise.getDescription()
    public void setDescription(String description) {
        this. = description;
    }
    @Override
    public String toString() {
        return "Settable [value=" +  + ", ready=" +  + "]";
    }
New to GrepCode? Check out our FAQ X