Merge pull request #9683 from sampada07/JAVA-2112

JAVA-2110: Split or move guava-collections module
This commit is contained in:
Dhawal Kapil 2020-07-12 11:08:15 +05:30 committed by GitHub
commit d4a7e1be4b
4 changed files with 120 additions and 106 deletions

View File

@ -4,3 +4,4 @@ This module contains articles a Google Guava
### Relevant Articles:
- [Introduction to Guava Throwables](https://www.baeldung.com/guava-throwables)
- [Guava CharMatcher](https://www.baeldung.com/guava-string-charmatcher)

View File

@ -0,0 +1,113 @@
package com.baeldung.guava.charmatcher;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import org.junit.Test;
import com.google.common.base.CharMatcher;
import com.google.common.base.Predicate;
public class GuavaCharMatcherUnitTest {
@Test
public void whenRemoveSpecialCharacters_thenRemoved() {
final String input = "H*el.lo,}12";
final CharMatcher matcher = CharMatcher.javaLetterOrDigit();
final String result = matcher.retainFrom(input);
assertEquals("Hello12", result);
}
@Test
public void whenRemoveNonASCIIChars_thenRemoved() {
final String input = "あhello₤";
String result = CharMatcher.ascii().retainFrom(input);
assertEquals("hello", result);
result = CharMatcher.inRange('0', 'z').retainFrom(input);
assertEquals("hello", result);
}
@Test
public void whenValidateString_thenValid() {
final String input = "hello";
boolean result = CharMatcher.javaLowerCase().matchesAllOf(input);
assertTrue(result);
result = CharMatcher.is('e').matchesAnyOf(input);
assertTrue(result);
result = CharMatcher.javaDigit().matchesNoneOf(input);
assertTrue(result);
}
@Test
public void whenTrimString_thenTrimmed() {
final String input = "---hello,,,";
String result = CharMatcher.is('-').trimLeadingFrom(input);
assertEquals("hello,,,", result);
result = CharMatcher.is(',').trimTrailingFrom(input);
assertEquals("---hello", result);
result = CharMatcher.anyOf("-,").trimFrom(input);
assertEquals("hello", result);
}
@Test
public void whenCollapseFromString_thenCollapsed() {
final String input = " hel lo ";
String result = CharMatcher.is(' ').collapseFrom(input, '-');
assertEquals("-hel-lo-", result);
result = CharMatcher.is(' ').trimAndCollapseFrom(input, '-');
assertEquals("hel-lo", result);
}
@Test
public void whenReplaceFromString_thenReplaced() {
final String input = "apple-banana.";
String result = CharMatcher.anyOf("-.").replaceFrom(input, '!');
assertEquals("apple!banana!", result);
result = CharMatcher.is('-').replaceFrom(input, " and ");
assertEquals("apple and banana.", result);
}
@Test
public void whenCountCharInString_thenCorrect() {
final String input = "a, c, z, 1, 2";
int result = CharMatcher.is(',').countIn(input);
assertEquals(4, result);
result = CharMatcher.inRange('a', 'h').countIn(input);
assertEquals(2, result);
}
@Test
public void whenRemoveCharsNotInCharset_thenRemoved() {
final Charset charset = Charset.forName("cp437");
final CharsetEncoder encoder = charset.newEncoder();
final Predicate<Character> inRange = new Predicate<Character>() {
@Override
public boolean apply(final Character c) {
return encoder.canEncode(c);
}
};
final String result = CharMatcher.forPredicate(inRange).retainFrom("helloは");
assertEquals("hello", result);
}
}

View File

@ -14,4 +14,3 @@ This module contains articles about Google Guava collections
- [Guava Lists](https://www.baeldung.com/guava-lists)
- [Guide to Guava MinMaxPriorityQueue and EvictingQueue](https://www.baeldung.com/guava-minmax-priority-queue-and-evicting-queue)
- [Guide to Guava Table](https://www.baeldung.com/guava-table)
- [Guava CharMatcher](https://www.baeldung.com/guava-string-charmatcher)

View File

@ -4,20 +4,19 @@ import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.google.common.collect.*;
import org.junit.Test;
import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public class GuavaStringUnitTest {
@ -113,102 +112,4 @@ public class GuavaStringUnitTest {
assertEquals(4, result.size());
assertThat(result, contains("a", "b", "c", "d,e"));
}
@Test
public void whenRemoveSpecialCharacters_thenRemoved() {
final String input = "H*el.lo,}12";
final CharMatcher matcher = CharMatcher.javaLetterOrDigit();
final String result = matcher.retainFrom(input);
assertEquals("Hello12", result);
}
@Test
public void whenRemoveNonASCIIChars_thenRemoved() {
final String input = "あhello₤";
String result = CharMatcher.ascii().retainFrom(input);
assertEquals("hello", result);
result = CharMatcher.inRange('0', 'z').retainFrom(input);
assertEquals("hello", result);
}
@Test
public void whenValidateString_thenValid() {
final String input = "hello";
boolean result = CharMatcher.javaLowerCase().matchesAllOf(input);
assertTrue(result);
result = CharMatcher.is('e').matchesAnyOf(input);
assertTrue(result);
result = CharMatcher.javaDigit().matchesNoneOf(input);
assertTrue(result);
}
@Test
public void whenTrimString_thenTrimmed() {
final String input = "---hello,,,";
String result = CharMatcher.is('-').trimLeadingFrom(input);
assertEquals("hello,,,", result);
result = CharMatcher.is(',').trimTrailingFrom(input);
assertEquals("---hello", result);
result = CharMatcher.anyOf("-,").trimFrom(input);
assertEquals("hello", result);
}
@Test
public void whenCollapseFromString_thenCollapsed() {
final String input = " hel lo ";
String result = CharMatcher.is(' ').collapseFrom(input, '-');
assertEquals("-hel-lo-", result);
result = CharMatcher.is(' ').trimAndCollapseFrom(input, '-');
assertEquals("hel-lo", result);
}
@Test
public void whenReplaceFromString_thenReplaced() {
final String input = "apple-banana.";
String result = CharMatcher.anyOf("-.").replaceFrom(input, '!');
assertEquals("apple!banana!", result);
result = CharMatcher.is('-').replaceFrom(input, " and ");
assertEquals("apple and banana.", result);
}
@Test
public void whenCountCharInString_thenCorrect() {
final String input = "a, c, z, 1, 2";
int result = CharMatcher.is(',').countIn(input);
assertEquals(4, result);
result = CharMatcher.inRange('a', 'h').countIn(input);
assertEquals(2, result);
}
@Test
public void whenRemoveCharsNotInCharset_thenRemoved() {
final Charset charset = Charset.forName("cp437");
final CharsetEncoder encoder = charset.newEncoder();
final Predicate<Character> inRange = new Predicate<Character>() {
@Override
public boolean apply(final Character c) {
return encoder.canEncode(c);
}
};
final String result = CharMatcher.forPredicate(inRange).retainFrom("helloは");
assertEquals("hello", result);
}
}