summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Clayton <alex.clayton@isode.com>2016-03-16 09:45:03 (GMT)
committerAlex Clayton <alex.clayton@isode.com>2016-03-16 10:34:33 (GMT)
commita452bc6ceed519cba43328f0d741af9723197a24 (patch)
tree753422c1282fc2fe9b93a3416829a4a85b532e45
parent1a0b79956729aefe3a10093c58b508ac651e4d0c (diff)
downloadstroke-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.java14
-rw-r--r--src/com/isode/stroke/elements/Stanza.java34
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 @@
1/* 1/*
2 * Copyright (c) 2010-2015, Isode Limited, London, England. 2 * Copyright (c) 2010-2016, Isode Limited, London, England.
3 * All rights reserved. 3 * All rights reserved.
4 */ 4 */
5package com.isode.stroke.elements; 5package com.isode.stroke.elements;
@@ -31,14 +31,20 @@ public class Message extends Stanza {
31 31
32 public String getBody() { 32 public String getBody() {
33 Body body = getPayload(new Body()); 33 Body body = getPayload(new Body());
34 String bodyData = null;
34 if (body != null) { 35 if (body != null) {
35 return body.getText(); 36 bodyData = body.getText();
36 } 37 }
37 return ""; 38 return bodyData;
38 } 39 }
39 40
40 public void setBody(String body) { 41 public void setBody(String body) {
41 updatePayload(new Body(body)); 42 if (body != null) {
43 updatePayload(new Body(body));
44 }
45 else {
46 removePayload(new Body());
47 }
42 } 48 }
43 49
44 public boolean isError() { 50 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 @@
1/* 1/*
2 * Copyright (c) 2010-2015, Isode Limited, London, England. 2 * Copyright (c) 2010-2016, Isode Limited, London, England.
3 * All rights reserved. 3 * All rights reserved.
4 */ 4 */
5 5
@@ -8,6 +8,7 @@ package com.isode.stroke.elements;
8import com.isode.stroke.jid.JID; 8import com.isode.stroke.jid.JID;
9 9
10import java.util.Date; 10import java.util.Date;
11import java.util.Iterator;
11import java.util.Vector; 12import java.util.Vector;
12 13
13/** 14/**
@@ -39,6 +40,35 @@ public abstract class Stanza implements Element {
39 } 40 }
40 payloads_ = new Vector<Payload>(other.payloads_); 41 payloads_ = new Vector<Payload>(other.payloads_);
41 } 42 }
43
44 /**
45 * Indicates if a given Payload is of a given type
46 * @param <T> A type of payload
47 * @param <P> A Payload
48 * @param type An instance of the type of payload to check for
49 * @param payload the payload to check the type of
50 * @return {@code true} if the Payload is of the same type,
51 * {@code false} otherwise.
52 */
53 private static <T extends Payload,P extends Payload> boolean isPayloadOfType(T type,P payload) {
54 return payload.getClass().isAssignableFrom(type.getClass());
55 }
56
57 /**
58 * Removes all the payloads of the given type from the stanza
59 * @param <T> The payload type
60 * @param type Object of the payload type to remove, should
61 * not be {@code null}
62 */
63 public <T extends Payload> void removePayload(T type) {
64 Iterator<Payload> payloadIterator = payloads_.iterator();
65 while (payloadIterator.hasNext()) {
66 Payload payload = payloadIterator.next();
67 if (isPayloadOfType(type, payload)) {
68 payloadIterator.remove();
69 }
70 }
71 }
42 72
43 /** 73 /**
44 * Get the payload of the given type from the stanza 74 * Get the payload of the given type from the stanza
@@ -49,7 +79,7 @@ public abstract class Stanza implements Element {
49 @SuppressWarnings("unchecked") 79 @SuppressWarnings("unchecked")
50 public <T extends Payload> T getPayload(T type) { 80 public <T extends Payload> T getPayload(T type) {
51 for (Payload payload : payloads_) { 81 for (Payload payload : payloads_) {
52 if (payload.getClass().isAssignableFrom(type.getClass())) { 82 if (isPayloadOfType(type, payload)) {
53 return (T)payload; 83 return (T)payload;
54 } 84 }
55 } 85 }