Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 130   Methods: 21
NCLOC: 56   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
AbstractOneToOneRelation.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.io.Serializable;
 39    import edu.rice.cs.plt.tuple.Pair;
 40    import edu.rice.cs.plt.lambda.Runnable1;
 41    import edu.rice.cs.plt.iter.EmptyIterator;
 42    import edu.rice.cs.plt.iter.MutableSingletonIterator;
 43   
 44    /**
 45    * An abstract parent class for implementations of OneToOneRelation. Subclasses must provide
 46    * {@link #isStatic}, {@link #functionMap}, and {@link #injectionMap}. To support mutation,
 47    * they must also override {@link #add(Object, Object)} and {@link #remove(Object, Object)}.
 48    * For best performance, they may also override {@link #clear}.
 49    */
 50    public abstract class AbstractOneToOneRelation<T1, T2> extends AbstractFunctionalRelation<T1, T2>
 51    implements OneToOneRelation<T1, T2> {
 52   
 53    public abstract boolean isStatic();
 54    public abstract LambdaMap<T1, T2> functionMap();
 55    public abstract LambdaMap<T2, T1> injectionMap();
 56   
 57    // Code copied from AbstractInjectiveRelation because we can't have multiple inheritance:
 58   
 59    /** Returns {@code injectionMap().keySet()}. */
 60  0 public PredicateSet<T2> secondSet() { return injectionMap().keySet(); }
 61   
 62    /** Returns {@code injectionMap().containsKey(second)}. */
 63  0 @Override public boolean containsSecond(T2 second) { return injectionMap().containsKey(second); }
 64   
 65    /** Returns a set that queries and manipulates the mapping from {@code second} in {@code injectionMap()}. */
 66  0 public PredicateSet<T1> matchSecond(T2 second) { return new MatchSecondSet(second); }
 67   
 68    /** Returns {@code injectionMap().get(first)}. */
 69  0 public T1 antecedent(T2 second) { return injectionMap().get(second); }
 70   
 71    /** Returns an {@link InverseOneToOneRelation}. */
 72  0 @Override public OneToOneRelation<T2, T1> inverse() { return new InverseOneToOneRelation(); }
 73   
 74    /**
 75    * A result of {@code matchSecond()} defined in terms of {@code injectionMap()}. The size and
 76    * contents are determined by the map's {@code containsKey()} and {@code get()} methods;
 77    * mutation delegates to {@code put()} and {@code remove()}.
 78    */
 79    private final class MatchSecondSet extends AbstractPredicateSet<T1> implements Serializable {
 80    private final T2 _key;
 81   
 82  0 public MatchSecondSet(T2 second) { _key = second; }
 83   
 84  0 @Override public boolean isEmpty() { return !injectionMap().containsKey(_key); }
 85  0 @Override public int size() { return injectionMap().containsKey(_key) ? 1 : 0; }
 86  0 @Override public int size(int bound) { return (bound == 0) ? 0 : size(); }
 87  0 public boolean isInfinite() { return false; }
 88  0 public boolean hasFixedSize() { return AbstractOneToOneRelation.this.isStatic(); }
 89  0 public boolean isStatic() { return AbstractOneToOneRelation.this.isStatic(); }
 90   
 91  0 public boolean contains(Object val) {
 92  0 return AbstractOneToOneRelation.this.contains(Pair.make(val, _key));
 93    }
 94   
 95  0 public Iterator<T1> iterator() {
 96  0 final LambdaMap<T2, T1> map = injectionMap();
 97  0 if (map.containsKey(_key)) {
 98  0 return new MutableSingletonIterator<T1>(map.get(_key), new Runnable1<T1>() {
 99  0 public void run(T1 val) { map.remove(_key); }
 100    });
 101    }
 102  0 else { return EmptyIterator.make(); }
 103    }
 104   
 105  0 @Override public boolean add(T1 val) {
 106  0 boolean result = !AbstractOneToOneRelation.this.contains(val, _key);
 107  0 if (result) { injectionMap().put(_key, val); }
 108  0 return result;
 109    }
 110   
 111  0 @Override public boolean remove(Object val) {
 112  0 boolean result = AbstractOneToOneRelation.this.contains(Pair.make(val, _key));
 113  0 if (result) { injectionMap().remove(_key); }
 114  0 return result;
 115    }
 116   
 117  0 @Override public void clear() { injectionMap().remove(_key); }
 118    }
 119   
 120    /**
 121    * An inverse of the enclosing relation. Extends {@link AbstractFunctionalRelation.InverseFunctionalRelation} with
 122    * the methods necessary to implement OneToOneRelation.
 123    */
 124    protected class InverseOneToOneRelation extends InverseFunctionalRelation implements OneToOneRelation<T2, T1> {
 125  0 public T1 value(T2 first) { return AbstractOneToOneRelation.this.antecedent(first); }
 126  0 public LambdaMap<T2, T1> functionMap() { return AbstractOneToOneRelation.this.injectionMap(); }
 127  0 @Override public OneToOneRelation<T1, T2> inverse() { return AbstractOneToOneRelation.this; }
 128    }
 129   
 130    }