From a452bc6ceed519cba43328f0d741af9723197a24 Mon Sep 17 00:00:00 2001 From: Alex Clayton Date: Wed, 16 Mar 2016 09:45:03 +0000 Subject: Allow null values for Message body As per patch swiften patch 'Change stanza body to boost::optional 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 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(other.payloads_); } + + /** + * Indicates if a given Payload is of a given type + * @param A type of payload + * @param

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 boolean isPayloadOfType(T type,P payload) { + return payload.getClass().isAssignableFrom(type.getClass()); + } + + /** + * Removes all the payloads of the given type from the stanza + * @param The payload type + * @param type Object of the payload type to remove, should + * not be {@code null} + */ + public void removePayload(T type) { + Iterator 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 getPayload(T type) { for (Payload payload : payloads_) { - if (payload.getClass().isAssignableFrom(type.getClass())) { + if (isPayloadOfType(type, payload)) { return (T)payload; } } -- cgit v0.10.2-6-g49f6