diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java index 22b25a810..f08c3c653 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java @@ -50,7 +50,7 @@ import org.jivesoftware.smack.util.XmlStringBuilder; * * @author Matt Tucker */ -public final class Message extends Stanza { +public final class Message extends Stanza implements Cloneable { public static final String ELEMENT = "message"; public static final String BODY = "body"; @@ -98,6 +98,23 @@ public final class Message extends Stanza { setBody(body); } + /** + * Copy constructor. + *

+ * This does not perform a deep clone, as extension elements are shared between the new and old + * instance. + *

+ * + * @param other + */ + public Message(Message other) { + super(other); + this.type = other.type; + this.thread = other.thread; + this.subjects.addAll(other.subjects); + this.bodies.addAll(other.bodies); + } + /** * Returns the type of the message. If no type has been set this method will return {@link * org.jivesoftware.smack.packet.Message.Type#normal}. @@ -445,6 +462,19 @@ public final class Message extends Stanza { return buf; } + /** + * Creates and returns a copy of this message stanza. + *

+ * This does not perform a deep clone, as extension elements are shared between the new and old + * instance. + *

+ * @return a clone of this message. + */ + @Override + public Message clone() { + return new Message(this); + } + /** * Represents a message subject, its language and the content of the subject. */ diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java index 86fe029a3..7f347dd51 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java @@ -55,7 +55,7 @@ import org.jivesoftware.smack.util.XmlStringBuilder; * * @author Matt Tucker */ -public final class Presence extends Stanza { +public final class Presence extends Stanza implements Cloneable { public static final String ELEMENT = "presence"; @@ -88,6 +88,23 @@ public final class Presence extends Stanza { setMode(mode); } + /** + * Copy constructor. + *

+ * This does not perform a deep clone, as extension elements are shared between the new and old + * instance. + *

+ * + * @param other + */ + public Presence(Presence other) { + super(other); + this.type = other.type; + this.status = other.status; + this.priority = other.priority; + this.mode = other.mode; + } + /** * Returns true if the {@link Type presence type} is available (online) and * false if the user is unavailable (offline), or if this is a presence packet @@ -228,6 +245,19 @@ public final class Presence extends Stanza { return buf; } + /** + * Creates and returns a copy of this presence stanza. + *

+ * This does not perform a deep clone, as extension elements are shared between the new and old + * instance. + *

+ * @return a clone of this presence. + */ + @Override + public Presence clone() { + return new Presence(this); + } + /** * An enum to represent the presence type. Note that presence type is often confused * with presence mode. Generally, if a user is signed in to a server, they have a presence