summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGurmeen Bindra <gurmeen.bindra@isode.com>2012-04-16 11:08:41 (GMT)
committerKevin Smith <git@kismith.co.uk>2012-04-19 10:45:37 (GMT)
commitbae6e6298a617b801a15cbdba13d6759d8b44f31 (patch)
tree8508bd612b8ace3ba25311d2fad181ca5761662a /src/com/isode/stroke/jid/JID.java
parent00b0f316b9125031f30151cacdf2dcf217b8fa01 (diff)
downloadstroke-bae6e6298a617b801a15cbdba13d6759d8b44f31.zip
stroke-bae6e6298a617b801a15cbdba13d6759d8b44f31.tar.bz2
Add a copy constructor to Presence and Stanza + compare method in JID
This patch adds a copy constructor to the Presence class(and hence base class Staza as well). It also ports the compare method to JID class. Also added javadocs to Presence and Stanza classes. Test-information: tested using the Work In Progress code that ports MUC Admin to stroke Reviewer: Kevin Smith <kevin.smith@isode.com>
Diffstat (limited to 'src/com/isode/stroke/jid/JID.java')
-rw-r--r--src/com/isode/stroke/jid/JID.java43
1 files changed, 39 insertions, 4 deletions
diff --git a/src/com/isode/stroke/jid/JID.java b/src/com/isode/stroke/jid/JID.java
index a93bebc..acd1430 100644
--- a/src/com/isode/stroke/jid/JID.java
+++ b/src/com/isode/stroke/jid/JID.java
@@ -10,8 +10,6 @@ package com.isode.stroke.jid;
import com.ibm.icu.text.StringPrep;
import com.ibm.icu.text.StringPrepParseException;
-import java.util.logging.Level;
-import java.util.logging.Logger;
/**
* JID helper.
@@ -32,7 +30,7 @@ import java.util.logging.Logger;
* <p>
* This is an immutable class.
*/
-public class JID {
+public class JID implements Comparable<JID> {
public enum CompareType {
WithResource, WithoutResource
};
@@ -50,7 +48,8 @@ public class JID {
/**
* Create a JID using the JID(String) constructor.
- * @param jid String formatted JID.
+ * @param jid String formatted JID, not null
+ * @return Jabber ID, not null
*/
public static JID fromString(final String jid) {
return new JID(jid);
@@ -191,6 +190,7 @@ public class 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 resource_ == null;
@@ -237,6 +237,36 @@ public class JID {
boolean resourceMatch = (resource1 == null && resource2 == null) || (resource1 != null && resource1.equals(resource2));
return nodeMatch && domainMatch && resourceMatch;
}
+
+ /**
+ * Compare two Jabber IDs by either including the resource part or
+ * excluding it
+ * @param o other JID to which this JID should be compared, can be null
+ * @param compareType comparison type
+ * @return 0 if equal, 1 if other JID is greater or -1 if this JID is greater
+ */
+ public int compare(final JID o, CompareType compareType) {
+ if(this == o) return 0;
+ if(o == null) return 1;
+ if (!node_ .equals(o.node_)) {
+ return node_.compareTo(o.node_);
+ }
+ if (!domain_ .equals(o.domain_)) {
+ return domain_.compareTo(o.domain_);
+ }
+ if (compareType == CompareType.WithResource) {
+ String res1 = getResource();
+ String res2 = o.getResource();
+ if(res1 != null && res2 != null) {
+ return res1.compareTo(res2);
+ }
+ if (res1 == null) { return -1; }
+ if (res2 == null) { return 1; }
+ }
+ return 0;
+
+ }
+
@Override
public int hashCode() {
@@ -246,4 +276,9 @@ public class JID {
hash = 73 * hash + (this.resource_ != null ? this.resource_.hashCode() : 0);
return hash;
}
+
+ @Override
+ public int compareTo(JID o) {
+ return compare(o, CompareType.WithResource);
+ }
}