Feature required in #73 (#77)

This commit is contained in:
César Aguilera
2017-05-09 18:16:11 +05:30
committed by Shekhar Gulati
parent 605eb4f2a5
commit 84b3303718
2 changed files with 63 additions and 3 deletions
+24 -1
View File
@@ -34,6 +34,9 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;
/**
@@ -87,7 +90,7 @@ public abstract class Strman {
* @return an Optional String if found else empty
*/
public static Optional<String> at(final String value, int index) {
if (value == null || value.isEmpty()) {
if (isNullOrEmpty(value)) {
return Optional.empty();
}
int length = value.length();
@@ -97,6 +100,10 @@ public abstract class Strman {
return (index < length && index >= 0) ? Optional.of(String.valueOf(value.charAt(index))) : Optional.empty();
}
private static boolean isNullOrEmpty(String input) {
return input == null || input.isEmpty();
}
/**
* Returns an array with strings between start and end.
*
@@ -1302,4 +1309,20 @@ public abstract class Strman {
}
return countSubstr(value.substring(offset), subStr, allowOverlapping, ++count);
}
/**
* Counts the number of occurrences of each character in the string
*
* @param input The input string
* @return A map containing the number of occurrences of each character in the string
*/
public static Map<Character, Long> charsCount(String input) {
if (isNullOrEmpty(input)) {
return Collections.emptyMap();
}
return input.chars()
.mapToObj(c -> (char) c)
.collect(groupingBy(identity(), counting()));
}
}
+39 -2
View File
@@ -29,8 +29,7 @@ package strman;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Arrays;
import java.util.Optional;
import java.util.*;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.CoreMatchers.*;
@@ -990,4 +989,42 @@ public class StrmanTest {
assertThat(trimEnd("-_-abc!-_-", "_", "-", "!"), is(Optional.of("-_-abc")));
assertThat(trimEnd("-_-abc#-_-", "_", "-", "!", "#"), is(Optional.of("-_-abc")));
}
@Test
public void charsCount_shouldReturnEmptyWhenInputStringIsNull(){
assertThat(charsCount(null), equalTo(Collections.emptyMap()));
}
@Test
public void charsCount_shouldReturnEmptyWhenInputStringIsEmpty(){
assertThat(charsCount(""), equalTo(Collections.emptyMap()));
}
@Test
public void charsCount_shouldReturnCharsCountWhenInputIsASimpleString(){
Map<Character, Long> expectedOutput = new HashMap<Character, Long>(){{
put('a',1L);
put('b', 1L);
put('c', 1L);
}};
assertThat(charsCount("abc"), equalTo(expectedOutput));
}
@Test
public void charsCount_shouldReturnCharsCountWhenInputIsAComplexString(){
Map<Character, Long> expectedOutput = new HashMap<Character, Long>(){{
put('a',1L);
put('b', 2L);
put('c', 3L);
put('A',1L);
put('B', 2L);
put('C', 3L);
put('-', 10L);
}};
assertThat(charsCount("-----abbcccCCCBBA-----"), equalTo(expectedOutput));
}
}