| 1 | package de.uka.ipd.sdq.probespec.framework.utils; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.Map; |
| 5 | import java.util.Map.Entry; |
| 6 | import java.util.concurrent.ConcurrentHashMap; |
| 7 | |
| 8 | /** |
| 9 | * This class supports the generation of numeric probe set ids required when |
| 10 | * using the Probe Specification. |
| 11 | * |
| 12 | * @author Philipp Merkle |
| 13 | * |
| 14 | */ |
| 15 | public class ProbeSetIDGenerator { |
| 16 | |
| 17 | private Map<String, Integer> idMap; |
| 18 | |
| 19 | private int lastId = -1; |
| 20 | |
| 21 | public ProbeSetIDGenerator() { |
| 22 | idMap = new ConcurrentHashMap<String, Integer>(); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Generates a numeric probe set id satisfying two conditions: When the |
| 27 | * String passed by the parameter id has not yet been passed before, the |
| 28 | * returned probe set id will be greater than all probe set ids generated |
| 29 | * before. Else the same probe set id generated previously will be returned. |
| 30 | * Thus, the same values for id will be mapped to the same numerical probe |
| 31 | * set id. |
| 32 | * |
| 33 | * @param ID |
| 34 | * @return |
| 35 | */ |
| 36 | public int obtainId(String ID) { |
| 37 | Integer foundId = idMap.get(ID); |
| 38 | if (foundId == null) { |
| 39 | idMap.put(ID, ++lastId); |
| 40 | return lastId; |
| 41 | } |
| 42 | return foundId; |
| 43 | } |
| 44 | |
| 45 | public String obtainOriginalId(Integer id) { |
| 46 | for (Entry<String, Integer> e : idMap.entrySet()) { |
| 47 | if (e.getValue().equals(id)) { |
| 48 | return e.getKey(); |
| 49 | } |
| 50 | } |
| 51 | return null; |
| 52 | } |
| 53 | |
| 54 | } |