From bad2718b1b85d806221fce52c76fa1d388993396 Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Wed, 7 Dec 2016 18:53:37 +0700 Subject: singleton java 8 date formatters --- .../com/juick/xmpp/extensions/JuickMessage.java | 132 ++++++++++----------- 1 file changed, 61 insertions(+), 71 deletions(-) (limited to 'juick-xmpp/src/main') 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 += "" + XmlUtils.escape(getText()) + ""; - } - for (com.juick.Tag Tag : getTags()) { - ret += "" + XmlUtils.escape(Tag.getName()) + ""; - } - ret += ""; + 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("").append(XmlUtils.escape(getText())).append(""); + + for (com.juick.Tag Tag : getTags()) + builder.append("").append(XmlUtils.escape(Tag.getName())).append(""); + + builder.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; } } -- cgit v1.2.3