1 | package de.uka.ipd.sdq.probespec.framework.probes; |
2 | |
3 | import java.util.HashMap; |
4 | import java.util.Map; |
5 | |
6 | import de.uka.ipd.sdq.probespec.framework.ProbeType; |
7 | import de.uka.ipd.sdq.probespec.framework.exceptions.ProbeStrategyNotFoundException; |
8 | |
9 | /** |
10 | * Basic implementation of the {@link IProbeStrategyRegistry} interface. |
11 | * |
12 | * @author Philipp Merkle |
13 | * |
14 | */ |
15 | public class ProbeStrategyRegistry implements IProbeStrategyRegistry { |
16 | |
17 | private Map<ProbeTypeEntityPair, IProbeStrategy> strategiesMap; |
18 | |
19 | public ProbeStrategyRegistry() { |
20 | strategiesMap = new HashMap<ProbeTypeEntityPair, IProbeStrategy>(); |
21 | } |
22 | |
23 | @Override |
24 | public IProbeStrategy getProbeStrategy(ProbeType type, |
25 | Object measurableEntity) { |
26 | ProbeTypeEntityPair pair = new ProbeTypeEntityPair(type, |
27 | measurableEntity); |
28 | IProbeStrategy strategy = strategiesMap.get(pair); |
29 | if (strategy == null) |
30 | throw new ProbeStrategyNotFoundException( |
31 | "Could not find a ProbeStrategy for probe type " |
32 | + type.name() + " that is able to measure a " |
33 | + measurableEntity.getClass().getSimpleName()); |
34 | return strategy; |
35 | } |
36 | |
37 | @Override |
38 | public void registerProbeStrategy(IProbeStrategy strategy, ProbeType type, |
39 | Object measurableEntity) { |
40 | ProbeTypeEntityPair pair = new ProbeTypeEntityPair(type, |
41 | measurableEntity); |
42 | strategiesMap.put(pair, strategy); |
43 | } |
44 | |
45 | private class ProbeTypeEntityPair { |
46 | |
47 | private ProbeType type; |
48 | |
49 | private Object entity; |
50 | |
51 | public ProbeTypeEntityPair(ProbeType type, Object entity) { |
52 | this.type = type; |
53 | this.entity = entity; |
54 | } |
55 | |
56 | @Override |
57 | public int hashCode() { |
58 | final int prime = 31; |
59 | int result = 1; |
60 | result = prime * result + getOuterType().hashCode(); |
61 | result = prime * result |
62 | + ((entity == null) ? 0 : entity.hashCode()); |
63 | result = prime * result + ((type == null) ? 0 : type.hashCode()); |
64 | return result; |
65 | } |
66 | |
67 | @Override |
68 | public boolean equals(Object obj) { |
69 | if (this == obj) { |
70 | return true; |
71 | } |
72 | if (obj == null) { |
73 | return false; |
74 | } |
75 | if (!(obj instanceof ProbeTypeEntityPair)) { |
76 | return false; |
77 | } |
78 | ProbeTypeEntityPair other = (ProbeTypeEntityPair) obj; |
79 | if (!getOuterType().equals(other.getOuterType())) { |
80 | return false; |
81 | } |
82 | if (entity == null) { |
83 | if (other.entity != null) { |
84 | return false; |
85 | } |
86 | } else if (!entity.equals(other.entity)) { |
87 | return false; |
88 | } |
89 | if (type == null) { |
90 | if (other.type != null) { |
91 | return false; |
92 | } |
93 | } else if (!type.equals(other.type)) { |
94 | return false; |
95 | } |
96 | return true; |
97 | } |
98 | |
99 | private ProbeStrategyRegistry getOuterType() { |
100 | return ProbeStrategyRegistry.this; |
101 | } |
102 | |
103 | } |
104 | |
105 | } |