1 | package dk.deepthought.sidious.util; |
2 | |
3 | import java.util.HashMap; |
4 | import java.util.Map; |
5 | |
6 | import net.jcip.annotations.ThreadSafe; |
7 | import dk.deepthought.sidious.supportsystem.PlanRequester; |
8 | import dk.deepthought.sidious.supportsystem.SuperLinkID; |
9 | |
10 | /** |
11 | * This class implements the <code>synchronized</code> access functionality of |
12 | * a <code>HashMap</code>. |
13 | * |
14 | * @see java.util.HashMap |
15 | * |
16 | * @author Deepthought |
17 | * |
18 | */ |
19 | @ThreadSafe |
20 | public class SidiousMap { |
21 | |
22 | /** |
23 | * Internal map instance. |
24 | */ |
25 | private final Map<SuperLinkID, PlanRequester> map = new HashMap<SuperLinkID, PlanRequester>(); |
26 | |
27 | /** |
28 | * Empty default constructor. |
29 | */ |
30 | public SidiousMap() { |
31 | } |
32 | |
33 | /** |
34 | * Returns the PlanRequester to which the specified id is mapped in this |
35 | * identity map, or <code>null</code> if the map contains no mapping for |
36 | * this id. |
37 | * |
38 | * @param id |
39 | * the id whose associated requester is to be returned |
40 | * @return the requester to which this map maps the specified id, or |
41 | * <code>null</code> if the map contains no mapping for this id |
42 | */ |
43 | public PlanRequester get(SuperLinkID id) { |
44 | PlanRequester planRequester = null; |
45 | synchronized (map) { |
46 | planRequester = map.get(id); |
47 | } |
48 | return planRequester; |
49 | } |
50 | |
51 | /** |
52 | * Associates the specified id with the specified requester in this map. If |
53 | * the map previously contained a mapping for this id, the old requester is |
54 | * replaced. |
55 | * |
56 | * @param id |
57 | * key with which the specified requester is to be associated |
58 | * @param requester |
59 | * value to be associated with the specified id |
60 | * @return the value previously associated with the id, <code>null</code> |
61 | * if the map did not previously contain a mapping for this id. |
62 | */ |
63 | public PlanRequester put(SuperLinkID id, |
64 | PlanRequester requester) { |
65 | PlanRequester old = null; |
66 | synchronized (map) { |
67 | old = map.put(id, requester); |
68 | } |
69 | return old; |
70 | } |
71 | |
72 | /** |
73 | * Removes the mapping for this key from this map if it is present. |
74 | * |
75 | * @param id |
76 | * the id for whic to remove the mapping |
77 | */ |
78 | public synchronized void remove(SuperLinkID id) { |
79 | map.remove(id); |
80 | } |
81 | |
82 | /** |
83 | * Returns the number of key-value mappings in this sidious map. |
84 | * |
85 | * @return the number of key-value mappings in this map. |
86 | */ |
87 | public synchronized int size() { |
88 | return map.size(); |
89 | } |
90 | |
91 | } |