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