[1282] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
| 14 | |
---|
| 15 | package de.ugoe.cs.autoquest.usageprofiles; |
---|
| 16 | |
---|
| 17 | import java.util.Collection; |
---|
| 18 | import java.util.HashMap; |
---|
| 19 | import java.util.Map; |
---|
| 20 | |
---|
| 21 | /** |
---|
| 22 | * <p> |
---|
| 23 | * Default implementation of the {@link SymbolMap}. Uses internally a {@link HashMap} for |
---|
| 24 | * implementing the interface by delegating the calls to this class to the internal map. |
---|
| 25 | * </p> |
---|
| 26 | * |
---|
| 27 | * @author Patrick Harms |
---|
| 28 | */ |
---|
| 29 | public class DefaultSymbolMap<K, V> implements SymbolMap<K, V> { |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * default serial version id |
---|
| 33 | */ |
---|
| 34 | private static final long serialVersionUID = 1L; |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * the internally used map for implementing this interface |
---|
| 38 | */ |
---|
| 39 | private Map<K, V> delegate; |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * <p> |
---|
| 43 | * creates a new symbol map |
---|
| 44 | * </p> |
---|
| 45 | */ |
---|
| 46 | public DefaultSymbolMap() { |
---|
| 47 | delegate = new HashMap<K, V>(); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * <p> |
---|
| 52 | * creates a copy of an existing symbol map. The other map must not be null. |
---|
| 53 | * </p> |
---|
| 54 | * |
---|
| 55 | * @param other the map to be copied |
---|
| 56 | */ |
---|
| 57 | public DefaultSymbolMap(SymbolMap<K, V> other) { |
---|
| 58 | if (other == null) { |
---|
| 59 | throw new IllegalArgumentException("other map must not be null"); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | delegate = new HashMap<K, V>(); |
---|
| 63 | |
---|
| 64 | for (K symbol : other.getSymbols()) { |
---|
| 65 | delegate.put(symbol, other.getValue(symbol)); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | /* (non-Javadoc) |
---|
| 70 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#size() |
---|
| 71 | */ |
---|
| 72 | @Override |
---|
| 73 | public int size() { |
---|
| 74 | return delegate.size(); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /* (non-Javadoc) |
---|
| 78 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#isEmpty() |
---|
| 79 | */ |
---|
| 80 | @Override |
---|
| 81 | public boolean isEmpty() { |
---|
| 82 | return delegate.isEmpty(); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /* (non-Javadoc) |
---|
| 86 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#containsSymbol(java.lang.Object) |
---|
| 87 | */ |
---|
| 88 | @Override |
---|
| 89 | public boolean containsSymbol(K symbol) { |
---|
| 90 | if (symbol == null) { |
---|
| 91 | throw new IllegalArgumentException("symbol must not be null"); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | return delegate.containsKey(symbol); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /* (non-Javadoc) |
---|
| 98 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#getValue(java.lang.Object) |
---|
| 99 | */ |
---|
| 100 | @Override |
---|
| 101 | public V getValue(K symbol) { |
---|
| 102 | if (symbol == null) { |
---|
| 103 | throw new IllegalArgumentException("symbol must not be null"); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | return delegate.get(symbol); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /* (non-Javadoc) |
---|
| 110 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#addSymbol(java.lang.Object, java.lang.Object) |
---|
| 111 | */ |
---|
| 112 | @Override |
---|
| 113 | public void addSymbol(K symbol, V value) { |
---|
| 114 | if (symbol == null) { |
---|
| 115 | throw new IllegalArgumentException("symbol must not be null"); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | delegate.put(symbol, value); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /* (non-Javadoc) |
---|
| 122 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#removeSymbol(java.lang.Object) |
---|
| 123 | */ |
---|
| 124 | @Override |
---|
| 125 | public V removeSymbol(K symbol) { |
---|
| 126 | if (symbol == null) { |
---|
| 127 | throw new IllegalArgumentException("symbol must not be null"); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | return delegate.remove(symbol); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /* (non-Javadoc) |
---|
| 134 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#getSymbols() |
---|
| 135 | */ |
---|
| 136 | @Override |
---|
| 137 | public Collection<K> getSymbols() { |
---|
| 138 | return delegate.keySet(); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /* (non-Javadoc) |
---|
| 142 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#getValues() |
---|
| 143 | */ |
---|
| 144 | @Override |
---|
| 145 | public Collection<V> getValues() { |
---|
| 146 | return delegate.values(); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /* (non-Javadoc) |
---|
| 150 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#clear() |
---|
| 151 | */ |
---|
| 152 | @Override |
---|
| 153 | public void clear() { |
---|
| 154 | delegate.clear(); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /* (non-Javadoc) |
---|
| 158 | * @see java.lang.Object#hashCode() |
---|
| 159 | */ |
---|
| 160 | @Override |
---|
| 161 | public int hashCode() { |
---|
| 162 | return delegate.hashCode(); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | /* (non-Javadoc) |
---|
| 166 | * @see java.lang.Object#equals(java.lang.Object) |
---|
| 167 | */ |
---|
| 168 | @SuppressWarnings("unchecked") |
---|
| 169 | @Override |
---|
| 170 | public boolean equals(Object obj) { |
---|
| 171 | if (this == obj) { |
---|
| 172 | return true; |
---|
| 173 | } |
---|
| 174 | else if (this.getClass().isInstance(obj)) { |
---|
| 175 | return delegate.equals(((DefaultSymbolMap<K, V>) obj).delegate); |
---|
| 176 | } |
---|
| 177 | else { |
---|
| 178 | return false; |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | } |
---|