diff options
author | Alex Clayton <alex.clayton@isode.com> | 2016-03-16 09:45:03 (GMT) |
---|---|---|
committer | Alex Clayton <alex.clayton@isode.com> | 2016-03-16 10:34:33 (GMT) |
commit | a452bc6ceed519cba43328f0d741af9723197a24 (patch) | |
tree | 753422c1282fc2fe9b93a3416829a4a85b532e45 | |
parent | 1a0b79956729aefe3a10093c58b508ac651e4d0c (diff) | |
download | stroke-a452bc6ceed519cba43328f0d741af9723197a24.zip stroke-a452bc6ceed519cba43328f0d741af9723197a24.tar.bz2 |
Allow null values for Message body
As per patch swiften patch 'Change stanza body to
boost::optional<std::string> type'
(1b9ccc1fef6104eaf951153ddccdc6bb15899e9a) allow null values for body
of a Message stanza for the case when it has no body.
Test-information: Unit tests still pass.
Change-Id: I487ecb14e4d915b7a903863d4378b8e2337d3b30
-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; } } |