|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| TotalMap.java | 75% | 42.3% | 21.1% | 36.7% |
|
||||||||||||||
| 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 edu.rice.cs.plt.lambda.Lambda; | |
| 40 | import edu.rice.cs.plt.lambda.LambdaUtil; | |
| 41 | ||
| 42 | /** | |
| 43 | * <p>A map that is defined for all values in the domain {@code K}. This class is similar | |
| 44 | * to the {@link java.util.Map} interface, but there are fundamental differences that make | |
| 45 | * it difficult to implement the interface. For example, it's impossible to produce a | |
| 46 | * {@code valueSet} for an arbitrary {@code TotalMap}. (It's even impossible to safely define | |
| 47 | * the {@code Map.get} method, since it is defined for all {@code Object}s, and the | |
| 48 | * lambda implementing the total map is only defined in terms of {@code K}.)<p> | |
| 49 | * | |
| 50 | * <p>The total coverage of the domain is achieved by defining the map in terms of a | |
| 51 | * {@link Lambda}. Additionally, specific values can be overridden with specific results | |
| 52 | * via the {@link #override} method; these mappings can be undone via {@link #revert}. | |
| 53 | * Optionally, the results of {@link #get} queries that call the lambda can be automatically | |
| 54 | * cached with the other overriding mappings.</p> | |
| 55 | * | |
| 56 | * <p>This class is not thread safe (to reduce overhead in sequenential applications, and because | |
| 57 | * external references to {@code _overrides} make it difficult to make safety guarantees). It is | |
| 58 | * possible, for example, for a TotalMap with caching enabled to still compute a result more than | |
| 59 | * once.</p> | |
| 60 | */ | |
| 61 | public class TotalMap<K, V> { | |
| 62 | ||
| 63 | private final Lambda<? super K, ? extends V> _lambda; | |
| 64 | private final Map<? super K, V> _overrides; | |
| 65 | private final boolean _cache; | |
| 66 | ||
| 67 | /** Create a {@code TotalMap} whose value is {@code null} for all inputs. Caching is disabled. */ | |
| 68 | 0 | public TotalMap() { |
| 69 | 0 | this(LambdaUtil.<V>nullLambda(), new HashMap<K, V>(), false); |
| 70 | } | |
| 71 | ||
| 72 | /** Create a {@code TotalMap} defined in terms of {@code lambda}. Caching is disabled. */ | |
| 73 | 0 | public TotalMap(Lambda<? super K, ? extends V> lambda) { |
| 74 | 0 | this(lambda, new HashMap<K, V>(), false); |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * Create a {@code TotalMap} defined in terms of {@code lambda}. Caching may be enabled | |
| 79 | * or disabled. | |
| 80 | */ | |
| 81 | 50 | public TotalMap(Lambda<? super K, ? extends V> lambda, boolean cache) { |
| 82 | 50 | this (lambda, new HashMap<K, V>(), cache); |
| 83 | } | |
| 84 | ||
| 85 | /** | |
| 86 | * Create a {@code TotalMap} whose value is {@code null} for all inputs, with the exception | |
| 87 | * of the given overriding assignments. The given map is stored (not copied), so subsequent | |
| 88 | * external changes will be reflected here (and vice versa). Cahcing is disabled. | |
| 89 | */ | |
| 90 | 0 | public TotalMap(Map<? super K, V> overrides) { |
| 91 | 0 | this(LambdaUtil.<V>nullLambda(), overrides, false); |
| 92 | } | |
| 93 | ||
| 94 | /** | |
| 95 | * Create a {@code TotalMap} defined in terms of {@code lambda}, with the exception of | |
| 96 | * the given overriding assignments. The given map is stored (not copied), so subsequent | |
| 97 | * external changes will be reflected here (and vice versa). Caching is disabled. | |
| 98 | */ | |
| 99 | 0 | public TotalMap(Lambda<? super K, ? extends V> lambda, Map<? super K, V> overrides) { |
| 100 | 0 | this (lambda, overrides, false); |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * Create a {@code TotalMap} defined in terms of {@code lambda}, with the exception of | |
| 105 | * the given overriding assignments. The given map is stored (not copied), so subsequent | |
| 106 | * external changes will be reflected here (and vice versa). Caching is enabled or disabled | |
| 107 | * as specified. | |
| 108 | */ | |
| 109 | 50 | public TotalMap(Lambda<? super K, ? extends V> lambda, Map<? super K, V> overrides, |
| 110 | boolean cache) { | |
| 111 | 50 | _lambda = lambda; |
| 112 | 50 | _overrides = overrides; |
| 113 | 50 | _cache = cache; |
| 114 | } | |
| 115 | ||
| 116 | /** @return The value corresponding to {@code key} */ | |
| 117 | 576 | public V get(K key) { |
| 118 | 7 | if (_overrides.containsKey(key)) { return _overrides.get(key); } |
| 119 | else { | |
| 120 | 569 | V result = _lambda.value(key); |
| 121 | 569 | if (_cache) { _overrides.put(key, result); } |
| 122 | 569 | return result; |
| 123 | } | |
| 124 | } | |
| 125 | ||
| 126 | /** | |
| 127 | * Add the given mapping to the override map. Subsequent invocations of {@code get(key)} | |
| 128 | * will immediately return {@code value}. | |
| 129 | */ | |
| 130 | 0 | public void override(K key, V value) { _overrides.put(key, value); } |
| 131 | ||
| 132 | /** @return {@code true} iff the override map contains the given key */ | |
| 133 | 0 | public boolean containsOverride(Object key) { return _overrides.containsKey(key); } |
| 134 | ||
| 135 | /** | |
| 136 | * Remove the given key from the override map. Subsequent invocations of {@code get(key)} | |
| 137 | * will invoke the lambda. | |
| 138 | * @return The value overridden by the given key, or {@code null} if the mapping | |
| 139 | * did not exist. | |
| 140 | */ | |
| 141 | 569 | public V revert(K key) { return _overrides.remove(key); } |
| 142 | ||
| 143 | /** Add all the given mappings to the override map */ | |
| 144 | 0 | public void overrideAll(Map<? extends K, ? extends V> map) { |
| 145 | 0 | _overrides.putAll(map); |
| 146 | } | |
| 147 | ||
| 148 | /** | |
| 149 | * Remove all mappings from the override map. Subsequent invocations of {@code get} will | |
| 150 | * invoke the lambda. | |
| 151 | */ | |
| 152 | 0 | public void revertAll() { _overrides.clear(); } |
| 153 | ||
| 154 | /** Get the size of the cache -- the number of overridden values */ | |
| 155 | 0 | public int cacheSize() { return _overrides.size(); } |
| 156 | ||
| 157 | /** Call the constructor (allows the type arguments to be inferred) */ | |
| 158 | 0 | public static <K, V> TotalMap<K, V> make() { return new TotalMap<K, V>(); } |
| 159 | ||
| 160 | /** Call the constructor (allows the type arguments to be inferred) */ | |
| 161 | 0 | public static <K, V> TotalMap<K, V> make(Lambda<? super K, ? extends V> lambda) { |
| 162 | 0 | return new TotalMap<K, V>(lambda); |
| 163 | } | |
| 164 | ||
| 165 | /** Call the constructor (allows the type arguments to be inferred) */ | |
| 166 | 0 | public static <K, V> TotalMap<K, V> make(Lambda<? super K, ? extends V> lambda, boolean cache) { |
| 167 | 0 | return new TotalMap<K, V>(lambda, cache); |
| 168 | } | |
| 169 | ||
| 170 | /** Call the constructor (allows the type arguments to be inferred) */ | |
| 171 | 0 | public static <K, V> TotalMap<K, V> make(Map<? super K, V> overrides) { |
| 172 | 0 | return new TotalMap<K, V>(overrides); |
| 173 | } | |
| 174 | ||
| 175 | /** Call the constructor (allows the type arguments to be inferred) */ | |
| 176 | 0 | public static <K, V> TotalMap<K, V> make(Lambda<? super K, ? extends V> lambda, |
| 177 | Map<? super K, V> overrides) { | |
| 178 | 0 | return new TotalMap<K, V>(lambda, overrides); |
| 179 | } | |
| 180 | ||
| 181 | /** Call the constructor (allows the type arguments to be inferred) */ | |
| 182 | 0 | public static <K, V> TotalMap<K, V> make(Lambda<? super K, ? extends V> lambda, |
| 183 | Map<? super K, V> overrides, boolean cache) { | |
| 184 | 0 | return new TotalMap<K, V>(lambda, overrides, cache); |
| 185 | } | |
| 186 | ||
| 187 | } |
|
||||||||||