[8780cbec] | 1 | package edu.stanford.peer.rbtm.util; |
---|
| 2 | |
---|
| 3 | import java.util.*; |
---|
| 4 | |
---|
| 5 | import edu.stanford.peer.rbtm.RBTMConstants; |
---|
| 6 | //import edu.stanford.peer.rbtm.util.*; |
---|
| 7 | |
---|
| 8 | /** Stores results and their evidences. */ |
---|
| 9 | public class ResultEvidenceMap implements RBTMConstants |
---|
| 10 | { |
---|
| 11 | static final public int DEFAULT_CAPACITY = 23; |
---|
| 12 | |
---|
| 13 | //private int _type = TRACK_NONE; |
---|
| 14 | private int _type = TRACK_ALL; |
---|
| 15 | private HashMap _map; |
---|
| 16 | |
---|
| 17 | public ResultEvidenceMap(int trackType, int capacity) { |
---|
| 18 | _type = trackType; |
---|
| 19 | _map = new HashMap(capacity); |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | public ResultEvidenceMap() { |
---|
| 23 | this(TRACK_ALL, DEFAULT_CAPACITY); |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | public ResultEvidenceMap(int trackType) { |
---|
| 27 | this(trackType, DEFAULT_CAPACITY); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * Retrieve a Result-to-Evidence mapping. If constructed with trackType |
---|
| 32 | * TRACK_NONE, the evidence will be null. |
---|
| 33 | * @return the evidence, if any, bound to the result. |
---|
| 34 | */ |
---|
| 35 | public Object getResultEvidence(Object result) { |
---|
| 36 | return _map.get(result); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * Add a Result-to-Evidence mapping. |
---|
| 41 | * @return true if this is a new Result, false if it is not |
---|
| 42 | */ |
---|
| 43 | public boolean putResultEvidence(Object result, Object evidence) { |
---|
| 44 | if (! _map.containsKey(result)) { // New result |
---|
| 45 | switch (_type) { |
---|
| 46 | case TRACK_NONE: |
---|
| 47 | _map.put(result, null); |
---|
| 48 | break; |
---|
| 49 | case TRACK_ANY: |
---|
| 50 | _map.put(result, evidence); |
---|
| 51 | break; |
---|
| 52 | case TRACK_ALL: |
---|
| 53 | HashSet eviSet = new HashSet(7); |
---|
| 54 | eviSet.add(evidence); |
---|
| 55 | System.out.println("###Adding " + result + "," |
---|
| 56 | + evidence); |
---|
| 57 | _map.put(result, eviSet); |
---|
| 58 | break; |
---|
| 59 | } |
---|
| 60 | return true; |
---|
| 61 | } else { // Result already exist |
---|
| 62 | if (_type == TRACK_ALL) { |
---|
| 63 | ((Set)_map.get(result)).add(evidence); |
---|
| 64 | } |
---|
| 65 | return false; |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | public boolean containsResult(Object result) { |
---|
| 70 | return _map.containsKey(result); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | public Set resultSet() { |
---|
| 74 | return _map.keySet(); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | public String toString() { |
---|
| 78 | return "["+ _type + "]" + _map.toString(); |
---|
| 79 | //return _map.toString(); |
---|
| 80 | } |
---|
| 81 | } |
---|