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
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>
-rw-r--r--src/com/isode/stroke/elements/Presence.java53
-rw-r--r--src/com/isode/stroke/elements/Stanza.java70
-rw-r--r--src/com/isode/stroke/jid/JID.java43
3 files changed, 162 insertions, 4 deletions
diff --git a/src/com/isode/stroke/elements/Presence.java b/src/com/isode/stroke/elements/Presence.java
index acf0a40..392573f 100644
--- a/src/com/isode/stroke/elements/Presence.java
+++ b/src/com/isode/stroke/elements/Presence.java
@@ -8,28 +8,61 @@
*/
package com.isode.stroke.elements;
+/**
+ * Class representing Presence stanza
+ *
+ */
public class Presence extends Stanza {
private Type type_;
+ /**
+ * Presence Stanza Type
+ *
+ */
public enum Type {
Available, Error, Probe, Subscribe, Subscribed, Unavailable, Unsubscribe, Unsubscribed
};
+ /**
+ * Create the Presence object
+ */
public Presence() {
type_ = Type.Available;
}
+
+ /**
+ * Create a Presence object from the specified object by
+ * creating a copy
+ * @param copy presence object to be copied, not null
+ */
+ public Presence(Presence copy) {
+ super(copy);
+ this.type_ = copy.type_;
+ }
+ /**
+ * Create the Presence object using the status string
+ * @param status status string, not null
+ */
public Presence(String status) {
type_ = Type.Available;
setStatus(status);
}
+ /**
+ * Get the Presence Stanza type
+ * @return presence type, not null
+ */
public Type getType() {
return type_;
}
+ /**
+ * Set the Presence stanza type
+ * @param type stanza type, not null
+ */
public void setType(Type type) {
type_ = type;
}
@@ -42,10 +75,18 @@ public class Presence extends Stanza {
return type_ == Type.Available ? StatusShow.Type.Online : StatusShow.Type.None;
}
+ /**
+ * Set the show status type
+ * @param show status type, not null
+ */
public void setShow(StatusShow.Type show) {
updatePayload(new StatusShow(show));
}
+ /**
+ * Get the status message
+ * @return status message, not null but can be empty
+ */
public String getStatus() {
Status status = getPayload(new Status());
if (status != null) {
@@ -54,15 +95,27 @@ public class Presence extends Stanza {
return "";
}
+ /**
+ * Set the status string
+ * @param status status string, not null
+ */
public void setStatus(String status) {
updatePayload(new Status(status));
}
+ /**
+ * Get the priority of presence stanza
+ * @return priority of presence stanza
+ */
public int getPriority() {
Priority priority = getPayload(new Priority());
return (priority != null ? priority.getPriority() : 0);
}
+ /**
+ * Set the priority of presence message.
+ * @param priority priority value (allowed values -127 to 128)
+ */
public void setPriority(int priority) {
updatePayload(new Priority(priority));
}
diff --git a/src/com/isode/stroke/elements/Stanza.java b/src/com/isode/stroke/elements/Stanza.java
index 036ec8b..c542e8b 100644
--- a/src/com/isode/stroke/elements/Stanza.java
+++ b/src/com/isode/stroke/elements/Stanza.java
@@ -21,7 +21,33 @@ public abstract class Stanza implements Element {
private JID to_;
private Vector<Payload> payloads_ = new Vector<Payload>();
+ /**
+ * Create a stanza object
+ */
+ public Stanza() {
+ }
+
+ /**
+ * Create a stanza object copy from another
+ * @param other object to be copied, not null
+ */
+ public Stanza(Stanza other) {
+ this.id_ = other.id_;
+ if(other.from_ != null) {
+ this.from_ = JID.fromString(other.from_.toString());
+ }
+ if(other.to_!= null) {
+ this.to_ = JID.fromString(other.to_.toString());
+ }
+ payloads_ = new Vector<Payload>(other.payloads_);
+ }
+ /**
+ * Get the payload of the given type from the stanza
+ * @param <T> payload type
+ * @param type payload type object instance, not null
+ * @return payload of given type, can be null
+ */
public <T extends Payload> T getPayload(T type) {
for (Payload payload : payloads_) {
if (payload.getClass().isAssignableFrom(type.getClass())) {
@@ -31,6 +57,12 @@ public abstract class Stanza implements Element {
return null;
}
+ /**
+ * Get the payloads of the given type from the stanza
+ * @param <T> payload type
+ * @param type payload type object instance, not null
+ * @return list of payloads of given type, not null but can be empty
+ */
public <T extends Payload> Vector<T> getPayloads(T type) {
Vector<T> results = new Vector<T>();
for (Payload payload : payloads_) {
@@ -41,14 +73,27 @@ public abstract class Stanza implements Element {
return results;
}
+ /**
+ * Get the list of payloads from this stanza
+ * @return list of payloads, not null but can be empty
+ */
public Vector<Payload> getPayloads() {
return payloads_;
}
+ /**
+ * Add a payload to the stanza
+ * @param payload payload to be added, not null
+ */
public void addPayload(Payload payload) {
payloads_.add(payload);
}
+ /**
+ * Update payload to the staza object. It will replace the payload of
+ * given type if it exists or add it the list if it does not exist
+ * @param payload payload to be updated, not null
+ */
public void updatePayload(Payload payload) {
for (int i = 0; i < payloads_.size(); i++) {
if (payloads_.get(i).getClass() == payload.getClass()) {
@@ -59,26 +104,51 @@ public abstract class Stanza implements Element {
payloads_.add(payload);
}
+ /**
+ * Get the jabber ID of the sender
+ * @return jabber id, can be null
+ */
public JID getFrom() {
return from_;
}
+ /**
+ * Set the jabber ID of the sender
+ * @param from jabber id, can be null
+ */
public void setFrom(JID from) {
from_ = from;
}
+ /**
+ * Get the jabber ID of the recipient user of the stanza
+ * @return jabber id, can be null
+ */
public JID getTo() {
return to_;
}
+ /**
+ * Set the jabber ID of the recipient user of the stanza
+ * @param to jabber id, can be null
+ */
public void setTo(JID to) {
to_ = to;
}
+ /**
+ * Get the identification string of the stanza
+ * @return ID string, can be null if its not an IQ stanza
+ */
public String getID() {
return id_;
}
+ /**
+ * Set the identification string of the stanza
+ * @param id ID string, not null for IQ stanza but can be null for
+ * Message or Presence stanza
+ */
public void setID(String id) {
id_ = id;
}
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);
+ }
}