Skip to content

Commit 69ca58d

Browse files
committed
xmpp-addr: Backfill missing class method for Java 1.7
This backfills missing class methods for `java.nio.charset.StandardCharsets` and `java.util.Objects` for compatibility with platforms which do not support these (mainly Android SDK versions <= 18).
1 parent ceaa313 commit 69ca58d

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

libs/xmpp-addr/src/main/java/rocks/xmpp/addr/AbstractJid.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
package rocks.xmpp.addr;
2626

2727
import java.text.Collator;
28-
import java.util.Objects;
28+
import java.util.Arrays;
2929

3030
/**
3131
* Abstract Jid implementation for both full and bare JIDs.
@@ -75,14 +75,14 @@ public final boolean equals(Object o) {
7575
}
7676
Jid other = (Jid) o;
7777

78-
return Objects.equals(getLocal(), other.getLocal())
79-
&& Objects.equals(getDomain(), other.getDomain())
80-
&& Objects.equals(getResource(), other.getResource());
78+
return (getLocal() == other.getLocal() || getLocal() != null && getLocal().equals(other.getLocal()))
79+
&& (getDomain() == other.getDomain() || getDomain() != null && getDomain().equals(other.getDomain()))
80+
&& (getResource() == other.getResource() || getResource() != null && getResource().equals(other.getResource()));
8181
}
8282

8383
@Override
8484
public final int hashCode() {
85-
return Objects.hash(getLocal(), getDomain(), getResource());
85+
return Arrays.hashCode(new String[]{getLocal(), getDomain(), getResource()});
8686
}
8787

8888
/**

libs/xmpp-addr/src/main/java/rocks/xmpp/addr/FullJid.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@
2929
import rocks.xmpp.util.cache.LruCache;
3030

3131
import java.net.IDN;
32-
import java.nio.charset.StandardCharsets;
32+
import java.nio.charset.Charset;
3333
import java.text.Normalizer;
3434
import java.util.Map;
35-
import java.util.Objects;
3635
import java.util.regex.Matcher;
3736
import java.util.regex.Pattern;
3837

@@ -109,6 +108,10 @@ private FullJid(final CharSequence local, final CharSequence domain, final CharS
109108

110109
final String unescapedLocalPart;
111110

111+
if (domain == null) {
112+
throw new NullPointerException();
113+
}
114+
112115
if (doUnescape) {
113116
unescapedLocalPart = unescape(local);
114117
} else {
@@ -126,7 +129,7 @@ private FullJid(final CharSequence local, final CharSequence domain, final CharS
126129
// character MUST be stripped before any other canonicalization steps
127130
// are taken.
128131
// Also validate, that the domain name can be converted to ASCII, i.e. validate the domain name (e.g. must not start with "_").
129-
final String strDomain = IDN.toASCII(LABEL_SEPARATOR_FINAL.matcher(Objects.requireNonNull(domain)).replaceAll(""), IDN.USE_STD3_ASCII_RULES);
132+
final String strDomain = IDN.toASCII(LABEL_SEPARATOR_FINAL.matcher(domain).replaceAll(""), IDN.USE_STD3_ASCII_RULES);
130133
enforcedLocalPart = escapedLocalPart != null ? PrecisProfiles.USERNAME_CASE_MAPPED.enforce(escapedLocalPart) : null;
131134
enforcedResource = resource != null ? PrecisProfiles.OPAQUE_STRING.enforce(resource) : null;
132135
// See https://tools.ietf.org/html/rfc5895#section-2
@@ -152,23 +155,26 @@ public Jid asBareJid() {
152155

153156
@Override
154157
public Jid withLocal(CharSequence local) {
155-
if (Objects.equals(local, this.getLocal())) {
158+
if (local == this.getLocal() || local != null && local.equals(this.getLocal())) {
156159
return this;
157160
}
158161
return new FullJid(local, getDomain(), getResource(), false, null);
159162
}
160163

161164
@Override
162165
public Jid withResource(CharSequence resource) {
163-
if (Objects.equals(resource, this.getResource())) {
166+
if (resource == this.getResource() || resource != null && resource.equals(this.getResource())) {
164167
return this;
165168
}
166169
return new FullJid(getLocal(), getDomain(), resource, false, asBareJid());
167170
}
168171

169172
@Override
170173
public Jid atSubdomain(CharSequence subdomain) {
171-
return new FullJid(getLocal(), Objects.requireNonNull(subdomain) + "." + getDomain(), getResource(), false, null);
174+
if (subdomain == null) {
175+
throw new NullPointerException();
176+
}
177+
return new FullJid(getLocal(), subdomain + "." + getDomain(), getResource(), false, null);
172178
}
173179

174180
@Override
@@ -206,7 +212,9 @@ public String getResource() {
206212
* @see <a href="https://xmpp.org/extensions/xep-0106.html">XEP-0106: JID Escaping</a>
207213
*/
208214
static Jid of(String jid, final boolean doUnescape) {
209-
Objects.requireNonNull(jid, "jid must not be null.");
215+
if (jid == null) {
216+
throw new NullPointerException("jid must not be null.");
217+
}
210218

211219
jid = jid.trim();
212220

@@ -278,7 +286,9 @@ private static String unescape(final CharSequence localPart) {
278286
}
279287

280288
private static void validateDomain(String domain) {
281-
Objects.requireNonNull(domain, "domain must not be null.");
289+
if (domain == null) {
290+
throw new NullPointerException("domain must not be null.");
291+
}
282292
if (domain.contains("@")) {
283293
// Prevent misuse of API.
284294
throw new IllegalArgumentException("domain must not contain a '@' sign");
@@ -297,7 +307,7 @@ private static void validateLength(CharSequence value, CharSequence part) {
297307
if (value.length() == 0) {
298308
throw new IllegalArgumentException(part + " must not be empty.");
299309
}
300-
if (value.toString().getBytes(StandardCharsets.UTF_8).length > 1023) {
310+
if (value.toString().getBytes(Charset.forName("UTF-8")).length > 1023) {
301311
throw new IllegalArgumentException(part + " must not be greater than 1023 bytes.");
302312
}
303313
}
@@ -391,7 +401,7 @@ public final String getResource() {
391401
*/
392402
@Override
393403
public final Jid withLocal(CharSequence local) {
394-
if (Objects.equals(local, this.getLocal())) {
404+
if (local == this.getLocal() || local != null && local.equals(this.getLocal())) {
395405
return this;
396406
}
397407
return new FullJid(local, getDomain(), getResource(), false, null);
@@ -408,7 +418,7 @@ public final Jid withLocal(CharSequence local) {
408418
*/
409419
@Override
410420
public final Jid withResource(CharSequence resource) {
411-
if (Objects.equals(resource, this.getResource())) {
421+
if (resource == this.getResource() || resource != null && resource.equals(this.getResource())) {
412422
return this;
413423
}
414424
return new FullJid(getLocal(), getDomain(), resource, false, asBareJid());
@@ -424,7 +434,10 @@ public final Jid withResource(CharSequence resource) {
424434
*/
425435
@Override
426436
public final Jid atSubdomain(CharSequence subdomain) {
427-
return new FullJid(getLocal(), Objects.requireNonNull(subdomain) + "." + getDomain(), getResource(), false, null);
437+
if (subdomain != null) {
438+
throw new NullPointerException();
439+
}
440+
return new FullJid(getLocal(), subdomain + "." + getDomain(), getResource(), false, null);
428441
}
429442

430443
/**

libs/xmpp-addr/src/main/java/rocks/xmpp/addr/MalformedJid.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
package rocks.xmpp.addr;
2626

27-
import java.util.Objects;
28-
2927
/**
3028
* Represents a malformed JID in order to handle the <code>jid-malformed</code> error.
3129
* <p>
@@ -96,7 +94,10 @@ public Jid withResource(CharSequence resource) {
9694

9795
@Override
9896
public Jid atSubdomain(CharSequence subdomain) {
99-
return new MalformedJid(localPart, Objects.requireNonNull(subdomain) + "." + domainPart, resourcePart, cause);
97+
if (subdomain == null) {
98+
throw new NullPointerException();
99+
}
100+
return new MalformedJid(localPart, subdomain + "." + domainPart, resourcePart, cause);
100101
}
101102

102103
@Override

0 commit comments

Comments
 (0)