Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.Makivay.sandbox.slaviksShittyTasks;
- import com.google.common.base.Strings;
- import com.google.common.collect.Lists;
- import sun.reflect.generics.reflectiveObjects.NotImplementedException;
- import javax.annotation.Nonnull;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Objects;
- /**
- * Created by makivay
- * on 13.12.17
- */
- public class Example {
- private final static String WORDS_SPLITTER = " ";
- private final static String LINES_SPLITTER = System.lineSeparator();
- private static long splitAndCount(final @Nonnull String source, final String splitter, final boolean includeEmptyStrings) {
- return Arrays
- .stream(source.split("(?<=" + splitter + ")"))
- .map(s -> s.replace(splitter, ""))
- .filter(s -> includeEmptyStrings || !Strings.isNullOrEmpty(s))
- .count();
- }
- private static long count(final String source, final Target target) {
- final long count;
- if (Strings.isNullOrEmpty(source)) {
- count = 0;
- } else if (Objects.equals(Target.CHARS, target)) {
- count = source.length();
- } else if (Objects.equals(Target.WORDS, target)) {
- count = splitAndCount(source, WORDS_SPLITTER, false);
- } else if (Objects.equals(Target.LINES, target)) {
- count = splitAndCount(source, LINES_SPLITTER, true);
- } else {
- throw new NotImplementedException();
- }
- return count;
- }
- public static void main(String[] args) {
- final List<String> examples = Lists.newArrayList("", "aa", "aaa aaa", " ", "a\na", "\n\n\n", null);
- for (Target target : Target.values()) {
- System.out.println(target);
- for (String example : examples) {
- System.out.println(count(example, target));
- }
- }
- }
- enum Target {
- CHARS, WORDS, LINES
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement