[1285] | 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.tasktrees.temporalrelation; |
---|
| 16 | |
---|
| 17 | import java.util.Collection; |
---|
| 18 | import java.util.HashMap; |
---|
| 19 | import java.util.Map; |
---|
| 20 | |
---|
| 21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 22 | import de.ugoe.cs.autoquest.usageprofiles.SymbolMap; |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * <p> |
---|
[1401] | 26 | * symbol map implementation for task instances considering two task instances as equal if their |
---|
| 27 | * tasks are identical |
---|
[1285] | 28 | * </p> |
---|
| 29 | * |
---|
| 30 | * @author Patrick Harms |
---|
| 31 | */ |
---|
[1853] | 32 | public class TaskSymbolIdentityMap<V> implements SymbolMap<ITask, V> { |
---|
[1285] | 33 | |
---|
| 34 | /** */ |
---|
| 35 | private static final long serialVersionUID = 1L; |
---|
| 36 | |
---|
| 37 | /** |
---|
[1401] | 38 | * <p> |
---|
| 39 | * internally used map for implementing the symbol map interface |
---|
| 40 | * </p> |
---|
[1285] | 41 | */ |
---|
| 42 | private Map<ITask, V> delegate; |
---|
| 43 | |
---|
| 44 | /** |
---|
[1401] | 45 | * <p> |
---|
| 46 | * mapping between the tasks and the real symbols stored in the map, i.e. the task instances |
---|
| 47 | * </p> |
---|
[1285] | 48 | */ |
---|
[1853] | 49 | private Map<ITask, ITask> symbols; |
---|
[1285] | 50 | |
---|
| 51 | /** |
---|
| 52 | * <p> |
---|
[1401] | 53 | * initializes this map |
---|
[1285] | 54 | * </p> |
---|
| 55 | */ |
---|
| 56 | public TaskSymbolIdentityMap() { |
---|
| 57 | delegate = new HashMap<ITask, V>(); |
---|
[1853] | 58 | symbols = new HashMap<ITask, ITask>(); |
---|
[1285] | 59 | } |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * <p> |
---|
[1401] | 63 | * copy constructor |
---|
[1285] | 64 | * </p> |
---|
| 65 | * |
---|
[1401] | 66 | * @param other the map to be copied |
---|
[1285] | 67 | */ |
---|
[1853] | 68 | public TaskSymbolIdentityMap(SymbolMap<ITask, V> other) { |
---|
[1285] | 69 | if (other == null) { |
---|
| 70 | throw new IllegalArgumentException("other map must not be null"); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | delegate = new HashMap<ITask, V>(); |
---|
[1853] | 74 | symbols = new HashMap<ITask, ITask>(); |
---|
[1285] | 75 | |
---|
[1853] | 76 | for (ITask symbol : other.getSymbols()) { |
---|
| 77 | delegate.put(symbol, other.getValue(symbol)); |
---|
| 78 | symbols.put(symbol, symbol); |
---|
[1285] | 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /* (non-Javadoc) |
---|
| 83 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#size() |
---|
| 84 | */ |
---|
| 85 | @Override |
---|
| 86 | public int size() { |
---|
| 87 | return delegate.size(); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /* (non-Javadoc) |
---|
| 91 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#isEmpty() |
---|
| 92 | */ |
---|
| 93 | @Override |
---|
| 94 | public boolean isEmpty() { |
---|
| 95 | return delegate.isEmpty(); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /* (non-Javadoc) |
---|
| 99 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#containsSymbol(java.lang.Object) |
---|
| 100 | */ |
---|
| 101 | @Override |
---|
[1853] | 102 | public boolean containsSymbol(ITask symbol) { |
---|
[1285] | 103 | if (symbol == null) { |
---|
| 104 | throw new IllegalArgumentException("symbol must not be null"); |
---|
| 105 | } |
---|
| 106 | |
---|
[1853] | 107 | return delegate.containsKey(symbol); |
---|
[1285] | 108 | } |
---|
| 109 | |
---|
| 110 | /* (non-Javadoc) |
---|
| 111 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#getValue(java.lang.Object) |
---|
| 112 | */ |
---|
| 113 | @Override |
---|
[1853] | 114 | public V getValue(ITask symbol) { |
---|
[1285] | 115 | if (symbol == null) { |
---|
| 116 | throw new IllegalArgumentException("symbol must not be null"); |
---|
| 117 | } |
---|
| 118 | |
---|
[1853] | 119 | return delegate.get(symbol); |
---|
[1285] | 120 | } |
---|
| 121 | |
---|
| 122 | /* (non-Javadoc) |
---|
| 123 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#addSymbol(java.lang.Object, java.lang.Object) |
---|
| 124 | */ |
---|
| 125 | @Override |
---|
[1853] | 126 | public void addSymbol(ITask symbol, V value) { |
---|
[1285] | 127 | if (symbol == null) { |
---|
| 128 | throw new IllegalArgumentException("symbol must not be null"); |
---|
| 129 | } |
---|
| 130 | |
---|
[1853] | 131 | delegate.put(symbol, value); |
---|
| 132 | symbols.put(symbol, symbol); |
---|
[1285] | 133 | } |
---|
| 134 | |
---|
| 135 | /* (non-Javadoc) |
---|
| 136 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#removeSymbol(java.lang.Object) |
---|
| 137 | */ |
---|
| 138 | @Override |
---|
[1853] | 139 | public V removeSymbol(ITask symbol) { |
---|
[1285] | 140 | if (symbol == null) { |
---|
| 141 | throw new IllegalArgumentException("symbol must not be null"); |
---|
| 142 | } |
---|
| 143 | |
---|
[1853] | 144 | symbols.remove(symbol); |
---|
| 145 | return delegate.remove(symbol); |
---|
[1285] | 146 | } |
---|
| 147 | |
---|
| 148 | /* (non-Javadoc) |
---|
| 149 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#getSymbols() |
---|
| 150 | */ |
---|
| 151 | @Override |
---|
[1853] | 152 | public Collection<ITask> getSymbols() { |
---|
[1285] | 153 | return symbols.values(); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | /* (non-Javadoc) |
---|
| 157 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#getValues() |
---|
| 158 | */ |
---|
| 159 | @Override |
---|
| 160 | public Collection<V> getValues() { |
---|
| 161 | return delegate.values(); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | /* (non-Javadoc) |
---|
| 165 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#clear() |
---|
| 166 | */ |
---|
| 167 | @Override |
---|
| 168 | public void clear() { |
---|
| 169 | delegate.clear(); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /* (non-Javadoc) |
---|
| 173 | * @see java.lang.Object#hashCode() |
---|
| 174 | */ |
---|
| 175 | @Override |
---|
| 176 | public int hashCode() { |
---|
| 177 | return delegate.hashCode(); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | /* (non-Javadoc) |
---|
| 181 | * @see java.lang.Object#equals(java.lang.Object) |
---|
| 182 | */ |
---|
| 183 | @SuppressWarnings("unchecked") |
---|
| 184 | @Override |
---|
| 185 | public boolean equals(Object obj) { |
---|
| 186 | if (this == obj) { |
---|
| 187 | return true; |
---|
| 188 | } |
---|
| 189 | else if (this.getClass().isInstance(obj)) { |
---|
| 190 | return delegate.equals(((TaskSymbolIdentityMap<V>) obj).delegate); |
---|
| 191 | } |
---|
| 192 | else { |
---|
| 193 | return false; |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | } |
---|