001    // Copyright 2010 Waterken Inc. under the terms of the MIT X license
002    // found at http://www.opensource.org/licenses/mit-license.html
003    package org.waterken.delayed;
004    
005    import java.io.Serializable;
006    
007    import org.ref_send.list.List;
008    import org.ref_send.promise.Deferred;
009    import org.ref_send.promise.Do;
010    import org.ref_send.promise.Eventual;
011    import org.ref_send.promise.Promise;
012    
013    /**
014     * A delayed resolution test.
015     */
016    public final class
017    Relay {
018        private Relay() {}
019        
020        static public Promise<?>
021        make(final Eventual _, final Forwarder forwarder_) {
022            class Return extends Do<Boolean,Promise<Boolean>>
023                         implements Serializable {
024                static private final long serialVersionUID = 1L;
025    
026                public Promise<Boolean>
027                fulfill(final Boolean value) { return Eventual.ref(value); }
028            }
029            class Extract extends Do<Deferred<Boolean>,Promise<Boolean>>
030                          implements Serializable {
031                static private final long serialVersionUID = 1L;
032    
033                public Promise<Boolean>
034                fulfill(final Deferred<Boolean> forward) throws Exception {
035                    return _.when(forward.promise, new Return());
036                }
037            }
038            class Resolve extends Do<Deferred<Boolean>,Void> implements Serializable{
039                static private final long serialVersionUID = 1L;
040                
041                public Void
042                fulfill(final Deferred<Boolean> forward) throws Exception {
043                  forward.resolver.apply(true);
044                  return null;
045                }
046            }
047            final Promise<Deferred<Boolean>> forward = forwarder_.forward(); 
048            final Promise<Boolean> r = _.when(forward, new Extract());
049            _.when(forward, new Resolve());
050            return r;
051        }
052    
053        static public void
054        main(final String[] args) throws Exception {
055            final List<Promise<?>> work = List.list();
056            final Eventual _ = new Eventual(work.appender());
057            final Promise<?> result = make(_, ForwarderMaker.make(_));
058            while (!work.isEmpty()) { work.pop().call(); }
059            result.call();
060        }
061    }