Install
JSR-305 JAR is used for detecting defects and only when wanted to be used, it need to explicitly define that dependency. Attention, when using Guava in Scala, you have to include the jar.
While the Java compiler does not require the library
containing the annotations when compiling, the Scala compiler currently does.
Utilities that help deal with some of the common tasks in programming.
Joiner
Concatenate strings together with a specified delimiter.
Joiner stringJoiner = Joiner.on("|").skipNulls();
stringJoiner.join("foo", "bar", null);
Once created, a join class is immutable. so it doesn’t work when you code like this stringJoiner.useForNull("missing")
Joiner class also can work with StringBuilder/FileWriter helping appending by using joiner.appentTo()
, it returns the preceding instance.
MapJoiner
Joiner mapJoiner = Joiner.on("#").withKeyValueSeperator("=");
String test = mapJoiner.join(testMap);
Splitter
Take a string with some delimiter character and split that string on the delimiter and obtain an array of the parts of the strings.
Splitter is also immutable on creation.
Splitter.on("|").split("foo|bar|baz");
Splitter splitter = Splitter.on("\\d+");
Splitter splitter = Splitter.on("|").trimResults();
A Splitter class can split on a single character, a fixed string, a java.util.regex.Pattern
package or a CharMatcher class.
MapSplitter
Splitter.on("#").withKeyValueSeparator("=").split(string);
CharUtils
These classes all help work with strings.
Charsets
Use Charsets to specify character sets of returning byte, but now no worth when Java 7 has provided StandardCharsets class.
Strings
padEnd/padStart
insert character to the string in front or the end until meeting the minimum length. Attention, if the provided string already had met length, no paddding.
nullToEmpty emptyToNull isNullOrEmpty
, method name tells.
CharMatcher
Help work with characters based on the presence or absence of characters.
Unlike Joiner and Splitter, CharMatcher can be combined to a new class.
CharMatcher.BREAKING_WHITESPACE.replaceFrom(string, ' ');
CharMatcher.WHITESPACE.collapseFrom/trimAndCollapseFrom(string, ' ');
CharMatcher.JAVA_DIGIT.retainFrom(string) //retain deigit
Preconditions
checkNotNull(obj, errMsg)
checkElementIndex(index, size, errMsg)
checkArgument(expression, errMsg)
checkState(expression, errMsg) //expression for obj
As name tells.
Object
Object.toStringHelper(this).omitNullValues().add(string, property).toString();
Object.firstNotNull(string, defaultString);
ComparisonChain.start().compare()...result();