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> |
---|
26 | * symbol map implementation for task instances considering two task instances as equal if their |
---|
27 | * tasks are identical |
---|
28 | * </p> |
---|
29 | * |
---|
30 | * @author Patrick Harms |
---|
31 | */ |
---|
32 | public class TaskSymbolIdentityMap<V> implements SymbolMap<ITask, V> { |
---|
33 | |
---|
34 | /** */ |
---|
35 | private static final long serialVersionUID = 1L; |
---|
36 | |
---|
37 | /** |
---|
38 | * <p> |
---|
39 | * internally used map for implementing the symbol map interface |
---|
40 | * </p> |
---|
41 | */ |
---|
42 | private Map<ITask, V> delegate; |
---|
43 | |
---|
44 | /** |
---|
45 | * <p> |
---|
46 | * mapping between the tasks and the real symbols stored in the map, i.e. the task instances |
---|
47 | * </p> |
---|
48 | */ |
---|
49 | private Map<ITask, ITask> symbols; |
---|
50 | |
---|
51 | /** |
---|
52 | * <p> |
---|
53 | * initializes this map |
---|
54 | * </p> |
---|
55 | */ |
---|
56 | public TaskSymbolIdentityMap() { |
---|
57 | delegate = new HashMap<ITask, V>(); |
---|
58 | symbols = new HashMap<ITask, ITask>(); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * <p> |
---|
63 | * copy constructor |
---|
64 | * </p> |
---|
65 | * |
---|
66 | * @param other the map to be copied |
---|
67 | */ |
---|
68 | public TaskSymbolIdentityMap(SymbolMap<ITask, V> other) { |
---|
69 | if (other == null) { |
---|
70 | throw new IllegalArgumentException("other map must not be null"); |
---|
71 | } |
---|
72 | |
---|
73 | delegate = new HashMap<ITask, V>(); |
---|
74 | symbols = new HashMap<ITask, ITask>(); |
---|
75 | |
---|
76 | for (ITask symbol : other.getSymbols()) { |
---|
77 | delegate.put(symbol, other.getValue(symbol)); |
---|
78 | symbols.put(symbol, symbol); |
---|
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 |
---|
102 | public boolean containsSymbol(ITask symbol) { |
---|
103 | if (symbol == null) { |
---|
104 | throw new IllegalArgumentException("symbol must not be null"); |
---|
105 | } |
---|
106 | |
---|
107 | return delegate.containsKey(symbol); |
---|
108 | } |
---|
109 | |
---|
110 | /* (non-Javadoc) |
---|
111 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#getValue(java.lang.Object) |
---|
112 | */ |
---|
113 | @Override |
---|
114 | public V getValue(ITask symbol) { |
---|
115 | if (symbol == null) { |
---|
116 | throw new IllegalArgumentException("symbol must not be null"); |
---|
117 | } |
---|
118 | |
---|
119 | return delegate.get(symbol); |
---|
120 | } |
---|
121 | |
---|
122 | /* (non-Javadoc) |
---|
123 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#addSymbol(java.lang.Object, java.lang.Object) |
---|
124 | */ |
---|
125 | @Override |
---|
126 | public void addSymbol(ITask symbol, V value) { |
---|
127 | if (symbol == null) { |
---|
128 | throw new IllegalArgumentException("symbol must not be null"); |
---|
129 | } |
---|
130 | |
---|
131 | delegate.put(symbol, value); |
---|
132 | symbols.put(symbol, symbol); |
---|
133 | } |
---|
134 | |
---|
135 | /* (non-Javadoc) |
---|
136 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#removeSymbol(java.lang.Object) |
---|
137 | */ |
---|
138 | @Override |
---|
139 | public V removeSymbol(ITask symbol) { |
---|
140 | if (symbol == null) { |
---|
141 | throw new IllegalArgumentException("symbol must not be null"); |
---|
142 | } |
---|
143 | |
---|
144 | symbols.remove(symbol); |
---|
145 | return delegate.remove(symbol); |
---|
146 | } |
---|
147 | |
---|
148 | /* (non-Javadoc) |
---|
149 | * @see de.ugoe.cs.autoquest.usageprofiles.SymbolMap#getSymbols() |
---|
150 | */ |
---|
151 | @Override |
---|
152 | public Collection<ITask> getSymbols() { |
---|
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 | } |
---|