Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The task is ready #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.epam.izh.rd.online.service;

import com.epam.izh.rd.online.helper.Direction;

import java.util.*;

import static com.epam.izh.rd.online.helper.Direction.ASC;
import static java.util.Collections.*;

/**
Expand All @@ -23,7 +22,12 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
*/
@Override
public int countSumLengthOfWords(String text) {
return 0;
if (text == null || text.length() == 0) return 0;
int sumLengthOfWords = 0;
for (String word : getWords(text)) {
sumLengthOfWords += word.length();
}
return sumLengthOfWords;
}

/**
Expand All @@ -34,7 +38,8 @@ public int countSumLengthOfWords(String text) {
*/
@Override
public int countNumberOfWords(String text) {
return 0;
if (text == null || text.length() == 0) return 0;
return getWords(text).size();
}

/**
Expand All @@ -44,7 +49,8 @@ public int countNumberOfWords(String text) {
*/
@Override
public int countNumberOfUniqueWords(String text) {
return 0;
if (text == null || text.length() == 0) return 0;
return getUniqueWords(text).size();
}

/**
Expand All @@ -57,7 +63,14 @@ public int countNumberOfUniqueWords(String text) {
*/
@Override
public List<String> getWords(String text) {
return emptyList();
if (text == null || text.length() == 0) return emptyList();
List<String> words = new ArrayList<>();
for (String word : text.split("\\W")) {
if (!word.isEmpty()) {
words.add(word);
}
}
return words;
}

/**
Expand All @@ -70,7 +83,8 @@ public List<String> getWords(String text) {
*/
@Override
public Set<String> getUniqueWords(String text) {
return emptySet();
if (text == null || text.length() == 0) return emptySet();
return new HashSet<>(getWords(text));
}

/**
Expand All @@ -82,7 +96,13 @@ public Set<String> getUniqueWords(String text) {
*/
@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
if (text == null || text.length() == 0) return emptyMap();
Map<String, Integer> wordRepeats = new HashMap<>();
for (String word :getWords(text)) {
wordRepeats.putIfAbsent(word, 0);
wordRepeats.put(word, wordRepeats.get(word) + 1);
}
return wordRepeats;
}

/**
Expand All @@ -95,6 +115,9 @@ public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
*/
@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
if (text == null || text.length() == 0) return emptyList();
List<String> wordsByLength = new ArrayList<>(getWords(text));
wordsByLength.sort((o1, o2) -> direction == ASC ? o1.length() - o2.length() : o2.length() - o1.length());
return wordsByLength;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,62 @@

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.stream.Collectors;
import static com.epam.izh.rd.online.helper.Direction.ASC;
import static java.util.Collections.*;

/**
* Данный класс обязан использовать StreamApi из функционала Java 8. Функциональность должна быть идентична
* {@link SimpleTextStatisticsAnalyzer}.
*/
public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer {

@Override
public int countSumLengthOfWords(String text) {
return 0;
if (text == null || text.length() == 0) return 0;
return getWords(text).stream().mapToInt(String::length).reduce(Integer::sum).orElse(0);
}

@Override
public int countNumberOfWords(String text) {
return 0;
if (text == null || text.length() == 0) return 0;
return getWords(text).size();
}

@Override
public int countNumberOfUniqueWords(String text) {
return 0;
if (text == null || text.length() == 0) return 0;
return getUniqueWords(text).size();
}

@Override
public List<String> getWords(String text) {
return emptyList();
if (text == null || text.length() == 0) return emptyList();
return Arrays.stream(text.split("\\W")).filter(s -> !s.isEmpty()).collect(Collectors.toList());
}

@Override
public Set<String> getUniqueWords(String text) {
return emptySet();
if (text == null || text.length() == 0) return emptySet();
return new HashSet<>(getWords(text));
}

@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
if (text == null || text.length() == 0) return emptyMap();
return getWords(text).stream().collect(Collectors.toMap(
s -> s,
s -> 1,
(a, b) -> a + 1
)
);
}

@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
if (text == null || text.length() == 0) return emptyList();
return getWords(text).stream().sorted((o1, o2) -> direction == ASC ? o1.length() - o2.length() : o2.length() - o1.length())
.collect(Collectors.toList());
}
}