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
AbstractInjectiveRelation.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 InjectiveRelation. Subclasses must provide
 50    * {@link #isStatic}, {@link #injectionMap}, {@link #firstSet}, and {@link #matchFirst}.
 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 AbstractInjectiveRelation<T1, T2> extends AbstractRelation<T1, T2>
 56    implements InjectiveRelation<T1, T2> {
 57   
 58    public abstract boolean isStatic();
 59    public abstract LambdaMap<T2, T1> injectionMap();
 60    public abstract PredicateSet<T1> firstSet();
 61    public abstract PredicateSet<T2> matchFirst(T1 first);
 62   
 63    /** Returns {@code injectionMap().isEmpty()}. */
 64  0 @Override public boolean isEmpty() { return injectionMap().isEmpty(); }
 65    /** Returns {@code injectionMap().size()}. */
 66  0 @Override public int size() { return injectionMap().size(); }
 67    /** Returns {@code injectionMap().keySet().size(bound)}. */
 68  0 @Override public int size(int bound) { return injectionMap().keySet().size(bound); }
 69    /** Returns {@code injectionMap().keySet().isInfinite()}. */
 70  0 public boolean isInfinite() { return injectionMap().keySet().isInfinite(); }
 71    /** Returns {@code injectionMap().keySet().hasFixedSize()}. */
 72  0 public boolean hasFixedSize() { return injectionMap().keySet().hasFixedSize(); }
 73   
 74    /** Checks for the given entry in {@code functionMap()}. */
 75  0 public boolean contains(T1 first, T2 second) {
 76  0 LambdaMap<T2, T1> map = injectionMap();
 77  0 return map.containsKey(second) && ObjectUtil.equal(map.get(second), first);
 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<T2, T1> map = injectionMap();
 85  0 return map.containsKey(p.second()) && ObjectUtil.equal(map.get(p.second()), p.first());
 86    }
 87  0 else { return false; }
 88    }
 89   
 90    /** Produces an iterator based on {@code injectionMap().entrySet()}. */
 91  0 public Iterator<Pair<T1, T2>> iterator() {
 92  0 return MappedIterator.make(injectionMap().entrySet().iterator(),
 93    new Lambda<Map.Entry<T2, T1>, Pair<T1, T2>>() {
 94  0 public Pair<T1, T2> value(Map.Entry<T2, T1> entry) {
 95  0 return new Pair<T1, T2>(entry.getValue(), entry.getKey());
 96    }
 97    });
 98    }
 99   
 100    /** Returns {@code injectionMap().keySet()}. */
 101  0 public PredicateSet<T2> secondSet() { return injectionMap().keySet(); }
 102   
 103    /** Returns {@code injectionMap().containsKey(second)}. */
 104  0 @Override public boolean containsSecond(T2 second) { return injectionMap().containsKey(second); }
 105   
 106    /** Returns a set that queries and manipulates the mapping from {@code second} in {@code injectionMap()}. */
 107  0 public PredicateSet<T1> matchSecond(T2 second) { return new MatchSecondSet(second); }
 108   
 109    /** Returns {@code injectionMap().get(first)}. */
 110  0 public T1 antecedent(T2 second) { return injectionMap().get(second); }
 111   
 112    /** Returns an {@link InverseInjectiveRelation}. */
 113  0 @Override public FunctionalRelation<T2, T1> inverse() { return new InverseInjectiveRelation(); }
 114   
 115    /**
 116    * A result of {@code matchSecond()} defined in terms of {@code injectionMap()}. 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 MatchSecondSet extends AbstractPredicateSet<T1> implements Serializable {
 121    private final T2 _key;
 122   
 123  0 public MatchSecondSet(T2 second) { _key = second; }
 124   
 125  0 @Override public boolean isEmpty() { return !injectionMap().containsKey(_key); }
 126  0 @Override public int size() { return injectionMap().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 AbstractInjectiveRelation.this.isStatic(); }
 130  0 public boolean isStatic() { return AbstractInjectiveRelation.this.isStatic(); }
 131   
 132  0 public boolean contains(Object val) {
 133  0 return AbstractInjectiveRelation.this.contains(Pair.make(val, _key));
 134    }
 135   
 136  0 public Iterator<T1> iterator() {
 137  0 final LambdaMap<T2, T1> map = injectionMap();
 138  0 if (map.containsKey(_key)) {
 139  0 return new MutableSingletonIterator<T1>(map.get(_key), new Runnable1<T1>() {
 140  0 public void run(T1 val) { map.remove(_key); }
 141    });
 142    }
 143  0 else { return EmptyIterator.make(); }
 144    }
 145   
 146  0 @Override public boolean add(T1 val) {
 147  0 boolean result = !AbstractInjectiveRelation.this.contains(val, _key);
 148  0 if (result) { injectionMap().put(_key, val); }
 149  0 return result;
 150    }
 151   
 152  0 @Override public boolean remove(Object val) {
 153  0 boolean result = AbstractInjectiveRelation.this.contains(Pair.make(val, _key));
 154  0 if (result) { injectionMap().remove(_key); }
 155  0 return result;
 156    }
 157   
 158  0 @Override public void clear() { injectionMap().remove(_key); }
 159    }
 160   
 161    /**
 162    * An inverse of the enclosing relation. Extends {@link AbstractRelation.InverseRelation} with
 163    * the methods necessary to implement FunctionalRelation.
 164    */
 165    protected class InverseInjectiveRelation extends InverseRelation implements FunctionalRelation<T2, T1> {
 166  0 public T1 value(T2 first) { return AbstractInjectiveRelation.this.antecedent(first); }
 167  0 public LambdaMap<T2, T1> functionMap() { return AbstractInjectiveRelation.this.injectionMap(); }
 168  0 @Override public Relation<T1, T2> inverse() { return AbstractInjectiveRelation.this; }
 169    }
 170   
 171    }