source: branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/TaskSymbolIdentityMap.java @ 1615

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