From f3963f0b12055ba6710b9b28ddc93a5b5fe91d67 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 22 Sep 2017 16:39:00 +0300 Subject: Initial getNotifications query * using Java 8 DateTime API where possible --- juick-core/src/main/java/com/juick/Message.java | 16 ++++++++++------ .../java/com/juick/formatters/PlainTextFormatter.java | 3 ++- .../src/main/java/com/juick/util/DateFormatter.java | 15 ++++++++------- .../java/com/juick/xml/adapters/SimpleDateAdapter.java | 8 ++++---- juick-core/src/test/java/com/juick/FormatterTest.java | 17 +++++++++-------- .../src/test/java/com/juick/test/util/MockUtils.java | 4 ++-- 6 files changed, 35 insertions(+), 28 deletions(-) (limited to 'juick-core/src') diff --git a/juick-core/src/main/java/com/juick/Message.java b/juick-core/src/main/java/com/juick/Message.java index 72f60c82..25463c46 100644 --- a/juick-core/src/main/java/com/juick/Message.java +++ b/juick-core/src/main/java/com/juick/Message.java @@ -26,7 +26,11 @@ import org.apache.commons.lang3.builder.ToStringBuilder; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; -import java.util.*; +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; /** * @author Ugnich Anton @@ -40,7 +44,7 @@ public class Message implements Comparable { private String text = null; private User user = null; private final List tags; - private Date date; + private Instant ts; @XmlTransient @JsonIgnore public int TimeAgo = 0; @@ -239,12 +243,12 @@ public class Message implements Comparable { @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "UTC") @XmlAttribute(name = "ts") @XmlJavaTypeAdapter(SimpleDateAdapter.class) - public Date getDate() { - return date; + public Instant getTimestamp() { + return ts; } - public void setDate(Date date) { - this.date = date; + public void setTimestamp(Instant timestamp) { + this.ts = timestamp; } public com.juick.PM getPM() { diff --git a/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java index 8f9378a8..c62e0134 100644 --- a/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java +++ b/juick-core/src/main/java/com/juick/formatters/PlainTextFormatter.java @@ -22,6 +22,7 @@ import com.juick.util.MessageUtils; import org.apache.commons.lang3.StringUtils; import org.ocpsoft.prettytime.PrettyTime; +import java.util.Date; import java.util.Locale; /** @@ -45,7 +46,7 @@ public class PlainTextFormatter { public static String formatPostSummary(Message m) { int cropLength = 384; - String timeAgo = pt.format(m.getDate()); + String timeAgo = pt.format(Date.from(m.getTimestamp())); String repliesCount = m.getReplies() == 1 ? "; 1 reply" : m.getReplies() == 0 ? "" : String.format("; %d replies", m.getReplies()); String txt = m.getText().length() >= cropLength ? diff --git a/juick-core/src/main/java/com/juick/util/DateFormatter.java b/juick-core/src/main/java/com/juick/util/DateFormatter.java index d7cf16bf..f9e23a91 100644 --- a/juick-core/src/main/java/com/juick/util/DateFormatter.java +++ b/juick-core/src/main/java/com/juick/util/DateFormatter.java @@ -19,10 +19,11 @@ package com.juick.util; import org.apache.commons.lang3.StringUtils; +import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; -import java.util.Date; +import java.util.Locale; /** * Created by aalexeev on 12/7/16. @@ -31,25 +32,25 @@ public class DateFormatter { private final DateTimeFormatter formatter; public DateFormatter(String pattern) { - formatter = DateTimeFormatter.ofPattern(pattern); + formatter = DateTimeFormatter.ofPattern(pattern, Locale.ENGLISH); } - public String format(final Date date) { - if (date == null) + public String format(final Instant ts) { + if (ts == null) return null; - LocalDateTime ldt = LocalDateTime.ofInstant(date.toInstant(), ZoneOffset.UTC); + LocalDateTime ldt = LocalDateTime.ofInstant(ts, ZoneOffset.UTC); return ldt.format(formatter); } - public Date parse(final String v) { + public Instant parse(final String v) { if (StringUtils.isBlank(v)) return null; LocalDateTime ldt = LocalDateTime.parse(v, formatter); - return Date.from(ldt.toInstant(ZoneOffset.UTC)); + return ldt.toInstant(ZoneOffset.UTC); } } diff --git a/juick-core/src/main/java/com/juick/xml/adapters/SimpleDateAdapter.java b/juick-core/src/main/java/com/juick/xml/adapters/SimpleDateAdapter.java index 9a6061cf..6dd40582 100644 --- a/juick-core/src/main/java/com/juick/xml/adapters/SimpleDateAdapter.java +++ b/juick-core/src/main/java/com/juick/xml/adapters/SimpleDateAdapter.java @@ -20,21 +20,21 @@ package com.juick.xml.adapters; import com.juick.util.DateFormattersHolder; import javax.xml.bind.annotation.adapters.XmlAdapter; -import java.util.Date; +import java.time.Instant; /** * Created by vitalyster on 15.11.2016. */ -public class SimpleDateAdapter extends XmlAdapter { +public class SimpleDateAdapter extends XmlAdapter { @Override - public String marshal(Date v) throws Exception { + public String marshal(Instant v) throws Exception { return DateFormattersHolder.getMessageFormatterInstance().format(v); } @Override - public Date unmarshal(String v) throws Exception { + public Instant unmarshal(String v) throws Exception { return DateFormattersHolder.getMessageFormatterInstance().parse(v); } } diff --git a/juick-core/src/test/java/com/juick/FormatterTest.java b/juick-core/src/test/java/com/juick/FormatterTest.java index d9324b8e..397c4b6c 100644 --- a/juick-core/src/test/java/com/juick/FormatterTest.java +++ b/juick-core/src/test/java/com/juick/FormatterTest.java @@ -21,6 +21,7 @@ import com.juick.util.DateFormattersHolder; import org.apache.commons.lang3.RandomUtils; import org.junit.Test; +import java.time.Instant; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -36,15 +37,15 @@ public class FormatterTest { @Test public void forAnyDateFormatterShouldReturnNotEmptyString() throws Exception { - Date date = new Date(); + Instant ts = Instant.now(); - assertThat(DateFormattersHolder.getMessageFormatterInstance().format(date), not(isEmptyOrNullString())); - assertThat(DateFormattersHolder.getRssFormatterInstance().format(date), not(isEmptyOrNullString())); + assertThat(DateFormattersHolder.getMessageFormatterInstance().format(ts), not(isEmptyOrNullString())); + assertThat(DateFormattersHolder.getRssFormatterInstance().format(ts), not(isEmptyOrNullString())); - date = new Date(RandomUtils.nextLong(1, Long.MAX_VALUE / 10000)); + ts = Instant.ofEpochMilli(RandomUtils.nextLong(1, Long.MAX_VALUE / 10000)); - assertThat(DateFormattersHolder.getMessageFormatterInstance().format(date), not(isEmptyOrNullString())); - assertThat(DateFormattersHolder.getRssFormatterInstance().format(date), not(isEmptyOrNullString())); + assertThat(DateFormattersHolder.getMessageFormatterInstance().format(ts), not(isEmptyOrNullString())); + assertThat(DateFormattersHolder.getRssFormatterInstance().format(ts), not(isEmptyOrNullString())); } @Test @@ -56,7 +57,7 @@ public class FormatterTest { Date date = calendar.getTime(); - assertThat(DateFormattersHolder.getMessageFormatterInstance().format(date), equalTo("2012-01-01 00:00:00")); - assertThat(DateFormattersHolder.getRssFormatterInstance().format(date), equalTo("Sun, 1 Jan 2012 00:00:00")); + assertThat(DateFormattersHolder.getMessageFormatterInstance().format(date.toInstant()), equalTo("2012-01-01 00:00:00")); + assertThat(DateFormattersHolder.getRssFormatterInstance().format(date.toInstant()), equalTo("Sun, 1 Jan 2012 00:00:00")); } } diff --git a/juick-core/src/test/java/com/juick/test/util/MockUtils.java b/juick-core/src/test/java/com/juick/test/util/MockUtils.java index 8426cde8..017af4d1 100644 --- a/juick-core/src/test/java/com/juick/test/util/MockUtils.java +++ b/juick-core/src/test/java/com/juick/test/util/MockUtils.java @@ -21,7 +21,7 @@ import com.juick.Message; import com.juick.User; import org.apache.commons.text.RandomStringGenerator; -import java.util.Date; +import java.time.Instant; /** * Created by vitalyster on 12.01.2017. @@ -34,7 +34,7 @@ public class MockUtils { msg.setMid(mid); msg.setUser(user); msg.setText(messageText == null ? generator.generate(24) : messageText); - msg.setDate(new Date()); + msg.setTimestamp(Instant.now()); return msg; } -- cgit v1.2.3