summaryrefslogtreecommitdiffstats
path: root/src/main/java/derms/net/tomulticast/Message.java
blob: a85e827e58e416568de98c74a1a126c8e0cd2f04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package derms.net.tomulticast;

import derms.net.MessagePayload;

class Message<T extends MessagePayload> implements MessagePayload, Comparable<Message<T>> {
    long seq; // Sequence number.
    T payload;

    Message(long seq, T payload) {
        this.seq = seq;
        this.payload = payload;
    }

    @Override
    public int hash() {
        return (int) seq * payload.hash();
    }

    @Override
    public int hashCode() {
        return hash();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (obj.getClass() != this.getClass())
            return false;
        Message<?> other = (Message<?>) obj;
        if (other.payload.getClass() != this.payload.getClass())
            return false;
        return other.seq == this.seq && other.hash() == this.hash();
    }

    @Override
    public int compareTo(Message<T> other) {
        return Long.compare(this.seq, other.seq);
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + "{" + seq + ", " + payload + "}";
    }
}