diff options
author | Alan Young <consult.awy@gmail.com> | 2016-09-13 08:47:18 (GMT) |
---|---|---|
committer | Alan Young <consult.awy@gmail.com> | 2016-09-13 09:42:12 (GMT) |
commit | 0794a4c6cc50b087bf12622ed6830f93e9250481 (patch) | |
tree | bd1aed3c117c209ae42258444ef5f6c0fc53b944 | |
parent | 4e70a84a8a792cfeae96bf7a0cb4c9b549e44d29 (diff) | |
download | stroke-0794a4c6cc50b087bf12622ed6830f93e9250481.zip stroke-0794a4c6cc50b087bf12622ed6830f93e9250481.tar.bz2 |
Rework JID class internal representation of resource presence.
Remove explicit hasResource_ boolean and use non-null status of
resource_ instead. Mostly test this with isBare(). Tidy up compare()
method.
A JID is valid by default. Remove extraneous setting of valid_ and
centralize testing of validity to nameprepAndSetComponents().
Ensure that resource_ is initialized to null by default so that it is
correct when a JID instance is restored using some sort of
serialization, including com.google.gson.Gson which is used by Swift.
All JID test cases pass, including ones using invalid "x@y/" test string
representations.
Change-Id: Ib77a7cde03e8390c405633cddea5939aa9e0b576
-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 @@ -46,10 +46,9 @@ public class JID implements Comparable<JID> { }; 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(); /** @@ -81,8 +80,7 @@ public class JID implements Comparable<JID> { * @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); } @@ -91,7 +89,7 @@ public class JID implements Comparable<JID> { valid_ = false; node_ = ""; domain_ = ""; - resource_ = ""; + resource_ = null; return; } @@ -102,7 +100,6 @@ public class JID implements Comparable<JID> { bare = parts[0]; resource = parts[1]; } else { - hasResource_ = false; resource = null; bare = jid; } @@ -145,11 +142,6 @@ public class JID implements Comparable<JID> { 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); } @@ -167,12 +159,15 @@ public class JID implements Comparable<JID> { valid_ = false; return; } + if (resource_ != null && resource_.isEmpty()) { + valid_ = false; + } if (domain_.isEmpty()) { valid_ = false; return; } } - + /** * @return Is a correctly-formatted JID. */ @@ -209,7 +204,7 @@ public class JID implements Comparable<JID> { * @return true if the resource part of JID is null, false if not */ public boolean isBare() { - return !hasResource_; + return resource_ == null; } /** @@ -309,9 +304,6 @@ public class JID implements Comparable<JID> { @Override public String toString() { - if (!valid_) { - return ""; - } String string = new String(); if (node_.length()!=0) { string += node_ + "@"; @@ -348,14 +340,12 @@ public class JID implements Comparable<JID> { 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; } 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 @@ -51,7 +51,7 @@ public class JIDTest { public void testConstructorWithString_EmptyResource() { JID testling = new JID("bar/"); - assertTrue(testling.isValid()); + assertFalse(testling.isValid()); assertFalse(testling.isBare()); } |