Guava学习笔记(二):基础(Joiner,Objects,Splitter及Strings)
添加Maven依赖
JoinerTest
import com.google.common.base.Joiner;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
public class JoinerTest {
private static final List<String> stringList = new ArrayList<String>();
static {
stringList.add("a");
stringList.add("b");
}
/**
* 基本用法
*
* @see Joiner#join(Object[])
* @see Joiner#join(Iterable)
* @see Joiner#join(Object, Object, Object...)
*/
@Test
public void test_base() {
String join = Joiner.on("|").join(new String[]{"a", "b"});
Assert.assertEquals("a|b", join);
join = Joiner.on("@").join(stringList);
Assert.assertEquals("a@b", join);
join = Joiner.on(",").join("a", "b");
Assert.assertEquals("a,b", join);
}
/**
* 处理有null的情况,跳过或赋予默认值
*/
@Test
public void test_null() {
stringList.add(null);
String join = Joiner.on("|").skipNulls().join(stringList);
Assert.assertEquals("a|b", join);
join = Joiner.on("|").useForNull("no value").join(stringList);
Assert.assertEquals("a|b|no value", join);
}
/**
* 处理追加情況
*/
@Test
public void test_append() {
StringBuilder stringBuilder = new StringBuilder("test:{");
String string = Joiner.on("|").appendTo(stringBuilder, stringList).append("}").toString();
Assert.assertEquals("test:{a|b}", string);
}
@Test
public void test_mapJoiner() {
Joiner.MapJoiner mapJoiner = Joiner.on(",").withKeyValueSeparator("=");
LinkedHashMap<Object, Object> linkedHashMap = new LinkedHashMap<Object, Object>();
linkedHashMap.put("a", 65);
linkedHashMap.put("b", 66);
String join = mapJoiner.join(linkedHashMap);
Assert.assertEquals("a=65,b=66", join);
}
}
ObjectsTest
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.collect.ComparisonChain;
import org.hamcrest.core.Is;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class ObjectsTest {
private String a;
private List<Integer> b;
private Date date;
@Test
public void test_compareTo() {
ObjectsTest objectsTest = new ObjectsTest();
objectsTest.setA("a");
ObjectsTest otherObjectsTest = new ObjectsTest();
otherObjectsTest.setA("b");
Assert.assertNotEquals(0, objectsTest.compareTo(otherObjectsTest));
}
@Test
public void test_toString() {
b = Arrays.asList(1, 3);
String string = MoreObjects.toStringHelper(this).omitNullValues().add("a", a).add("b", b).toString();
Assert.assertThat(string, Is.is("ObjectsTest{b=[1, 3]}"));
a = "hah";
string = MoreObjects.toStringHelper(this).omitNullValues().add("a", a).add("b", b).toString();
Assert.assertThat(string, Is.is("ObjectsTest{a=hah, b=[1, 3]}"));
}
@Test
public void test_firstNonNull() {
String s = null;
String default_value = MoreObjects.firstNonNull(s, "default value");
Assert.assertEquals("default value", default_value);
}
@Test
public void test_hashCode() {
int i = Objects.hashCode(a, b);
Assert.assertNotEquals(0, i);
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public List<Integer> getB() {
return b;
}
public void setB(List<Integer> b) {
this.b = b;
}
public int compareTo(ObjectsTest objectsTest) {
return ComparisonChain.start().compare(this.a, objectsTest.a).compare(this.date, objectsTest.date).result();
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
SplitterTest
import com.google.common.base.Splitter;
import org.junit.Assert;
import org.junit.Test;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class SplitterTest {
@Test
public void test_base() {
Iterable<String> split = Splitter.on("|").split("a|b|c");
String next = split.iterator().next();
Assert.assertEquals("a", next);
}
@Test
public void test_mapSplitter() {
LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("a", "65");
linkedHashMap.put("b", "66");
Map<String, String> stringMap = Splitter.on(",").withKeyValueSeparator("=").split("a=65,b=66");
assertThat(linkedHashMap, is(stringMap));
}
}
StringsTest
import com.google.common.base.CharMatcher;
import org.junit.Test;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class StringsTest {
/**
* 返回输入字符序列的字符串副本,每个匹配此匹配器的连续字符组由单个替换字符替换。 例如:
* CharMatcher.anyOf(“eko”).collapseFrom(“bookkeeper”,“ - ”) 返回“b-p-r”。
* 默认实现使用indexIn(CharSequence)来查找第一个匹配字符,然后迭代序列的其余部分,为每个字符调用matches(char)。
*/
@Test
public void test_RemoveWhiteSpace() {
String tabsAndSpaces = "String with spaces and tabs ";
String expected = "String with spaces and tabs ";
String scrubbed = CharMatcher.WHITESPACE.
collapseFrom(tabsAndSpaces, ' ');
assertThat(scrubbed, is(expected));
}
/**
* 除了替换连续字符外,将原字符串中的前后空格去掉
*/
@Test
public void test_TrimRemoveWhiteSpace() {
String tabsAndSpaces = " String with spaces and tabs ";
String expected = "String with spaces and tabs";
String scrubbed = CharMatcher.WHITESPACE.
trimAndCollapseFrom(tabsAndSpaces, ' ');
assertThat(scrubbed, is(expected));
}
/**
* 保留数字,另外CharMatcher还有逻辑运算
*
* @see CharMatcher#or(CharMatcher)
* @see CharMatcher#and(CharMatcher)
*/
@Test
public void testRetainFrom() {
String lettersAndNumbers = "foo989yxbar234";
String expected = "989234";
String retained = CharMatcher.JAVA_DIGIT.
retainFrom(lettersAndNumbers);
assertThat(expected, is(retained));
}
}
God, Grant me the SERENITY, to accept the things I cannot change,
COURAGE to change the things I can, and the WISDOM to know the difference.