diff options
-rw-r--r-- | src/com/isode/stroke/elements/Message.java | 14 | ||||
-rw-r--r-- | src/com/isode/stroke/elements/Stanza.java | 34 |
2 files changed, 42 insertions, 6 deletions
diff --git a/src/com/isode/stroke/elements/Message.java b/src/com/isode/stroke/elements/Message.java index 1bb601a..a0c8daa 100644 --- a/src/com/isode/stroke/elements/Message.java +++ b/src/com/isode/stroke/elements/Message.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2015, Isode Limited, London, England. + * Copyright (c) 2010-2016, Isode Limited, London, England. * All rights reserved. */ package com.isode.stroke.elements; @@ -31,14 +31,20 @@ public class Message extends Stanza { public String getBody() { Body body = getPayload(new Body()); + String bodyData = null; if (body != null) { - return body.getText(); + bodyData = body.getText(); } - return ""; + return bodyData; } public void setBody(String body) { - updatePayload(new Body(body)); + if (body != null) { + updatePayload(new Body(body)); + } + else { + removePayload(new Body()); + } } public boolean isError() { diff --git a/src/com/isode/stroke/elements/Stanza.java b/src/com/isode/stroke/elements/Stanza.java index 11ba1b4..9f03168 100644 --- a/src/com/isode/stroke/elements/Stanza.java +++ b/src/com/isode/stroke/elements/Stanza.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2015, Isode Limited, London, England. + * Copyright (c) 2010-2016, Isode Limited, London, England. * All rights reserved. */ @@ -8,6 +8,7 @@ package com.isode.stroke.elements; import com.isode.stroke.jid.JID; import java.util.Date; +import java.util.Iterator; import java.util.Vector; /** @@ -39,6 +40,35 @@ public abstract class Stanza implements Element { } payloads_ = new Vector<Payload>(other.payloads_); } + + /** + * Indicates if a given Payload is of a given type + * @param <T> A type of payload + * @param <P> A Payload + * @param type An instance of the type of payload to check for + * @param payload the payload to check the type of + * @return {@code true} if the Payload is of the same type, + * {@code false} otherwise. + */ + private static <T extends Payload,P extends Payload> boolean isPayloadOfType(T type,P payload) { + return payload.getClass().isAssignableFrom(type.getClass()); + } + + /** + * Removes all the payloads of the given type from the stanza + * @param <T> The payload type + * @param type Object of the payload type to remove, should + * not be {@code null} + */ + public <T extends Payload> void removePayload(T type) { + Iterator<Payload> payloadIterator = payloads_.iterator(); + while (payloadIterator.hasNext()) { + Payload payload = payloadIterator.next(); + if (isPayloadOfType(type, payload)) { + payloadIterator.remove(); + } + } + } /** * Get the payload of the given type from the stanza @@ -49,7 +79,7 @@ public abstract class Stanza implements Element { @SuppressWarnings("unchecked") public <T extends Payload> T getPayload(T type) { for (Payload payload : payloads_) { - if (payload.getClass().isAssignableFrom(type.getClass())) { + if (isPayloadOfType(type, payload)) { return (T)payload; } } |