Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 198   Methods: 31
NCLOC: 121   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
IndexedRelation.java 40% 46% 45.2% 45.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.util.Set;
 38    import java.util.Map;
 39    import java.util.Iterator;
 40    import java.io.Serializable;
 41    import edu.rice.cs.plt.tuple.Pair;
 42    import edu.rice.cs.plt.iter.IterUtil;
 43    import edu.rice.cs.plt.lambda.Thunk;
 44   
 45    /** An implementation of the Relation interface based on indexing maps. */
 46    public class IndexedRelation<T1, T2> extends AbstractRelation<T1, T2> implements Serializable {
 47    private final RelationIndex<T1, T2> _firstIndex;
 48    private final RelationIndex<T2, T1> _secondIndex;
 49   
 50    /** Index in both directions using {@link java.util.HashMap}s and {@link java.util.HashSet}s. */
 51  1 public IndexedRelation() {
 52  1 this(CollectUtil.<T1, PredicateSet<T2>>hashMapFactory(),
 53    CollectUtil.<T2>hashSetFactory(4),
 54    CollectUtil.<T2, PredicateSet<T1>>hashMapFactory(),
 55    CollectUtil.<T1>hashSetFactory(4));
 56    }
 57   
 58    /**
 59    * Index using {@link java.util.HashMap}s and {@link java.util.HashSet}s. If {@code indexSecond}
 60    * is false, no second-to-first index is created.
 61    */
 62  1 public IndexedRelation(boolean indexSecond) {
 63  1 if (indexSecond) {
 64  0 _firstIndex = makeFirstIndex(CollectUtil.<T1, PredicateSet<T2>>hashMapFactory(),
 65    CollectUtil.<T2>hashSetFactory(4));
 66  0 _secondIndex = makeSecondIndex(CollectUtil.<T2, PredicateSet<T1>>hashMapFactory(),
 67    CollectUtil.<T1>hashSetFactory(4));
 68    }
 69    else {
 70  1 _firstIndex = makeFirstIndex(CollectUtil.<T1, PredicateSet<T2>>hashMapFactory(),
 71    CollectUtil.<T2>hashSetFactory(4));
 72  1 _secondIndex = new LazyRelationIndex<T2, T1>(IterUtil.map(_firstIndex, Pair.<T1, T2>inverter()));
 73    }
 74    }
 75   
 76    /**
 77    * Create indices based on the given factories.
 78    * @param firstIndexFactory Maps firsts to sets of seconds.
 79    * @param firstIndexEntryFactory Produces sets for the first-to-second index.
 80    * @param secondIndexFactory Maps seconds to sets of firsts.
 81    * @param secondIndexEntryFactory Produces sets for the second-to-first index.
 82    */
 83  1 public IndexedRelation(Thunk<Map<T1, PredicateSet<T2>>> firstIndexFactory,
 84    Thunk<Set<T2>> firstIndexEntryFactory,
 85    Thunk<Map<T2, PredicateSet<T1>>> secondIndexFactory,
 86    Thunk<Set<T1>> secondIndexEntryFactory) {
 87  1 _firstIndex = makeFirstIndex(firstIndexFactory, firstIndexEntryFactory);
 88  1 _secondIndex = makeSecondIndex(secondIndexFactory, secondIndexEntryFactory);
 89    }
 90   
 91    /**
 92    * Create an index based on the given factories. No second-to-first index is created. (If
 93    * <em>only</em> a second-to-first index is wanted, invoke this constructor and then invert
 94    * the result.)
 95    * @param firstIndexFactory Maps firsts to sets of seconds.
 96    * @param firstIndexEntryFactory Produces sets for the first-to-second index.
 97    */
 98  0 public IndexedRelation(Thunk<Map<T1, PredicateSet<T2>>> firstIndexFactory,
 99    Thunk<Set<T2>> firstIndexEntryFactory) {
 100  0 _firstIndex = makeFirstIndex(firstIndexFactory, firstIndexEntryFactory);
 101  0 _secondIndex = new LazyRelationIndex<T2, T1>(IterUtil.map(_firstIndex, Pair.<T1, T2>inverter()));
 102    }
 103   
 104  2 private RelationIndex<T1, T2> makeFirstIndex(Thunk<Map<T1, PredicateSet<T2>>> mapFactory,
 105    Thunk<Set<T2>> setFactory) {
 106  2 return new ConcreteRelationIndex<T1, T2>(mapFactory, setFactory) {
 107  0 public void addToRelation(T1 first, T2 second) { _secondIndex.added(second, first); }
 108  0 public void removeFromRelation(T1 first, T2 second) { _secondIndex.removed(second, first); }
 109  0 public void clearRelation() { _secondIndex.cleared(); }
 110    };
 111    }
 112   
 113  1 private RelationIndex<T2, T1> makeSecondIndex(Thunk<Map<T2, PredicateSet<T1>>> mapFactory,
 114    Thunk<Set<T1>> setFactory) {
 115  1 return new ConcreteRelationIndex<T2, T1>(mapFactory, setFactory) {
 116  0 public void addToRelation(T2 second, T1 first) { _firstIndex.added(first, second); }
 117  0 public void removeFromRelation(T2 second, T1 first) { _firstIndex.removed(first, second); }
 118  0 public void clearRelation() { _firstIndex.cleared(); }
 119    };
 120    }
 121   
 122  8 @Override public boolean isEmpty() { return _firstIndex.isEmpty(); }
 123  32 @Override public int size() { return _firstIndex.size(); }
 124  0 @Override public int size(int bound) { return _firstIndex.size(bound); }
 125  0 public boolean isInfinite() { return false; }
 126  0 public boolean hasFixedSize() { return false; }
 127  0 public boolean isStatic() { return false; }
 128   
 129  0 public boolean contains(T1 first, T2 second) {
 130  0 return _firstIndex.contains(first, second);
 131    }
 132   
 133  0 public boolean contains(Object obj) {
 134  0 if (obj instanceof Pair<?, ?>) {
 135  0 Pair<?, ?> p = (Pair<?, ?>) obj;
 136  0 return _firstIndex.contains(p.first(), p.second());
 137    }
 138  0 else { return false; }
 139    }
 140   
 141  12 public Iterator<Pair<T1, T2>> iterator() { return _firstIndex.iterator(); }
 142   
 143  64 public PredicateSet<T1> firstSet() { return _firstIndex.keys(); }
 144  24 public PredicateSet<T2> matchFirst(T1 first) { return _firstIndex.match(first); }
 145  84 public PredicateSet<T2> secondSet() { return _secondIndex.keys(); }
 146  44 public PredicateSet<T1> matchSecond(T2 second) { return _secondIndex.match(second); }
 147   
 148  10 @Override public boolean add(T1 first, T2 second) {
 149  10 boolean result = !_firstIndex.contains(first, second);
 150  10 if (result) {
 151  8 _firstIndex.added(first, second);
 152  8 _secondIndex.added(second, first);
 153    }
 154  10 return result;
 155    }
 156   
 157  0 @Override public boolean remove(T1 first, T2 second) {
 158  0 boolean result = _firstIndex.contains(first, second);
 159  0 if (result) {
 160  0 _firstIndex.removed(first, second);
 161  0 _secondIndex.removed(second, first);
 162    }
 163  0 return result;
 164    }
 165   
 166  2 @Override public void clear() {
 167  2 if (!_firstIndex.isEmpty()) {
 168  2 _firstIndex.cleared();
 169  2 _secondIndex.cleared();
 170    }
 171    }
 172   
 173    /** Make an IndexedRelation indexed by {@link java.util.HashMap}s and {@link java.util.HashSet}s. */
 174  0 public static <T1, T2> IndexedRelation<T1, T2> makeHashBased() {
 175  0 return new IndexedRelation<T1, T2>(CollectUtil.<T1, PredicateSet<T2>>hashMapFactory(),
 176    CollectUtil.<T2>hashSetFactory(4),
 177    CollectUtil.<T2, PredicateSet<T1>>hashMapFactory(),
 178    CollectUtil.<T1>hashSetFactory(4));
 179    }
 180   
 181    /** Make an IndexedRelation indexed by {@link java.util.LinkedHashMap}s and {@link java.util.LinkedHashSet}s. */
 182  0 public static <T1, T2> IndexedRelation<T1, T2> makeLinkedHashBased() {
 183  0 return new IndexedRelation<T1, T2>(CollectUtil.<T1, PredicateSet<T2>>linkedHashMapFactory(),
 184    CollectUtil.<T2>linkedHashSetFactory(4),
 185    CollectUtil.<T2, PredicateSet<T1>>linkedHashMapFactory(),
 186    CollectUtil.<T1>linkedHashSetFactory(4));
 187    }
 188   
 189    /** Make an IndexedRelation indexed by {@link java.util.TreeMap}s and {@link java.util.TreeSet}s. */
 190  0 public static <T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>>
 191    IndexedRelation<T1, T2> makeTreeBased() {
 192  0 return new IndexedRelation<T1, T2>(CollectUtil.<T1, PredicateSet<T2>>treeMapFactory(),
 193    CollectUtil.<T2>treeSetFactory(),
 194    CollectUtil.<T2, PredicateSet<T1>>treeMapFactory(),
 195    CollectUtil.<T1>treeSetFactory());
 196    }
 197   
 198    }