aboutsummaryrefslogtreecommitdiff
path: root/juick-xmpp/src
diff options
context:
space:
mode:
authorGravatar Alexander Alexeev2016-12-07 18:53:37 +0700
committerGravatar Vitaly Takmazov2016-12-07 22:37:17 +0300
commitbad2718b1b85d806221fce52c76fa1d388993396 (patch)
tree8788b2b128012c3688fe9d56f295039c8d86f7b3 /juick-xmpp/src
parent3f0229dd0c0d07286604c77980ca578c1b3b8cbc (diff)
singleton java 8 date formatters
Diffstat (limited to 'juick-xmpp/src')
-rw-r--r--juick-xmpp/src/main/java/com/juick/xmpp/extensions/JuickMessage.java132
1 files changed, 61 insertions, 71 deletions
diff --git a/juick-xmpp/src/main/java/com/juick/xmpp/extensions/JuickMessage.java b/juick-xmpp/src/main/java/com/juick/xmpp/extensions/JuickMessage.java
index 18477b2a..b8a329db 100644
--- a/juick-xmpp/src/main/java/com/juick/xmpp/extensions/JuickMessage.java
+++ b/juick-xmpp/src/main/java/com/juick/xmpp/extensions/JuickMessage.java
@@ -18,35 +18,27 @@
package com.juick.xmpp.extensions;
import com.juick.Tag;
+import com.juick.util.DateFormattersHolder;
+import com.juick.xmpp.StanzaChild;
import com.juick.xmpp.utils.XmlUtils;
-import com.juick.xmpp.*;
-import java.io.IOException;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.TimeZone;
-
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
+import java.io.IOException;
+import java.text.ParseException;
+
/**
- *
* @author Ugnich Anton
*/
public class JuickMessage extends com.juick.Message implements StanzaChild {
-
public final static String XMLNS = "http://juick.com/message";
public final static String TagName = "juick";
- private SimpleDateFormat df;
public JuickMessage() {
- df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- df.setTimeZone(TimeZone.getTimeZone("UTC"));
}
public JuickMessage(com.juick.Message msg) {
super(msg);
- df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- df.setTimeZone(TimeZone.getTimeZone("UTC"));
}
@Override
@@ -84,7 +76,7 @@ public class JuickMessage extends com.juick.Message implements StanzaChild {
}
String ts = parser.getAttributeValue(null, "ts");
if (ts != null) {
- jmsg.setDate(df.parse(ts));
+ jmsg.setDate(DateFormattersHolder.getMessageFormatterInstance().parse(ts));
}
jmsg.setAttachmentType(parser.getAttributeValue(null, "attach"));
@@ -106,78 +98,76 @@ public class JuickMessage extends com.juick.Message implements StanzaChild {
@Override
public String toString() {
- String ret = "";
+ StringBuilder builder = new StringBuilder();
- ret = "<" + TagName + " xmlns=\"" + XMLNS + "\"";
- if (getMid() > 0) {
- ret += " mid=\"" + getMid() + "\"";
- }
- if (getRid() > 0) {
- ret += " rid=\"" + getRid() + "\"";
- }
- if (getReplyto() > 0) {
- ret += " replyto=\"" + getReplyto() + "\"";
- }
- ret += " privacy=\"" + getPrivacy() + "\"";
- if (FriendsOnly) {
- ret += " friendsonly=\"1\"";
- }
- if (ReadOnly) {
- ret += " readonly=\"1\"";
- }
- if (getDate() != null) {
- ret += " ts=\"" + df.format(getDate()) + "\"";
- }
- if (getAttachmentType() != null) {
- ret += " attach=\"" + getAttachmentType() + "\"";
- }
- ret += ">";
- if (getUser() != null) {
- ret += JuickUser.toString(getUser());
- }
- if (getText() != null) {
- ret += "<body>" + XmlUtils.escape(getText()) + "</body>";
- }
- for (com.juick.Tag Tag : getTags()) {
- ret += "<tag>" + XmlUtils.escape(Tag.getName()) + "</tag>";
- }
- ret += "</" + TagName + ">";
+ builder.append("<").append(TagName).append(" xmlns=\"").append(XMLNS).append("\"");
+
+ if (getMid() > 0)
+ builder.append(" mid=\"").append(getMid()).append("\"");
+
+ if (getRid() > 0)
+ builder.append(" rid=\"").append(getRid()).append("\"");
+
+ if (getReplyto() > 0)
+ builder.append(" replyto=\"").append(getReplyto()).append("\"");
+
+ builder.append(" privacy=\"").append(getPrivacy()).append("\"");
+
+ if (FriendsOnly)
+ builder.append(" friendsonly=\"1\"");
- return ret;
+ if (ReadOnly)
+ builder.append(" readonly=\"1\"");
+
+ if (getDate() != null)
+ builder.append(" ts=\"")
+ .append(DateFormattersHolder.getMessageFormatterInstance().format(getDate()))
+ .append("\"");
+
+ if (getAttachmentType() != null)
+ builder.append(" attach=\"").append(getAttachmentType()).append("\"");
+
+ builder.append(">");
+
+ if (getUser() != null)
+ builder.append(JuickUser.toString(getUser()));
+
+ if (getText() != null)
+ builder.append("<body>").append(XmlUtils.escape(getText())).append("</body>");
+
+ for (com.juick.Tag Tag : getTags())
+ builder.append("<tag>").append(XmlUtils.escape(Tag.getName())).append("</tag>");
+
+ builder.append("</").append(TagName).append(">");
+
+ return builder.toString();
}
@Override
public boolean equals(Object obj) {
- if (!(obj instanceof JuickMessage)) {
+ if (obj == this)
+ return true;
+
+ if (!(obj instanceof JuickMessage))
return false;
- }
+
JuickMessage jmsg = (JuickMessage) obj;
return (this.getMid() == jmsg.getMid() && this.getRid() == jmsg.getRid());
}
@Override
public int compareTo(Object obj) throws ClassCastException {
- if (!(obj instanceof JuickMessage)) {
- throw new ClassCastException();
- }
- JuickMessage jmsg = (JuickMessage) obj;
+ if (obj == this)
+ return 0;
- if (this.getMid() != jmsg.getMid()) {
- if (this.getMid() > jmsg.getMid()) {
- return -1;
- } else {
- return 1;
- }
- }
+ if (!(obj instanceof JuickMessage))
+ throw new ClassCastException();
- if (this.getRid() != jmsg.getRid()) {
- if (this.getRid() < jmsg.getRid()) {
- return -1;
- } else {
- return 1;
- }
- }
+ JuickMessage jmsg = (JuickMessage) obj;
+ int cmp = Integer.compare(jmsg.getMid(), getMid());
+ if (cmp == 0)
+ cmp = Integer.compare(getRid(), jmsg.getRid());
- return 0;
+ return cmp;
}
}