Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 158   Methods: 25
NCLOC: 80   Classes: 7
 
 Source file Conditionals Statements Methods TOTAL
ConsVisitor.java 100% 76% 68% 73.1%
coverage 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.collect;
 36   
 37    import java.io.Serializable;
 38    import edu.rice.cs.plt.lambda.Lambda;
 39    import edu.rice.cs.plt.lambda.Predicate;
 40   
 41    /**
 42    * A visitor for {@link ConsList}s. Implementations handle the two list variants:
 43    * {@link ConsList.Empty} and {@link ConsList.Nonempty}. For convenience, visitors may
 44    * also be treated as {@code Lambda}s -- the {@code value} method is implemented to
 45    * apply the visitor. A number of standard list visitors are also provided, either as static
 46    * fields or static methods.
 47    */
 48    public abstract class ConsVisitor<T, Ret> implements Lambda<ConsList<? extends T>, Ret> {
 49   
 50    /** Handle an empty list */
 51    public abstract Ret forEmpty();
 52   
 53    /** Handle a nonempty list */
 54    public abstract Ret forNonempty(T first, ConsList<? extends T> rest);
 55   
 56    /** Invoke {@code list.apply(this)} */
 57  8 public Ret value(ConsList<? extends T> list) { return list.apply(this); }
 58   
 59   
 60    /** Attempt to access the first of the given list (throws an exception in the empty case). */
 61  0 @SuppressWarnings("unchecked")
 62  0 public static final <T> ConsVisitor<T, T> first() { return (First<T>) First.INSTANCE; }
 63   
 64    private static class First<T> extends ConsVisitor<T, T> implements Serializable {
 65    private static final First<Object> INSTANCE = new First<Object>();
 66  0 private First() {}
 67  0 public T forEmpty() {
 68  0 throw new IllegalArgumentException("Empty ConsList has no first");
 69    }
 70  0 public T forNonempty(T first, ConsList<? extends T> rest) {
 71  0 return first;
 72    }
 73    }
 74   
 75   
 76    /** Attempt to access the rest of the given list (throws an exception in the empty case). */
 77  0 @SuppressWarnings("unchecked")
 78    public static final <T> ConsVisitor<T, ConsList<? extends T>> rest() {
 79  0 return (Rest<T>) Rest.INSTANCE;
 80    }
 81   
 82    private static class Rest<T> extends ConsVisitor<T, ConsList<? extends T>> implements Serializable {
 83    private static final Rest<Object> INSTANCE = new Rest<Object>();
 84  0 private Rest() {}
 85  0 public ConsList<? extends T> forEmpty() {
 86  0 throw new IllegalArgumentException("Empty ConsList has no rest");
 87    }
 88  0 public ConsList<? extends T> forNonempty(T first, ConsList<? extends T> rest) {
 89  0 return rest;
 90    }
 91    }
 92   
 93   
 94    /** Reverses the order of the elements in a list */
 95  9 public static <T> ConsVisitor<T, ConsList<? extends T>> reverse() {
 96  9 return new ReverseHelper<T>(ConsList.<T>empty());
 97    }
 98   
 99    /** Reverses the list and appends {@code toAppend} to the end of it */
 100    private static class ReverseHelper<T> extends ConsVisitor<T, ConsList<? extends T>> implements Serializable {
 101    private ConsList<? extends T> _toAppend;
 102   
 103  27 public ReverseHelper(ConsList<? extends T> toAppend) { _toAppend = toAppend; }
 104   
 105  12 public ConsList<? extends T> forEmpty() { return _toAppend; }
 106   
 107  18 public ConsList<? extends T> forNonempty(T first, ConsList<? extends T> rest) {
 108  18 return rest.apply(new ReverseHelper<T>(ConsList.cons(first, _toAppend)));
 109    }
 110    }
 111   
 112   
 113    /** Appends the given list to the end of another list */
 114  10 public static <T> ConsVisitor<T, ConsList<? extends T>> append(final ConsList<? extends T> rest) {
 115  10 return new Append<T>(rest);
 116    }
 117   
 118    private static class Append<T> extends ConsVisitor<T, ConsList<? extends T>> implements Serializable {
 119    private final ConsList<? extends T> _toAppend;
 120  10 public Append(ConsList<? extends T> toAppend) { _toAppend = toAppend; }
 121  10 public ConsList<? extends T> forEmpty() { return _toAppend; }
 122  12 public ConsList<? extends T> forNonempty(T first, ConsList<? extends T> rest) {
 123  12 return ConsList.cons(first, rest.apply(this));
 124    }
 125    }
 126   
 127   
 128    /** Filters a list to contain only those elements accepted by the given predicate */
 129  8 public static <T> ConsVisitor<T, ConsList<T>> filter(Predicate<? super T> pred) {
 130  8 return new Filter<T>(pred);
 131    }
 132   
 133    private static class Filter<T> extends ConsVisitor<T, ConsList<T>> implements Serializable {
 134    private final Predicate<? super T> _pred;
 135  8 public Filter(Predicate<? super T> pred) { _pred = pred; }
 136  8 public ConsList<T> forEmpty() { return ConsList.empty(); }
 137  12 public ConsList<T> forNonempty(T first, ConsList<? extends T> rest) {
 138  8 if (_pred.contains(first)) { return ConsList.cons(first, rest.apply(this)); }
 139  4 else { return rest.apply(this); }
 140    }
 141    }
 142   
 143   
 144    /** Produces a new list by applying the given lambda to each of a list's elements */
 145  5 public static <S, T> ConsVisitor<S, ConsList<T>> map(Lambda<? super S, ? extends T> lambda) {
 146  5 return new Map<S, T>(lambda);
 147    }
 148   
 149    private static class Map<S, T> extends ConsVisitor<S, ConsList<T>> implements Serializable {
 150    private final Lambda<? super S, ? extends T> _lambda;
 151  5 public Map(Lambda<? super S, ? extends T> lambda) { _lambda = lambda; }
 152  5 public ConsList<T> forEmpty() { return ConsList.empty(); }
 153  10 public ConsList<T> forNonempty(S first, ConsList<? extends S> rest) {
 154  10 return ConsList.cons(_lambda.value(first), rest.apply(this));
 155    }
 156    }
 157   
 158    }