aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/xmpp/mam/MamReference.java
blob: 788e0488991266b7cac742dbd2d0ae062c37db0f (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package de.pixart.messenger.xmpp.mam;

public class MamReference {

    private final long timestamp;
    private final String reference;

    public MamReference(long timestamp) {
        this.timestamp = timestamp;
        this.reference = null;
    }

    public MamReference(long timestamp, String reference) {
        this.timestamp = timestamp;
        this.reference = reference;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public String getReference() {
        return reference;
    }

    public boolean greaterThan(MamReference b) {
        return timestamp > b.getTimestamp();
    }

    public boolean greaterThan(long b) {
        return timestamp > b;
    }

    public static MamReference max(MamReference a, MamReference b) {
        if (a != null && b != null) {
            return a.timestamp > b.timestamp ? a : b;
        } else if (a != null) {
            return a;
        } else {
            return b;
        }
    }

    public static MamReference max(MamReference a, long b) {
        return max(a,new MamReference(b));
    }

    public static MamReference fromAttribute(String attr) {
        if (attr == null) {
            return new MamReference(0);
        } else {
            String[] attrs = attr.split(":");
            try {
                long timestamp = Long.parseLong(attrs[0]);
                if (attrs.length >= 2) {
                    return new MamReference(timestamp,attrs[1]);
                } else {
                    return new MamReference(timestamp);
                }
            } catch (Exception e) {
                return new MamReference(0);
            }
        }
    }

    public MamReference timeOnly() {
        return reference == null ? this : new MamReference(timestamp);
    }
}