Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 82   Methods: 4
NCLOC: 22   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ComposedContinuation.java 100% 100% 100% 100%
coverage
 1    /*BEGIN_COPYRIGHT_BLOCK*
 2   
 3    PLT Utilities BSD License
 4   
 5    Copyright (c) 2007-2010 JavaPLT group at Rice University
 6    All rights reserved.
 7   
 8    Developed by: Java Programming Languages Team
 9    Rice University
 10    http://www.cs.rice.edu/~javaplt/
 11   
 12    Redistribution and use in source and binary forms, with or without modification, are permitted
 13    provided that the following conditions are met:
 14   
 15    - Redistributions of source code must retain the above copyright notice, this list of conditions
 16    and the following disclaimer.
 17    - Redistributions in binary form must reproduce the above copyright notice, this list of
 18    conditions and the following disclaimer in the documentation and/or other materials provided
 19    with the distribution.
 20    - Neither the name of the JavaPLT group, Rice University, nor the names of the library's
 21    contributors may be used to endorse or promote products derived from this software without
 22    specific prior written permission.
 23   
 24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 25    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 26    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND
 27    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 28    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 29    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 30    IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 31    OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32   
 33    *END_COPYRIGHT_BLOCK*/
 34   
 35    package edu.rice.cs.plt.recur;
 36   
 37    import edu.rice.cs.plt.lambda.Lambda;
 38   
 39    /**
 40    * A continuation defined in terms of two other continuations, where the second is parameterized
 41    * by the result of the first. This class is essential to correctly handling general recursion.
 42    * Its {@link #step} and {@link #compose} methods are defined so that iterative computation
 43    * does not require "drilling down" to an unbounded depth in order to locate and perform the
 44    * next evaluation step.
 45    */
 46    public class ComposedContinuation<T, R> extends PendingContinuation<R> {
 47   
 48    private final Continuation<? extends T> _first;
 49    private final Lambda<? super T, ? extends Continuation<? extends R>> _rest;
 50   
 51  4101 public ComposedContinuation(Continuation<? extends T> first,
 52    Lambda<? super T, ? extends Continuation<? extends R>> rest) {
 53  4101 _first = first;
 54  4101 _rest = rest;
 55    }
 56   
 57    /**
 58    * If {@code first} is resolved, apply {@code rest} to compute the second. Otherwise,
 59    * invoke {@code first.step()}, and then "push" {@code rest} into the result by invoking
 60    * {@code compose()}.
 61    */
 62  2571 public Continuation<? extends R> step() {
 63  1549 if (_first.isResolved()) { return _rest.value(_first.value()); }
 64  1022 else { return _first.step().compose(_rest); }
 65    }
 66   
 67    /**
 68    * Create a new {@code ComposedContinuation} with the same {@code first}, but with a {@code rest}
 69    * that will compose {@code c} onto the result of this object's {@code rest} function.
 70    * (Note that the default behavior -- nesting this object in another {@code ComposedContinuation}
 71    * with {@code c} as its {@code rest} -- results in continuations whose {@code step()} methods
 72    * are invoked recursively to an unbounded depth.)
 73    */
 74  1530 public <S> Continuation<S> compose(final Lambda<? super R, ? extends Continuation<? extends S>> c) {
 75  1530 return new ComposedContinuation<T, S>(_first, new Lambda<T, Continuation<? extends S>>() {
 76  1530 public Continuation<? extends S> value(T arg) {
 77  1530 return _rest.value(arg).compose(c);
 78    }
 79    });
 80    }
 81   
 82    }