Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 171   Methods: 30
NCLOC: 86   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
AbstractFunctionalRelation.java 0% 0% 0% 0%
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.util.Iterator;
 38    import java.util.Map;
 39    import java.io.Serializable;
 40    import edu.rice.cs.plt.tuple.Pair;
 41    import edu.rice.cs.plt.lambda.Lambda;
 42    import edu.rice.cs.plt.lambda.Runnable1;
 43    import edu.rice.cs.plt.iter.MappedIterator;
 44    import edu.rice.cs.plt.iter.EmptyIterator;
 45    import edu.rice.cs.plt.iter.MutableSingletonIterator;
 46    import edu.rice.cs.plt.object.ObjectUtil;
 47   
 48    /**
 49    * An abstract parent class for implementations of FunctionalRelation. Subclasses must provide
 50    * {@link #isStatic}, {@link #functionMap}, {@link #secondSet}, and {@link #matchSecond}.
 51    * To support mutation, they must also override {@link #add(Object, Object)}
 52    * and {@link #remove(Object, Object)}. For best performance, they may also override
 53    * {@link #clear}.
 54    */
 55    public abstract class AbstractFunctionalRelation<T1, T2> extends AbstractRelation<T1, T2>
 56    implements FunctionalRelation<T1, T2> {
 57   
 58    public abstract boolean isStatic();
 59    public abstract LambdaMap<T1, T2> functionMap();
 60    public abstract PredicateSet<T2> secondSet();
 61    public abstract PredicateSet<T1> matchSecond(T2 second);
 62   
 63    /** Returns {@code functionMap().isEmpty()}. */
 64  0 @Override public boolean isEmpty() { return functionMap().isEmpty(); }
 65    /** Returns {@code functionMap().size()}. */
 66  0 @Override public int size() { return functionMap().size(); }
 67    /** Returns {@code functionMap().keySet().size(bound)}. */
 68  0 @Override public int size(int bound) { return functionMap().keySet().size(bound); }
 69    /** Returns {@code functionMap().keySet().isInfinite()}. */
 70  0 public boolean isInfinite() { return functionMap().keySet().isInfinite(); }
 71    /** Returns {@code functionMap().keySet().hasFixedSize()}. */
 72  0 public boolean hasFixedSize() { return functionMap().keySet().hasFixedSize(); }
 73   
 74    /** Checks for the given entry in {@code functionMap()}. */
 75  0 public boolean contains(T1 first, T2 second) {
 76  0 LambdaMap<T1, T2> map = functionMap();
 77  0 return map.containsKey(first) && ObjectUtil.equal(map.get(first), second);
 78    }
 79   
 80    /** Checks for the given entry in {@code functionMap()}. */
 81  0 public boolean contains(Object obj) {
 82  0 if (obj instanceof Pair<?, ?>) {
 83  0 Pair<?, ?> p = (Pair<?, ?>) obj;
 84  0 LambdaMap<T1, T2> map = functionMap();
 85  0 return map.containsKey(p.first()) && ObjectUtil.equal(map.get(p.first()), p.second());
 86    }
 87  0 else { return false; }
 88    }
 89   
 90    /** Produces an iterator based on {@code functionMap().entrySet()}. */
 91  0 public Iterator<Pair<T1, T2>> iterator() {
 92  0 return MappedIterator.make(functionMap().entrySet().iterator(),
 93    new Lambda<Map.Entry<T1, T2>, Pair<T1, T2>>() {
 94  0 public Pair<T1, T2> value(Map.Entry<T1, T2> entry) {
 95  0 return new Pair<T1, T2>(entry.getKey(), entry.getValue());
 96    }
 97    });
 98    }
 99   
 100    /** Returns {@code functionMap().keySet()}. */
 101  0 public PredicateSet<T1> firstSet() { return functionMap().keySet(); }
 102   
 103    /** Returns {@code functionMap().containsKey(first)}. */
 104  0 @Override public boolean containsFirst(T1 first) { return functionMap().containsKey(first); }
 105   
 106    /** Returns a set that queries and manipulates the mapping from {@code first} in {@code functionMap()}. */
 107  0 public PredicateSet<T2> matchFirst(T1 first) { return new MatchFirstSet(first); }
 108   
 109    /** Returns {@code functionMap().get(first)}. */
 110  0 public T2 value(T1 first) { return functionMap().get(first); }
 111   
 112    /** Returns an {@link InverseFunctionalRelation}. */
 113  0 @Override public Relation<T2, T1> inverse() { return new InverseFunctionalRelation(); }
 114   
 115    /**
 116    * A result of {@code matchFirst()} defined in terms of {@code functionMap()}. The size and
 117    * contents are determined by the map's {@code containsKey()} and {@code get()} methods;
 118    * mutation delegates to {@code put()} and {@code remove()}.
 119    */
 120    private final class MatchFirstSet extends AbstractPredicateSet<T2> implements Serializable {
 121    private final T1 _key;
 122   
 123  0 public MatchFirstSet(T1 first) { _key = first; }
 124   
 125  0 @Override public boolean isEmpty() { return !functionMap().containsKey(_key); }
 126  0 @Override public int size() { return functionMap().containsKey(_key) ? 1 : 0; }
 127  0 @Override public int size(int bound) { return (bound == 0) ? 0 : size(); }
 128  0 public boolean isInfinite() { return false; }
 129  0 public boolean hasFixedSize() { return AbstractFunctionalRelation.this.isStatic(); }
 130  0 public boolean isStatic() { return AbstractFunctionalRelation.this.isStatic(); }
 131   
 132  0 public boolean contains(Object val) {
 133  0 return AbstractFunctionalRelation.this.contains(Pair.make(_key, val));
 134    }
 135   
 136  0 public Iterator<T2> iterator() {
 137  0 final LambdaMap<T1, T2> map = functionMap();
 138  0 if (map.containsKey(_key)) {
 139  0 return new MutableSingletonIterator<T2>(map.get(_key), new Runnable1<T2>() {
 140  0 public void run(T2 val) { map.remove(_key); }
 141    });
 142    }
 143  0 else { return EmptyIterator.make(); }
 144    }
 145   
 146  0 @Override public boolean add(T2 val) {
 147  0 boolean result = !AbstractFunctionalRelation.this.contains(_key, val);
 148  0 if (result) { functionMap().put(_key, val); }
 149  0 return result;
 150    }
 151   
 152  0 @Override public boolean remove(Object val) {
 153  0 boolean result = AbstractFunctionalRelation.this.contains(Pair.make(_key, val));
 154  0 if (result) { functionMap().remove(_key); }
 155  0 return result;
 156    }
 157   
 158  0 @Override public void clear() { functionMap().remove(_key); }
 159    }
 160   
 161    /**
 162    * An inverse of the enclosing relation. Extends {@link AbstractRelation.InverseRelation} with
 163    * the methods necessary to implement InjectiveRelation.
 164    */
 165    protected class InverseFunctionalRelation extends InverseRelation implements InjectiveRelation<T2, T1> {
 166  0 public T2 antecedent(T1 second) { return AbstractFunctionalRelation.this.value(second); }
 167  0 public LambdaMap<T1, T2> injectionMap() { return AbstractFunctionalRelation.this.functionMap(); }
 168  0 @Override public FunctionalRelation<T1, T2> inverse() { return AbstractFunctionalRelation.this; }
 169    }
 170   
 171    }