Skip to content

Commit 84b3303

Browse files
Cs4rshekhargulati
authored andcommitted
Feature required in #73 (#77)
1 parent 605eb4f commit 84b3303

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/main/java/strman/Strman.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
import java.util.regex.Pattern;
3535
import java.util.stream.Stream;
3636

37+
import static java.util.function.Function.identity;
38+
import static java.util.stream.Collectors.counting;
39+
import static java.util.stream.Collectors.groupingBy;
3740
import static java.util.stream.Collectors.joining;
3841

3942
/**
@@ -87,7 +90,7 @@ public static String appendArray(final String value, final String[] appends) {
8790
* @return an Optional String if found else empty
8891
*/
8992
public static Optional<String> at(final String value, int index) {
90-
if (value == null || value.isEmpty()) {
93+
if (isNullOrEmpty(value)) {
9194
return Optional.empty();
9295
}
9396
int length = value.length();
@@ -97,6 +100,10 @@ public static Optional<String> at(final String value, int index) {
97100
return (index < length && index >= 0) ? Optional.of(String.valueOf(value.charAt(index))) : Optional.empty();
98101
}
99102

103+
private static boolean isNullOrEmpty(String input) {
104+
return input == null || input.isEmpty();
105+
}
106+
100107
/**
101108
* Returns an array with strings between start and end.
102109
*
@@ -1302,4 +1309,20 @@ private static long countSubstr(String value, String subStr, boolean allowOverla
13021309
}
13031310
return countSubstr(value.substring(offset), subStr, allowOverlapping, ++count);
13041311
}
1312+
1313+
/**
1314+
* Counts the number of occurrences of each character in the string
1315+
*
1316+
* @param input The input string
1317+
* @return A map containing the number of occurrences of each character in the string
1318+
*/
1319+
public static Map<Character, Long> charsCount(String input) {
1320+
if (isNullOrEmpty(input)) {
1321+
return Collections.emptyMap();
1322+
}
1323+
1324+
return input.chars()
1325+
.mapToObj(c -> (char) c)
1326+
.collect(groupingBy(identity(), counting()));
1327+
}
13051328
}

src/test/java/strman/StrmanTest.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
import org.junit.Ignore;
3030
import org.junit.Test;
3131

32-
import java.util.Arrays;
33-
import java.util.Optional;
32+
import java.util.*;
3433

3534
import static java.util.stream.Collectors.toList;
3635
import static org.hamcrest.CoreMatchers.*;
@@ -990,4 +989,42 @@ public void trimEnd_shouldRemoveAllTrailingSpecialCharacters() throws Exception
990989
assertThat(trimEnd("-_-abc!-_-", "_", "-", "!"), is(Optional.of("-_-abc")));
991990
assertThat(trimEnd("-_-abc#-_-", "_", "-", "!", "#"), is(Optional.of("-_-abc")));
992991
}
992+
993+
994+
@Test
995+
public void charsCount_shouldReturnEmptyWhenInputStringIsNull(){
996+
assertThat(charsCount(null), equalTo(Collections.emptyMap()));
997+
}
998+
999+
@Test
1000+
public void charsCount_shouldReturnEmptyWhenInputStringIsEmpty(){
1001+
assertThat(charsCount(""), equalTo(Collections.emptyMap()));
1002+
}
1003+
1004+
@Test
1005+
public void charsCount_shouldReturnCharsCountWhenInputIsASimpleString(){
1006+
Map<Character, Long> expectedOutput = new HashMap<Character, Long>(){{
1007+
put('a',1L);
1008+
put('b', 1L);
1009+
put('c', 1L);
1010+
}};
1011+
1012+
assertThat(charsCount("abc"), equalTo(expectedOutput));
1013+
}
1014+
1015+
@Test
1016+
public void charsCount_shouldReturnCharsCountWhenInputIsAComplexString(){
1017+
Map<Character, Long> expectedOutput = new HashMap<Character, Long>(){{
1018+
put('a',1L);
1019+
put('b', 2L);
1020+
put('c', 3L);
1021+
1022+
put('A',1L);
1023+
put('B', 2L);
1024+
put('C', 3L);
1025+
put('-', 10L);
1026+
}};
1027+
1028+
assertThat(charsCount("-----abbcccCCCBBA-----"), equalTo(expectedOutput));
1029+
}
9931030
}

0 commit comments

Comments
 (0)