aboutsummaryrefslogtreecommitdiffstats
path: root/libs/xmpp-addr/src/main/java/rocks/xmpp/addr/AbstractJid.java
blob: 963c3a491f6710776ea29d7e45d483e70b866b25 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2017 Christian Schudt
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package rocks.xmpp.addr;

import java.text.Collator;
import java.util.Arrays;

/**
 * Abstract Jid implementation for both full and bare JIDs.
 *
 * @author Christian Schudt
 */
abstract class AbstractJid implements Jid {

    /**
     * Checks if the JID is a full JID.
     * <blockquote>
     * <p>The term "full JID" refers to an XMPP address of the form &lt;localpart@domainpart/resourcepart&gt; (for a particular authorized client or device associated with an account) or of the form &lt;domainpart/resourcepart&gt; (for a particular resource or script associated with a server).</p>
     * </blockquote>
     *
     * @return True, if the JID is a full JID; otherwise false.
     */
    @Override
    public final boolean isFullJid() {
        return getResource() != null;
    }

    /**
     * Checks if the JID is a bare JID.
     * <blockquote>
     * <p>The term "bare JID" refers to an XMPP address of the form &lt;localpart@domainpart&gt; (for an account at a server) or of the form &lt;domainpart&gt; (for a server).</p>
     * </blockquote>
     *
     * @return True, if the JID is a bare JID; otherwise false.
     */
    @Override
    public final boolean isBareJid() {
        return getResource() == null;
    }

    @Override
    public final boolean isDomainJid() {
        return getLocal() == null;
    }

    @Override
    public final boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof Jid)) {
            return false;
        }
        Jid other = (Jid) o;

        return (getLocal() == other.getLocal() || getLocal() != null && getLocal().equals(other.getLocal()))
            && (getDomain() == other.getDomain() || getDomain() != null && getDomain().equals(other.getDomain()))
            && (getResource() == other.getResource() || getResource() != null && getResource().equals(other.getResource()));
    }

    @Override
    public final int hashCode() {
        return Arrays.hashCode(new String[]{getLocal(), getDomain(), getResource()});
    }

    /**
     * Compares this JID with another JID. First domain parts are compared. If these are equal, local parts are compared
     * and if these are equal, too, resource parts are compared.
     *
     * @param o The other JID.
     * @return The comparison result.
     */
    @Override
    public final int compareTo(Jid o) {

        if (this == o) {
            return 0;
        }

        if (o != null) {
            final Collator collator = Collator.getInstance();
            int result;
            // First compare domain parts.
            if (getDomain() != null) {
                result = o.getDomain() != null ? collator.compare(getDomain(), o.getDomain()) : -1;
            } else {
                result = o.getDomain() != null ? 1 : 0;
            }
            // If the domains are equal, compare local parts.
            if (result == 0) {
                if (getLocal() != null) {
                    // If this local part is not null, but the other is null, move this down (1).
                    result = o.getLocal() != null ? collator.compare(getLocal(), o.getLocal()) : 1;
                } else {
                    // If this local part is null, but the other is not, move this up (-1).
                    result = o.getLocal() != null ? -1 : 0;
                }
            }
            // If the local parts are equal, compare resource parts.
            if (result == 0) {
                if (getResource() != null) {
                    // If this resource part is not null, but the other is null, move this down (1).
                    return o.getResource() != null ? collator.compare(getResource(), o.getResource()) : 1;
                } else {
                    // If this resource part is null, but the other is not, move this up (-1).
                    return o.getResource() != null ? -1 : 0;
                }
            }
            return result;
        } else {
            return -1;
        }
    }

    @Override
    public final int length() {
        return toString().length();
    }

    @Override
    public final char charAt(int index) {
        return toString().charAt(index);
    }

    @Override
    public final CharSequence subSequence(int start, int end) {
        return toString().subSequence(start, end);
    }

    /**
     * Returns the JID in its string representation, i.e. [ localpart "@" ] domainpart [ "/" resourcepart ].
     *
     * @return The JID.
     * @see #toEscapedString()
     */
    @Override
    public final String toString() {
        return toString(getLocal(), getDomain(), getResource());
    }

    @Override
    public final String toEscapedString() {
        return toString(getEscapedLocal(), getDomain(), getResource());
    }

    static String toString(String local, String domain, String resource) {
        StringBuilder sb = new StringBuilder();
        if (local != null) {
            sb.append(local).append('@');
        }
        sb.append(domain);
        if (resource != null) {
            sb.append('/').append(resource);
        }
        return sb.toString();
    }
}