Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 180   Methods: 20
NCLOC: 102   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
IndexedInjectiveRelation.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.Map;
 38    import java.util.HashMap;
 39    import java.util.Set;
 40    import java.io.Serializable;
 41    import edu.rice.cs.plt.lambda.Thunk;
 42   
 43    /**
 44    * A hash code-based implementation of the InjectiveRelation interface. By default, a hash-based index
 45    * is created mapping firsts to sets of seconds; this functionality may be turned off where it
 46    * is not needed.
 47    */
 48    public class IndexedInjectiveRelation<T1, T2> extends AbstractInjectiveRelation<T1, T2> implements Serializable {
 49   
 50    private RelationIndex<T1, T2> _firstIndex;
 51    private Map<T2, T1> _secondMap;
 52    private LambdaMap<T2, T1> _injectionMap;
 53   
 54    /** Index in both directions using {@link java.util.HashMap}s and {@link java.util.HashSet}s. */
 55  0 public IndexedInjectiveRelation() {
 56  0 this(CollectUtil.<T2, T1>hashMapFactory(),
 57    CollectUtil.<T1, PredicateSet<T2>>hashMapFactory(),
 58    CollectUtil.<T2>hashSetFactory(4));
 59    }
 60   
 61    /**
 62    * Index using {@link java.util.HashMap}s and {@link java.util.HashSet}s. If {@code indexFirst}
 63    * is false, no first-to-second index is created.
 64    */
 65  0 public IndexedInjectiveRelation(boolean indexFirst) {
 66  0 if (indexFirst) {
 67  0 _secondMap = new HashMap<T2, T1>();
 68  0 _injectionMap = new ImmutableMap<T2, T1>(_secondMap);
 69  0 _firstIndex = makeFirstIndex(CollectUtil.<T1, PredicateSet<T2>>hashMapFactory(),
 70    CollectUtil.<T2>hashSetFactory(4));
 71    }
 72    else {
 73  0 _secondMap = new HashMap<T2, T1>();
 74  0 _injectionMap = new ImmutableMap<T2, T1>(_secondMap);
 75  0 _firstIndex = new LazyRelationIndex<T1, T2>(this);
 76    }
 77    }
 78   
 79    /**
 80    * Create indices based on the given factories.
 81    * @param secondIndexFactory Maps seconds to firsts.
 82    * @param firstIndexFactory Maps firsts to sets of seconds.
 83    * @param firstIndexEntryFactory Produces sets for the first-to-second index.
 84    */
 85  0 public IndexedInjectiveRelation(Thunk<Map<T2, T1>> secondIndexFactory,
 86    Thunk<Map<T1, PredicateSet<T2>>> firstIndexFactory,
 87    Thunk<Set<T2>> firstIndexEntryFactory) {
 88  0 _secondMap = secondIndexFactory.value();
 89  0 _injectionMap = new ImmutableMap<T2, T1>(_secondMap);
 90  0 _firstIndex = makeFirstIndex(firstIndexFactory, firstIndexEntryFactory);
 91    }
 92   
 93    /**
 94    * Create an index based on the given factory. No first-to-second index is created.
 95    * @param secondIndexFactory Maps seconds to firsts.
 96    */
 97  0 public IndexedInjectiveRelation(Thunk<Map<T2, T1>> secondIndexFactory) {
 98  0 _secondMap = secondIndexFactory.value();
 99  0 _injectionMap = new ImmutableMap<T2, T1>(_secondMap);
 100  0 _firstIndex = new LazyRelationIndex<T1, T2>(this);
 101    }
 102   
 103  0 private RelationIndex<T1, T2> makeFirstIndex(Thunk<Map<T1, PredicateSet<T2>>> mapFactory,
 104    Thunk<Set<T2>> setFactory) {
 105  0 return new ConcreteRelationIndex<T1, T2>(mapFactory, setFactory) {
 106  0 public void validateAdd(T1 first, T2 second) { IndexedInjectiveRelation.this.validateAdd(first, second); }
 107  0 public void addToRelation(T1 first, T2 second) { _secondMap.put(second, first); }
 108  0 public void removeFromRelation(T1 first, T2 second) { _secondMap.remove(second); }
 109  0 public void clearRelation() { _secondMap.clear(); }
 110    };
 111    }
 112   
 113  0 public boolean isStatic() { return false; }
 114  0 public LambdaMap<T2, T1> injectionMap() { return _injectionMap; }
 115  0 public PredicateSet<T1> firstSet() { return _firstIndex.keys(); }
 116  0 public PredicateSet<T2> matchFirst(T1 first) { return _firstIndex.match(first); }
 117   
 118  0 @Override public boolean add(T1 first, T2 second) {
 119  0 boolean result = validateAdd(first, second);
 120  0 if (result) {
 121  0 _secondMap.put(second, first);
 122  0 _firstIndex.added(first, second);
 123    }
 124  0 return result;
 125    }
 126   
 127    /**
 128    * Returns true if this pair is not already present and can be added.
 129    * Throws an exception if the pair violates integrity constraints.
 130    */
 131  0 private boolean validateAdd(T1 first, T2 second) {
 132  0 if (_secondMap.containsKey(second)) {
 133  0 T1 current = _secondMap.get(second);
 134  0 if ((current == null) ? (first == null) : current.equals(first)) {
 135  0 return false;
 136    }
 137    else {
 138  0 throw new IllegalArgumentException("Relation already contains an entry for " + second);
 139    }
 140    }
 141  0 else { return true; }
 142    }
 143   
 144  0 @Override public boolean remove(T1 first, T2 second) {
 145  0 boolean result = contains(first, second);
 146  0 if (result) {
 147  0 _secondMap.remove(second);
 148  0 _firstIndex.removed(first, second);
 149    }
 150  0 return result;
 151    }
 152   
 153  0 @Override public void clear() {
 154  0 _secondMap.clear();
 155  0 _firstIndex.cleared();
 156    }
 157   
 158    /** Make an IndexedInjectiveRelation indexed by {@link java.util.HashMap}s and {@link java.util.HashSet}s. */
 159  0 public static <T1, T2> IndexedInjectiveRelation<T1, T2> makeHashBased() {
 160  0 return new IndexedInjectiveRelation<T1, T2>(CollectUtil.<T2, T1>hashMapFactory(),
 161    CollectUtil.<T1, PredicateSet<T2>>hashMapFactory(),
 162    CollectUtil.<T2>hashSetFactory(4));
 163    }
 164   
 165    /** Make an IndexedInjectiveRelation indexed by {@link java.util.LinkedHashMap}s and {@link java.util.LinkedHashSet}s. */
 166  0 public static <T1, T2> IndexedInjectiveRelation<T1, T2> makeLinkedHashBased() {
 167  0 return new IndexedInjectiveRelation<T1, T2>(CollectUtil.<T2, T1>linkedHashMapFactory(),
 168    CollectUtil.<T1, PredicateSet<T2>>linkedHashMapFactory(),
 169    CollectUtil.<T2>linkedHashSetFactory(4));
 170    }
 171   
 172    /** Make an IndexedInjectiveRelation indexed by {@link java.util.TreeMap}s and {@link java.util.TreeSet}s. */
 173  0 public static <T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>>
 174    IndexedInjectiveRelation<T1, T2> makeTreeBased() {
 175  0 return new IndexedInjectiveRelation<T1, T2>(CollectUtil.<T2, T1>treeMapFactory(),
 176    CollectUtil.<T1, PredicateSet<T2>>treeMapFactory(),
 177    CollectUtil.<T2>treeSetFactory());
 178    }
 179   
 180    }