diff options
| -rw-r--r-- | src/com/isode/stroke/jid/JID.java | 36 | ||||
| -rw-r--r-- | test/com/isode/stroke/jid/JIDTest.java | 2 |
2 files changed, 14 insertions, 24 deletions
diff --git a/src/com/isode/stroke/jid/JID.java b/src/com/isode/stroke/jid/JID.java index 8b62ca1..07b2084 100644 --- a/src/com/isode/stroke/jid/JID.java +++ b/src/com/isode/stroke/jid/JID.java @@ -43,16 +43,15 @@ import com.isode.stroke.idn.IDNConverter; public class JID implements Comparable<JID> { public enum CompareType { WithResource, WithoutResource }; private boolean valid_ = true; - private boolean hasResource_ = true; private String node_ = ""; private String domain_ = ""; - private String resource_ = ""; + private String resource_ = null; private static IDNConverter idnConverter = new ICUConverter(); /** * Create an invalid JID. */ public JID() { @@ -78,34 +77,32 @@ public class JID implements Comparable<JID> { * alice@wonderland.lit * alice@wonderland.lit/TeaParty * * @param jid String representation. Invalid JID if null or invalid. */ public JID(String jid) { - NotNull.exceptIfNull(jid, "jid"); - valid_ = true; + NotNull.exceptIfNull(jid, "jid"); initializeFromString(jid); } private void initializeFromString(String jid) { if (jid.startsWith("@")) { valid_ = false; node_ = ""; domain_ = ""; - resource_ = ""; + resource_ = null; return; } String bare; String resource; String[] parts = jid.split("/", 2); if (parts.length > 1) { bare = parts[0]; resource = parts[1]; } else { - hasResource_ = false; resource = null; bare = jid; } String[] nodeAndDomain = bare.split("@", 2); String node; String domain; @@ -142,17 +139,12 @@ public class JID implements Comparable<JID> { * @param domain JID domain part. * @param resource JID resource part. */ public JID(String node, String domain, String resource) { NotNull.exceptIfNull(node, "node"); NotNull.exceptIfNull(domain, "domain"); - valid_ = true; - hasResource_ = (resource != null); - if (hasResource_ && resource.isEmpty()) { - valid_ = false; - } nameprepAndSetComponents(node, domain, resource); } private void nameprepAndSetComponents(String node, String domain, String resource) { if (domain.isEmpty() || idnConverter.getIDNAEncoded(domain) == null) { valid_ = false; @@ -164,18 +156,21 @@ public class JID implements Comparable<JID> { domain_ = idnConverter.getStringPrepared(domain, IDNConverter.StringPrepProfile.NamePrep); resource_ = resource != null ? idnConverter.getStringPrepared(resource, IDNConverter.StringPrepProfile.XMPPResourcePrep) : null; } catch (IllegalArgumentException e) { valid_ = false; return; } + if (resource_ != null && resource_.isEmpty()) { + valid_ = false; + } if (domain_.isEmpty()) { valid_ = false; return; } } - + /** * @return Is a correctly-formatted JID. */ public boolean isValid() { return valid_; } @@ -206,13 +201,13 @@ public class JID implements Comparable<JID> { /** * Is a bare JID, i.e. has no resource part. * @return true if the resource part of JID is null, false if not */ public boolean isBare() { - return !hasResource_; + return resource_ == null; } /** * Get the JID without a resource. * @return non-null. Invalid if the original is invalid. */ @@ -306,15 +301,12 @@ public class JID implements Comparable<JID> { public void setIDNConverter(IDNConverter converter) { idnConverter = converter; } @Override public String toString() { - if (!valid_) { - return ""; - } String string = new String(); if (node_.length()!=0) { string += node_ + "@"; } string += domain_; if (!isBare()) { @@ -345,20 +337,18 @@ public class JID implements Comparable<JID> { return node_.compareTo(o.node_); } if (!domain_ .equals(o.domain_)) { return domain_.compareTo(o.domain_); } if (compareType == CompareType.WithResource) { - String res1 = resource_; - String res2 = o.resource_; - if(res1 != null && res2 != null) { - return res1.compareTo(res2); + if (isBare() != o.isBare()) { + return !isBare() ? 1 : -1; + } + if (!isBare()) { + return resource_.compareTo(o.resource_); } - if(res1 == null && res2 == null) { return 0; } - if (res1 == null) { return -1; } - if (res2 == null) { return 1; } } return 0; } @Override diff --git a/test/com/isode/stroke/jid/JIDTest.java b/test/com/isode/stroke/jid/JIDTest.java index 69bee05..758bd5f 100644 --- a/test/com/isode/stroke/jid/JIDTest.java +++ b/test/com/isode/stroke/jid/JIDTest.java @@ -48,13 +48,13 @@ public class JIDTest { @Test public void testConstructorWithString_EmptyResource() { JID testling = new JID("bar/"); - assertTrue(testling.isValid()); + assertFalse(testling.isValid()); assertFalse(testling.isBare()); } @Test public void testConstructorWithString_NoNode() { |
Swift