From 104146c5ed53813a8b97e39c4a9c9980a57685da Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 17 Jul 2017 20:37:49 +0200 Subject: [PATCH] Add Jingle.getSoleContentOrThrow() --- .../smackx/jingle/element/Jingle.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/Jingle.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/Jingle.java index 3106563d8..3f7ac01c9 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/Jingle.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/Jingle.java @@ -135,6 +135,25 @@ public final class Jingle extends IQ { return contents; } + /** + * Get the only jingle content if one exists, or null. This method will throw an + * {@link IllegalStateException} if there is more than one jingle content. + * + * @return a JingleContent instance or null. + * @throws IllegalStateException if there is more than one jingle content. + */ + public JingleContent getSoleContentOrThrow() { + if (contents.isEmpty()) { + return null; + } + + if (contents.size() > 1) { + throw new IllegalStateException(); + } + + return contents.get(0); + } + @Override protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { xml.optAttribute(INITIATOR_ATTRIBUTE_NAME, getInitiator());