From 0f2dce2a50b5868f17ccd35e6c5da74da7e4f808 Mon Sep 17 00:00:00 2001 From: Anatoliy Sablin Date: Wed, 18 Oct 2017 23:52:10 +0300 Subject: Simplified the code. --- .../src/main/java/com/juick/service/TagService.java | 3 ++- .../main/java/com/juick/service/TagServiceImpl.java | 21 +++++---------------- juick-www/src/main/java/com/juick/www/WebApp.java | 2 +- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/juick-server-core/src/main/java/com/juick/service/TagService.java b/juick-server-core/src/main/java/com/juick/service/TagService.java index e24cf928..78e8f563 100644 --- a/juick-server-core/src/main/java/com/juick/service/TagService.java +++ b/juick-server-core/src/main/java/com/juick/service/TagService.java @@ -23,6 +23,7 @@ import com.juick.server.helpers.TagStats; import java.util.Collection; import java.util.List; +import java.util.stream.Stream; /** * Created by aalexeev on 11/13/16. @@ -32,7 +33,7 @@ public interface TagService { com.juick.Tag getTag(String tag, boolean autoCreate); - List getTags(String[] tags, boolean autoCreate); + List getTags(Stream tags, boolean autoCreate); boolean getTagNoIndex(int tagId); diff --git a/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java index 79163d1c..9b89bca3 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java @@ -41,9 +41,11 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Created by aalexeev on 11/13/16. @@ -108,22 +110,9 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { } @Override - public List getTags(final String[] tags, final boolean autoCreate) { - if (ArrayUtils.isEmpty(tags)) - return Collections.emptyList(); - - List ret = new ArrayList<>(); - - for (String tag : tags) { - if (!tag.isEmpty()) { - Tag t = getTag(tag, autoCreate); - - if (t != null) - ret.add(t); - } - } - - return ret.stream().distinct().collect(Collectors.toList()); + public List getTags(Stream tags, final boolean autoCreate) { + return tags.filter(StringUtils::isNotBlank).map(tag -> getTag(tag, autoCreate)).filter(Objects::nonNull).distinct() + .collect(Collectors.toList()); } @Transactional(readOnly = true) diff --git a/juick-www/src/main/java/com/juick/www/WebApp.java b/juick-www/src/main/java/com/juick/www/WebApp.java index a0819ad6..4c706426 100644 --- a/juick-www/src/main/java/com/juick/www/WebApp.java +++ b/juick-www/src/main/java/com/juick/www/WebApp.java @@ -125,7 +125,7 @@ public class WebApp implements DisposableBean { } return t; }); - tags = tagService.getTags(tagsList.toArray(String[]::new), true); + tags = tagService.getTags(tagsList, true); while (tags.size() > 5) { tags.remove(5); } -- cgit v1.2.3