From aabad2347aaf52e225541cb954e98ab509e9aa2f Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 1 Mar 2019 19:17:40 +0300 Subject: Tags refactoring --- src/main/java/com/juick/Message.java | 15 ++++++----- .../com/juick/service/MessagesServiceImpl.java | 2 +- src/main/java/com/juick/util/MessageUtils.java | 30 ++++++++++------------ 3 files changed, 23 insertions(+), 24 deletions(-) (limited to 'src/main/java/com/juick') diff --git a/src/main/java/com/juick/Message.java b/src/main/java/com/juick/Message.java index 617ed834..fb6be67c 100644 --- a/src/main/java/com/juick/Message.java +++ b/src/main/java/com/juick/Message.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2017, Juick + * Copyright (C) 2008-2019, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -24,12 +24,13 @@ import com.juick.model.Entity; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.builder.ToStringBuilder; +import javax.annotation.Nonnull; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import java.net.URI; import java.time.Instant; -import java.util.ArrayList; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Objects; import java.util.Set; @@ -45,7 +46,7 @@ public class Message implements Comparable { private int replyto = 0; private String text = null; private User user = null; - private final List tags; + private final Set tags; private Instant ts; private Instant updated; private Instant updatedAt; @@ -86,7 +87,7 @@ public class Message implements Comparable { private List entities; public Message() { - tags = new ArrayList<>(); + tags = new LinkedHashSet<>(); reactions = new HashSet<>(); } @@ -120,7 +121,7 @@ public class Message implements Comparable { } @Override - public int compareTo(Object obj) throws ClassCastException { + public int compareTo(@Nonnull Object obj) throws ClassCastException { if (obj == this) return 0; @@ -218,11 +219,11 @@ public class Message implements Comparable { @JsonProperty("tags") @XmlElement(name = "tag") - public List getTags() { + public Set getTags() { return tags; } - public void setTags(List tags) { + public void setTags(Set tags) { this.tags.clear(); if (CollectionUtils.isNotEmpty(tags)) this.tags.addAll(tags); diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index 2a107fbf..e95742ab 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -98,7 +98,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ msg.setAttachmentType(rs.getString(11)); msg.setLikes(rs.getInt(12)); msg.Hidden = rs.getBoolean(13); - String tagsStr = rs.getString(14); + String tagsStr = StringUtils.defaultString(rs.getString(14)); msg.setTags(MessageUtils.parseTags(tagsStr)); msg.setRepliesBy(rs.getString(15)); msg.setText(rs.getString(16)); diff --git a/src/main/java/com/juick/util/MessageUtils.java b/src/main/java/com/juick/util/MessageUtils.java index 78899ca2..fa94e978 100644 --- a/src/main/java/com/juick/util/MessageUtils.java +++ b/src/main/java/com/juick/util/MessageUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2017, Juick + * Copyright (C) 2008-2019, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -28,7 +28,13 @@ import org.springframework.web.util.UriComponentsBuilder; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URLEncoder; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -299,23 +305,15 @@ public class MessageUtils { public static boolean replyStartsWithQuote(Message msg) { return msg.getRid() > 0 && StringUtils.defaultString(msg.getText()).startsWith(">"); } - public static List parseTags(String strTags) { - List tags = new ArrayList<>(); - if (StringUtils.isNotEmpty(strTags)) { - Set tagSet = new TreeSet<>(tags); - for (String str : strTags.split(" ")) { - Tag tag = new Tag(str); - if (!tagSet.contains(tag)) { - tags.add(tag); - tagSet.add(tag); - } - } - } - return tags; + public static Set parseTags(String strTags) { + return StringUtils.isEmpty(strTags) ? Collections.emptySet() + : Arrays.stream(strTags.split(" ")).map(Tag::new) + .collect(Collectors.toCollection(LinkedHashSet::new)); } public static String getTagsString(Message msg) { StringBuilder builder = new StringBuilder(); - List tags = msg.getTags(); + Set tags = msg.getTags(); + if (!tags.isEmpty()) { for (Tag Tag : tags) builder.append(" *").append(Tag.getName()); -- cgit v1.2.3