From 07784852433b94ba63c23534421ad77f8d6a37d9 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 6 Sep 2018 19:04:02 +0200 Subject: [PATCH] Deduplicate code ChatMarkersElements by introducing ChatMarkerExtensionWithId. --- .../element/ChatMarkersElements.java | 106 ++++++------------ 1 file changed, 37 insertions(+), 69 deletions(-) diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/element/ChatMarkersElements.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/element/ChatMarkersElements.java index 2c5593865..cc3529544 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/element/ChatMarkersElements.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/element/ChatMarkersElements.java @@ -1,6 +1,6 @@ /** * - * Copyright © 2016 Fernando Ramirez + * Copyright © 2016 Fernando Ramirez, 2018 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -76,24 +76,10 @@ public class ChatMarkersElements { } } - /** - * Received extension class. - * - * @see XEP-0333: Chat - * Markers - * @author Fernando Ramirez - * - */ - public static class ReceivedExtension implements ExtensionElement { + protected abstract static class ChatMarkerExtensionWithId implements ExtensionElement { + protected final String id; - /** - * received element. - */ - public static final String ELEMENT = ChatMarkersState.received.toString(); - - private final String id; - - public ReceivedExtension(String id) { + protected ChatMarkerExtensionWithId(String id) { this.id = StringUtils.requireNotNullNorEmpty(id, "Message ID must not be null"); } @@ -102,10 +88,38 @@ public class ChatMarkersElements { * * @return the id */ - public String getId() { + public final String getId() { return id; } + @Override + public final XmlStringBuilder toXML(String enclosingNamespace) { + XmlStringBuilder xml = new XmlStringBuilder(this); + xml.attribute("id", id); + xml.closeEmptyElement(); + return xml; + } + } + + /** + * Received extension class. + * + * @see XEP-0333: Chat + * Markers + * @author Fernando Ramirez + * + */ + public static class ReceivedExtension extends ChatMarkerExtensionWithId { + + /** + * received element. + */ + public static final String ELEMENT = ChatMarkersState.received.toString(); + + public ReceivedExtension(String id) { + super(id); + } + @Override public String getElementName() { return ELEMENT; @@ -116,14 +130,6 @@ public class ChatMarkersElements { return NAMESPACE; } - @Override - public CharSequence toXML(String enclosingNamespace) { - XmlStringBuilder xml = new XmlStringBuilder(this); - xml.attribute("id", id); - xml.closeEmptyElement(); - return xml; - } - public static ReceivedExtension from(Message message) { return (ReceivedExtension) message.getExtension(ELEMENT, NAMESPACE); } @@ -137,26 +143,15 @@ public class ChatMarkersElements { * @author Fernando Ramirez * */ - public static class DisplayedExtension implements ExtensionElement { + public static class DisplayedExtension extends ChatMarkerExtensionWithId { /** * displayed element. */ public static final String ELEMENT = ChatMarkersState.displayed.toString(); - private final String id; - public DisplayedExtension(String id) { - this.id = StringUtils.requireNotNullNorEmpty(id, "Message ID must not be null"); - } - - /** - * Get the id. - * - * @return the id - */ - public String getId() { - return id; + super(id); } @Override @@ -169,14 +164,6 @@ public class ChatMarkersElements { return NAMESPACE; } - @Override - public CharSequence toXML(String enclosingNamespace) { - XmlStringBuilder xml = new XmlStringBuilder(this); - xml.attribute("id", id); - xml.closeEmptyElement(); - return xml; - } - public static DisplayedExtension from(Message message) { return (DisplayedExtension) message.getExtension(ELEMENT, NAMESPACE); } @@ -190,26 +177,15 @@ public class ChatMarkersElements { * @author Fernando Ramirez * */ - public static class AcknowledgedExtension implements ExtensionElement { + public static class AcknowledgedExtension extends ChatMarkerExtensionWithId { /** * acknowledged element. */ public static final String ELEMENT = ChatMarkersState.acknowledged.toString(); - private final String id; - public AcknowledgedExtension(String id) { - this.id = StringUtils.requireNotNullNorEmpty(id, "Message id must not be null"); - } - - /** - * Get the id. - * - * @return the id - */ - public String getId() { - return id; + super(id); } @Override @@ -222,14 +198,6 @@ public class ChatMarkersElements { return NAMESPACE; } - @Override - public CharSequence toXML(String enclosingNamespace) { - XmlStringBuilder xml = new XmlStringBuilder(this); - xml.attribute("id", id); - xml.closeEmptyElement(); - return xml; - } - public static AcknowledgedExtension from(Message message) { return (AcknowledgedExtension) message.getExtension(ELEMENT, NAMESPACE); }