Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 131   Methods: 12
NCLOC: 71   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
IndexedOneToOneRelation.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.io.Serializable;
 39    import edu.rice.cs.plt.lambda.Thunk;
 40   
 41    /** A implementation of OneToOneRelation based on indexing maps. */
 42    public class IndexedOneToOneRelation<T1, T2> extends AbstractOneToOneRelation<T1, T2> implements Serializable {
 43   
 44    private Map<T1, T2> _firstMap;
 45    private LambdaMap<T1, T2> _functionMap;
 46    private Map<T2, T1> _secondMap;
 47    private LambdaMap<T2, T1> _injectionMap;
 48   
 49    /** Index using {@link java.util.HashMap}s. */
 50  0 public IndexedOneToOneRelation() {
 51  0 this(CollectUtil.<T1, T2>hashMapFactory(), CollectUtil.<T2, T1>hashMapFactory());
 52    }
 53   
 54    /** Create an IndexedOneToOneRelation using the given map factories to produce indices. */
 55  0 public IndexedOneToOneRelation(Thunk<Map<T1, T2>> firstIndexFactory,
 56    Thunk<Map<T2, T1>> secondIndexFactory) {
 57  0 _firstMap = firstIndexFactory.value();
 58    // TODO: support mutation:
 59  0 _functionMap = new ImmutableMap<T1, T2>(_firstMap);
 60  0 _secondMap = secondIndexFactory.value();
 61    // TODO: support mutation:
 62  0 _injectionMap = new ImmutableMap<T2, T1>(_secondMap);
 63    }
 64   
 65  0 public boolean isStatic() { return false; }
 66  0 public LambdaMap<T1, T2> functionMap() { return _functionMap; }
 67  0 public LambdaMap<T2, T1> injectionMap() { return _injectionMap; }
 68   
 69  0 @Override public boolean add(T1 first, T2 second) {
 70  0 boolean result = validateAdd(first, second);
 71  0 if (result) {
 72  0 _firstMap.put(first, second);
 73  0 _secondMap.put(second, first);
 74    }
 75  0 return result;
 76    }
 77   
 78    /**
 79    * Returns true if this pair is not already present and can be added.
 80    * Throws an exception if the pair violates integrity constraints.
 81    */
 82  0 private boolean validateAdd(T1 first, T2 second) {
 83  0 if (_firstMap.containsKey(first)) {
 84  0 T2 current = _firstMap.get(first);
 85  0 if ((current == null) ? (second == null) : current.equals(second)) {
 86  0 return false;
 87    }
 88    else {
 89  0 throw new IllegalArgumentException("Relation already contains an entry for " + first);
 90    }
 91    }
 92  0 else if (_secondMap.containsKey(second)) {
 93  0 throw new IllegalArgumentException("Relation already contains an entry for " + second);
 94    }
 95  0 else { return true; }
 96    }
 97   
 98  0 @Override public boolean remove(T1 first, T2 second) {
 99  0 boolean result = contains(first, second);
 100  0 if (result) {
 101  0 _firstMap.remove(first);
 102  0 _secondMap.remove(second);
 103    }
 104  0 return result;
 105    }
 106   
 107  0 @Override public void clear() {
 108  0 _firstMap.clear();
 109  0 _secondMap.clear();
 110    }
 111   
 112    /** Make an IndexedOneToOneRelation indexed by {@link java.util.HashMap}s. */
 113  0 public static <T1, T2> IndexedOneToOneRelation<T1, T2> makeHashBased() {
 114  0 return new IndexedOneToOneRelation<T1, T2>(CollectUtil.<T1, T2>hashMapFactory(),
 115    CollectUtil.<T2, T1>hashMapFactory());
 116    }
 117   
 118    /** Make an IndexedOneToOneRelation indexed by {@link java.util.LinkedHashMap}s. */
 119  0 public static <T1, T2> IndexedOneToOneRelation<T1, T2> makeLinkedHashBased() {
 120  0 return new IndexedOneToOneRelation<T1, T2>(CollectUtil.<T1, T2>linkedHashMapFactory(),
 121    CollectUtil.<T2, T1>linkedHashMapFactory());
 122    }
 123   
 124    /** Make an IndexedOneToOneRelation indexed by {@link java.util.TreeMap}s. */
 125  0 public static <T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>>
 126    IndexedOneToOneRelation<T1, T2> makeTreeBased() {
 127  0 return new IndexedOneToOneRelation<T1, T2>(CollectUtil.<T1, T2>treeMapFactory(),
 128    CollectUtil.<T2, T1>treeMapFactory());
 129    }
 130   
 131    }