| 23 | import java.util.Set; |
| 24 | |
| 25 | public class MessageId { |
| 26 | static final MessageId unanchoredMsgId = makeId(Collections.emptyMap()); |
| 27 | |
| 28 | private Map<Long, Long> anchorsToIds; |
| 29 | |
| 30 | protected MessageId(Map<Long, Long> anchorsToIds) { |
| 31 | this.anchorsToIds = anchorsToIds; |
| 32 | } |
| 33 | |
| 34 | public static long generateId(Random rand) { |
| 35 | return rand.nextLong(); |
| 36 | } |
| 37 | |
| 38 | public static MessageId makeUnanchored() { |
| 39 | return unanchoredMsgId; |
| 40 | } |
| 41 | |
| 42 | public static MessageId makeId(Map<Long, Long> anchorsToIds) { |
| 43 | return new MessageId(anchorsToIds); |
| 44 | } |
| 45 | |
| 46 | public static MessageId makeRootId(long id, long val) { |
| 47 | Map<Long, Long> anchorsToIds = new HashMap<>(); |
| 48 | anchorsToIds.put(id, val); |
| 49 | return new MessageId(anchorsToIds); |
| 50 | } |
| 51 | |
| 52 | public static MessageId deserialize(Input in) throws IOException { |
| 53 | int numAnchors = in.readInt(true); |
| 54 | Map<Long, Long> anchorsToIds = new HashMap<>(); |
| 55 | for (int i = 0; i < numAnchors; i++) { |
| 56 | anchorsToIds.put(in.readLong(), in.readLong()); |
| 57 | } |
| 58 | return new MessageId(anchorsToIds); |
| 59 | } |
| 60 | |
| 61 | public Map<Long, Long> getAnchorsToIds() { |
| 62 | return anchorsToIds; |
| 63 | } |
| 64 | |
| 65 | public Set<Long> getAnchors() { |
| 66 | return anchorsToIds.keySet(); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public int hashCode() { |
| 71 | return anchorsToIds.hashCode(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean equals(Object other) { |
| 76 | return other instanceof MessageId && anchorsToIds.equals(((MessageId) other).anchorsToIds); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public String toString() { |
| 81 | return anchorsToIds.toString(); |
| 82 | } |