From 0794a4c6cc50b087bf12622ed6830f93e9250481 Mon Sep 17 00:00:00 2001 From: Alan Young Date: Tue, 13 Sep 2016 09:47:18 +0100 Subject: 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 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 { }; 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 { * @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 { valid_ = false; node_ = ""; domain_ = ""; - resource_ = ""; + resource_ = null; return; } @@ -102,7 +100,6 @@ public class JID implements Comparable { bare = parts[0]; resource = parts[1]; } else { - hasResource_ = false; resource = null; bare = jid; } @@ -145,11 +142,6 @@ public class JID implements Comparable { 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 { 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 { * @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 { @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 { 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()); } -- cgit v0.10.2-6-g49f6