Deduplicate code ChatMarkersElements

by introducing ChatMarkerExtensionWithId.
This commit is contained in:
Florian Schmaus 2018-09-06 19:04:02 +02:00
parent 36bfa060a5
commit 0778485243
1 changed files with 37 additions and 69 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -76,24 +76,10 @@ public class ChatMarkersElements {
} }
} }
/** protected abstract static class ChatMarkerExtensionWithId implements ExtensionElement {
* Received extension class. protected final String id;
*
* @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat
* Markers</a>
* @author Fernando Ramirez
*
*/
public static class ReceivedExtension implements ExtensionElement {
/** protected ChatMarkerExtensionWithId(String id) {
* received element.
*/
public static final String ELEMENT = ChatMarkersState.received.toString();
private final String id;
public ReceivedExtension(String id) {
this.id = StringUtils.requireNotNullNorEmpty(id, "Message ID must not be null"); this.id = StringUtils.requireNotNullNorEmpty(id, "Message ID must not be null");
} }
@ -102,10 +88,38 @@ public class ChatMarkersElements {
* *
* @return the id * @return the id
*/ */
public String getId() { public final String getId() {
return id; 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 <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat
* Markers</a>
* @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 @Override
public String getElementName() { public String getElementName() {
return ELEMENT; return ELEMENT;
@ -116,14 +130,6 @@ public class ChatMarkersElements {
return NAMESPACE; 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) { public static ReceivedExtension from(Message message) {
return (ReceivedExtension) message.getExtension(ELEMENT, NAMESPACE); return (ReceivedExtension) message.getExtension(ELEMENT, NAMESPACE);
} }
@ -137,26 +143,15 @@ public class ChatMarkersElements {
* @author Fernando Ramirez * @author Fernando Ramirez
* *
*/ */
public static class DisplayedExtension implements ExtensionElement { public static class DisplayedExtension extends ChatMarkerExtensionWithId {
/** /**
* displayed element. * displayed element.
*/ */
public static final String ELEMENT = ChatMarkersState.displayed.toString(); public static final String ELEMENT = ChatMarkersState.displayed.toString();
private final String id;
public DisplayedExtension(String id) { public DisplayedExtension(String id) {
this.id = StringUtils.requireNotNullNorEmpty(id, "Message ID must not be null"); super(id);
}
/**
* Get the id.
*
* @return the id
*/
public String getId() {
return id;
} }
@Override @Override
@ -169,14 +164,6 @@ public class ChatMarkersElements {
return NAMESPACE; 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) { public static DisplayedExtension from(Message message) {
return (DisplayedExtension) message.getExtension(ELEMENT, NAMESPACE); return (DisplayedExtension) message.getExtension(ELEMENT, NAMESPACE);
} }
@ -190,26 +177,15 @@ public class ChatMarkersElements {
* @author Fernando Ramirez * @author Fernando Ramirez
* *
*/ */
public static class AcknowledgedExtension implements ExtensionElement { public static class AcknowledgedExtension extends ChatMarkerExtensionWithId {
/** /**
* acknowledged element. * acknowledged element.
*/ */
public static final String ELEMENT = ChatMarkersState.acknowledged.toString(); public static final String ELEMENT = ChatMarkersState.acknowledged.toString();
private final String id;
public AcknowledgedExtension(String id) { public AcknowledgedExtension(String id) {
this.id = StringUtils.requireNotNullNorEmpty(id, "Message id must not be null"); super(id);
}
/**
* Get the id.
*
* @return the id
*/
public String getId() {
return id;
} }
@Override @Override
@ -222,14 +198,6 @@ public class ChatMarkersElements {
return NAMESPACE; 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) { public static AcknowledgedExtension from(Message message) {
return (AcknowledgedExtension) message.getExtension(ELEMENT, NAMESPACE); return (AcknowledgedExtension) message.getExtension(ELEMENT, NAMESPACE);
} }