Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. package ru.Makivay.sandbox.slaviksShittyTasks;
  2.  
  3.  
  4. import com.google.common.base.Strings;
  5. import com.google.common.collect.Lists;
  6. import sun.reflect.generics.reflectiveObjects.NotImplementedException;
  7.  
  8. import javax.annotation.Nonnull;
  9. import java.util.Arrays;
  10. import java.util.List;
  11. import java.util.Objects;
  12.  
  13. /**
  14.  * Created by makivay
  15.  * on 13.12.17
  16.  */
  17. public class Example {
  18.  
  19.     private final static String WORDS_SPLITTER = " ";
  20.     private final static String LINES_SPLITTER = System.lineSeparator();
  21.  
  22.  
  23.     private static long splitAndCount(final @Nonnull String source, final String splitter, final boolean includeEmptyStrings) {
  24.         return Arrays
  25.                 .stream(source.split("(?<=" + splitter + ")"))
  26.                 .map(s -> s.replace(splitter, ""))
  27.                 .filter(s -> includeEmptyStrings || !Strings.isNullOrEmpty(s))
  28.                 .count();
  29.     }
  30.  
  31.     private static long count(final String source, final Target target) {
  32.         final long count;
  33.         if (Strings.isNullOrEmpty(source)) {
  34.             count = 0;
  35.         } else if (Objects.equals(Target.CHARS, target)) {
  36.             count = source.length();
  37.         } else if (Objects.equals(Target.WORDS, target)) {
  38.             count = splitAndCount(source, WORDS_SPLITTER, false);
  39.         } else if (Objects.equals(Target.LINES, target)) {
  40.             count = splitAndCount(source, LINES_SPLITTER, true);
  41.         } else {
  42.             throw new NotImplementedException();
  43.         }
  44.         return count;
  45.     }
  46.  
  47.     public static void main(String[] args) {
  48.         final List<String> examples = Lists.newArrayList("", "aa", "aaa aaa", "    ", "a\na", "\n\n\n", null);
  49.         for (Target target : Target.values()) {
  50.             System.out.println(target);
  51.             for (String example : examples) {
  52.                 System.out.println(count(example, target));
  53.             }
  54.         }
  55.     }
  56.  
  57.     enum Target {
  58.         CHARS, WORDS, LINES
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement