|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| LazyRelationIndex.java | - | 50% | 47.1% | 48.5% |
|
||||||||||||||
| 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.io.Serializable; | |
| 38 | import java.util.Iterator; | |
| 39 | import java.util.Set; | |
| 40 | ||
| 41 | import edu.rice.cs.plt.tuple.Pair; | |
| 42 | import edu.rice.cs.plt.lambda.Predicate; | |
| 43 | import edu.rice.cs.plt.iter.IterUtil; | |
| 44 | ||
| 45 | /** | |
| 46 | * A RelationIndex implementation defined lazily in terms of the contents of a dynamic list of pairs. | |
| 47 | * Requires essentially no space, but is far less efficient than a {@link ConcreteRelationIndex}. | |
| 48 | * Does not support mutation. Defined for use in relations in which a particular mapping is unlikely | |
| 49 | * to be used heavily. | |
| 50 | */ | |
| 51 | public class LazyRelationIndex<K, V> implements RelationIndex<K, V>, Serializable { | |
| 52 | ||
| 53 | // kept for when iteration need not eliminate duplicates (which is expensive) | |
| 54 | private final Iterable<? extends Pair<K, V>> _pairs; | |
| 55 | private final PredicateSet<Pair<K, V>> _pairSet; | |
| 56 | ||
| 57 | 65 | public LazyRelationIndex(Iterable<? extends Pair<K, V>> pairs) { |
| 58 | 65 | _pairs = pairs; |
| 59 | 65 | _pairSet = new IterableSet<Pair<K, V>>(_pairs); |
| 60 | } | |
| 61 | ||
| 62 | 44 | public LazyRelationIndex(Set<Pair<K, V>> pairs) { |
| 63 | 44 | _pairs = pairs; |
| 64 | 44 | _pairSet = CollectUtil.asPredicateSet(pairs); |
| 65 | } | |
| 66 | ||
| 67 | 0 | public boolean isEmpty() { return _pairSet.isEmpty(); } |
| 68 | 0 | public int size() { return _pairSet.size(); } |
| 69 | 0 | public int size(int bound) { return _pairSet.size(bound); } |
| 70 | 0 | public boolean isInfinite() { return _pairSet.isInfinite(); } |
| 71 | 0 | public boolean hasFixedSize() { return _pairSet.hasFixedSize(); } |
| 72 | 0 | public boolean isStatic() { return _pairSet.isStatic(); } |
| 73 | ||
| 74 | ||
| 75 | 0 | public boolean contains(Object key, Object value) { |
| 76 | 0 | return IterUtil.contains(_pairs, Pair.make(key, value)); |
| 77 | } | |
| 78 | ||
| 79 | 116 | public PredicateSet<K> keys() { |
| 80 | 116 | return new IterableSet<K>(IterUtil.pairFirsts(_pairs)); |
| 81 | } | |
| 82 | ||
| 83 | 56 | public PredicateSet<V> match(K key) { |
| 84 | 56 | return new IterableSet<V>(IterUtil.pairSeconds(IterUtil.filter(_pairs, new FirstMatcher<K>(key)))); |
| 85 | } | |
| 86 | ||
| 87 | 0 | public Iterator<Pair<K, V>> iterator() { return _pairSet.iterator(); } |
| 88 | ||
| 89 | 4 | public void added(K key, V value) {} |
| 90 | 0 | public void removed(K key, V value) {} |
| 91 | 1 | public void cleared() {} |
| 92 | ||
| 93 | /** Contains pairs with the given first value. */ | |
| 94 | private static class FirstMatcher<T> implements Predicate<Pair<? extends T, ?>>, Serializable { | |
| 95 | private T _val; | |
| 96 | 56 | public FirstMatcher(T val) { _val = val; } |
| 97 | 184 | public boolean contains(Pair<? extends T, ?> pair) { return _val.equals(pair.first()); } |
| 98 | } | |
| 99 | ||
| 100 | } |
|
||||||||||