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.
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.
Joiner mapJoiner = Joiner.on("#").withKeyValueSeperator("=");
String test = mapJoiner.join(testMap);
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.
Splitter.on("#").withKeyValueSeparator("=").split(string);
These classes all help work with strings.
Use Charsets to specify character sets of returning byte, but now no worth when Java 7 has provided StandardCharsets class.
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.
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
checkNotNull(obj, errMsg)
checkElementIndex(index, size, errMsg)
checkArgument(expression, errMsg)
checkState(expression, errMsg) //expression for obj
As name tells.
Object.toStringHelper(this).omitNullValues().add(string, property).toString();
Object.firstNotNull(string, defaultString);
ComparisonChain.start().compare()...result();