From 6595fdbaaa047cd2c7e31c64b7c4ff2ff69002cc Mon Sep 17 00:00:00 2001 From: ilia_isakhin Date: Sun, 20 Sep 2020 12:39:39 +0400 Subject: [PATCH] fixed test commit --- .../service/SimpleTextStatisticsAnalyzer.java | 45 ++++++++++++++----- .../StreamApiTextStatisticsAnalyzer.java | 42 ++++++++++++----- 2 files changed, 65 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/epam/izh/rd/online/service/SimpleTextStatisticsAnalyzer.java b/src/main/java/com/epam/izh/rd/online/service/SimpleTextStatisticsAnalyzer.java index 32f8e35..faf16b3 100644 --- a/src/main/java/com/epam/izh/rd/online/service/SimpleTextStatisticsAnalyzer.java +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleTextStatisticsAnalyzer.java @@ -4,8 +4,6 @@ import java.util.*; -import static java.util.Collections.*; - /** * Совет: * Начните с реализации метода {@link SimpleTextStatisticsAnalyzer#getWords(String)}. @@ -23,7 +21,12 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer { */ @Override public int countSumLengthOfWords(String text) { - return 0; + int count = 0; + List list = new ArrayList<>(getWords(text)); + for (String s : list) { + count += s.length(); + } + return count; } /** @@ -34,7 +37,8 @@ public int countSumLengthOfWords(String text) { */ @Override public int countNumberOfWords(String text) { - return 0; + List list = new ArrayList<>(getWords(text)); + return list.size(); } /** @@ -44,7 +48,8 @@ public int countNumberOfWords(String text) { */ @Override public int countNumberOfUniqueWords(String text) { - return 0; + Set set = new HashSet<>(getWords(text)); + return set.size(); } /** @@ -57,7 +62,14 @@ public int countNumberOfUniqueWords(String text) { */ @Override public List getWords(String text) { - return emptyList(); + List list = new ArrayList<>(); + String regex = "\\W"; + for (String s : text.split(regex)) { + if (!s.equals("")) { + list.add(s); + } + } + return list; } /** @@ -70,7 +82,7 @@ public List getWords(String text) { */ @Override public Set getUniqueWords(String text) { - return emptySet(); + return new HashSet<>(getWords(text)); } /** @@ -82,7 +94,13 @@ public Set getUniqueWords(String text) { */ @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + ArrayList list = new ArrayList<>(getWords(text)); + Map map = new HashMap<>(); + for (String s : list) { + int count = Collections.frequency(list, s); + map.put(s, count); + } + return map; } /** @@ -95,6 +113,13 @@ public Map countNumberOfWordsRepetitions(String text) { */ @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + List list = new ArrayList<>(getWords(text)); + if (direction.equals(Direction.ASC)) { + list.sort(Comparator.comparing(String::length)); + } else if (direction.equals(Direction.DESC)) { + list.sort(Comparator.comparing(String::length)); + Collections.reverse(list); + } + return list; } -} +} \ No newline at end of file diff --git a/src/main/java/com/epam/izh/rd/online/service/StreamApiTextStatisticsAnalyzer.java b/src/main/java/com/epam/izh/rd/online/service/StreamApiTextStatisticsAnalyzer.java index e9b8957..c41892b 100644 --- a/src/main/java/com/epam/izh/rd/online/service/StreamApiTextStatisticsAnalyzer.java +++ b/src/main/java/com/epam/izh/rd/online/service/StreamApiTextStatisticsAnalyzer.java @@ -2,12 +2,11 @@ import com.epam.izh.rd.online.helper.Direction; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; -import static java.util.Collections.*; /** * Данный класс обязан использовать StreamApi из функционала Java 8. Функциональность должна быть идентична @@ -16,36 +15,55 @@ public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer { @Override public int countSumLengthOfWords(String text) { - return 0; + List list = new ArrayList<>(getWords(text)); + Stream stream = list.stream(); + int count = stream.mapToInt(String::length).sum(); + return count; } @Override public int countNumberOfWords(String text) { - return 0; + List list = new ArrayList<>(getWords(text)); + Stream stream = list.stream(); + return (int) stream.count(); } @Override public int countNumberOfUniqueWords(String text) { - return 0; + Set set = new HashSet<>(getWords(text)); + Stream stream = set.stream(); + return (int) stream.count(); } @Override public List getWords(String text) { - return emptyList(); + String regex = "\\W"; + List list = Arrays.stream(text.split(regex)).collect(Collectors.toList()); + list.removeIf(String::isEmpty); + return list; } @Override public Set getUniqueWords(String text) { - return emptySet(); + ArrayList list = new ArrayList<>(getWords(text)); + return list.stream().collect(Collectors.toSet()); } @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + ArrayList list = new ArrayList<>(getWords(text)); + return list.stream().collect(Collectors.toMap(Function.identity(), x -> 1, Integer::sum)); } @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + List list = new ArrayList<>(getWords(text)); + if (direction.equals(Direction.ASC)) { + list = list.stream().sorted(Comparator.comparing(String::length)).collect(Collectors.toList()); + } else if (direction.equals(Direction.DESC)) { + list = list.stream().sorted(Comparator.comparing(String::length).reversed()).collect(Collectors.toList()); + } + return list; + } }