Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 147   Methods: 24
NCLOC: 83   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
CartesianRelation.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.Set;
 38    import java.util.Iterator;
 39    import java.io.Serializable;
 40    import edu.rice.cs.plt.tuple.Pair;
 41    import edu.rice.cs.plt.object.Composite;
 42    import edu.rice.cs.plt.object.ObjectUtil;
 43    import edu.rice.cs.plt.iter.CartesianIterator;
 44    import edu.rice.cs.plt.iter.EmptyIterator;
 45   
 46    /**
 47    * A Relation representing the cartesian (or cross) product of two sets. Does not support mutation,
 48    * but the contents are updated dynamically as the given sets change.
 49    */
 50    public class CartesianRelation<T1, T2> extends AbstractRelation<T1, T2> implements Composite, Serializable {
 51   
 52    private final PredicateSet<T1> _firstSet;
 53    private final PredicateSet<T2> _secondSet;
 54   
 55  0 public CartesianRelation(Set<? extends T1> firsts, Set<? extends T2> seconds) {
 56  0 _firstSet = new ImmutableSet<T1>(firsts);
 57  0 _secondSet = new ImmutableSet<T2>(seconds);
 58    }
 59   
 60  0 public int compositeHeight() { return ObjectUtil.compositeHeight(_firstSet, _secondSet) + 1; }
 61  0 public int compositeSize() { return ObjectUtil.compositeSize(_firstSet, _secondSet) + 1; }
 62   
 63  0 @Override public int size(int bound) {
 64    // copied from CartesianIterable
 65  0 int size1 = _firstSet.size(bound);
 66  0 if (size1 == 0) { return 0; }
 67    else {
 68  0 int bound2 = bound / size1;
 69  0 if (bound2 < Integer.MAX_VALUE) { bound2++; } // division must round up, not down
 70  0 int size2 = _secondSet.size(bound2);
 71    // if this overflows, it must be negative:
 72    // size1*size2 <= size1 * ((bound/size1)+1) = bound + size1
 73  0 int result = size1*size2;
 74  0 return (result > bound || result < 0) ? bound : result;
 75    }
 76    }
 77   
 78  0 @Override public boolean isEmpty() { return _firstSet.isEmpty() && _secondSet.isEmpty(); }
 79  0 public boolean isInfinite() { return _firstSet.isInfinite() || _secondSet.isInfinite(); }
 80  0 public boolean hasFixedSize() { return _firstSet.hasFixedSize() && _secondSet.hasFixedSize(); }
 81  0 public boolean isStatic() { return _firstSet.isStatic() && _secondSet.isStatic(); }
 82   
 83  0 public boolean contains(T1 first, T2 second) {
 84  0 return _firstSet.contains(first) && _secondSet.contains(second);
 85    }
 86   
 87  0 public boolean contains(Object o) {
 88  0 if (o instanceof Pair<?, ?>) {
 89  0 Pair<?, ?> p = (Pair<?, ?>) o;
 90  0 return _firstSet.contains(p.first()) && _secondSet.contains(p.second());
 91    }
 92  0 else { return false; }
 93    }
 94   
 95  0 public Iterator<Pair<T1, T2>> iterator() {
 96  0 return CartesianIterator.make(_firstSet.iterator(), _secondSet, Pair.<T1, T2>factory());
 97    }
 98   
 99  0 public PredicateSet<T1> firstSet() { return _firstSet; }
 100   
 101  0 public PredicateSet<T2> matchFirst(T1 first) {
 102  0 if (_firstSet.isStatic()) {
 103  0 return _firstSet.contains(first) ? _secondSet : CollectUtil.<T2>emptySet();
 104    }
 105  0 else { return new MatchSet<T1, T2>(first, _firstSet, _secondSet); }
 106    }
 107   
 108  0 public PredicateSet<T2> secondSet() { return _secondSet; }
 109   
 110  0 public PredicateSet<T1> matchSecond(T2 second) {
 111  0 if (_secondSet.isStatic()) {
 112  0 return _secondSet.contains(second) ? _firstSet : CollectUtil.<T1>emptySet();
 113    }
 114  0 else { return new MatchSet<T2, T1>(second, _secondSet, _firstSet); }
 115    }
 116   
 117    /**
 118    * The result of a "match" method that dynamically changes based on whether the given key
 119    * appears in a set of keys. Does not support mutation.
 120    */
 121    private static class MatchSet<K, V> extends AbstractPredicateSet<V> {
 122    private final K _key;
 123    private final PredicateSet<K> _keys;
 124    private final PredicateSet<V> _vals;
 125   
 126  0 public MatchSet(K key, PredicateSet<K> keys, PredicateSet<V> vals) {
 127  0 _key = key;
 128  0 _keys = keys;
 129  0 _vals = vals;
 130    }
 131   
 132  0 public boolean contains(Object obj) {
 133  0 return _keys.contains(_key) ? _vals.contains(obj) : false;
 134    }
 135  0 public Iterator<V> iterator() {
 136  0 return _keys.contains(_key) ? _vals.iterator() : EmptyIterator.<V>make();
 137    }
 138   
 139  0 @Override public boolean isEmpty() { return !_keys.contains(_key) || _vals.isEmpty(); }
 140  0 @Override public int size() { return _keys.contains(_key) ? _vals.size() : 0; }
 141  0 @Override public int size(int bound) { return _keys.contains(_key) ? _vals.size(bound) : 0; }
 142  0 public boolean isInfinite() { return _keys.contains(_key) && _vals.isInfinite(); }
 143  0 public boolean hasFixedSize() { return _keys.isStatic() && _vals.hasFixedSize(); }
 144  0 public boolean isStatic() { return _keys.isStatic() && _vals.isStatic(); }
 145    }
 146   
 147    }