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,8 +1,8 @@ /* - * Copyright (c) 2010-2015, Isode Limited, London, England. + * Copyright (c) 2010-2016, Isode Limited, London, England. * All rights reserved. */ package com.isode.stroke.elements; public class Message extends Stanza { @@ -28,20 +28,26 @@ public class Message extends Stanza { public void setSubject(String subject) { updatePayload(new Subject(subject)); } 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() { ErrorPayload error = getPayload(new ErrorPayload()); return getType().equals(Type.Error) || error != null; } 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,16 +1,17 @@ /* - * Copyright (c) 2010-2015, Isode Limited, London, England. + * Copyright (c) 2010-2016, Isode Limited, London, England. * All rights reserved. */ package com.isode.stroke.elements; import com.isode.stroke.jid.JID; import java.util.Date; +import java.util.Iterator; import java.util.Vector; /** * Base class for all types of XMPP stanza. */ public abstract class Stanza implements Element { @@ -36,23 +37,52 @@ public abstract class Stanza implements Element { } if(other.to_!= null) { this.to_ = JID.fromString(other.to_.toString()); } 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 * @param <T> payload type * @param type payload type object instance, not null * @return payload of given type, can be null */ @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; } } return null; } |
Swift