From bae6e6298a617b801a15cbdba13d6759d8b44f31 Mon Sep 17 00:00:00 2001 From: Gurmeen Bindra Date: Mon, 16 Apr 2012 12:08:41 +0100 Subject: 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 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 payloads_ = new Vector(); + /** + * 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(other.payloads_); + } + /** + * Get the payload of the given type from the stanza + * @param payload type + * @param type payload type object instance, not null + * @return payload of given type, can be null + */ public 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 payload type + * @param type payload type object instance, not null + * @return list of payloads of given type, not null but can be empty + */ public Vector getPayloads(T type) { Vector results = new Vector(); 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 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; *

* This is an immutable class. */ -public class JID { +public class JID implements Comparable { 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); + } } -- cgit v0.10.2-6-g49f6