You don’t want empty strings to be part of my result.
So, split() method is leaving something to be desired.
But with the help of Splitter Class, code equivalent to above can be written asSplitter splitter = Splitter.
on(“,”);String[] parts = splitter.
split(test);split() method returns an iterable object containing individual string parts from test string.
trimResults() method can be used to remove leading and trailing whitespaces from results.
Just like Joiner class, Splitter class also is immutable once it is created.
So, it is thread-safe and can be used as static final variable.
2.
4 MapSplitter ClassSplitter class is accompanied by MapSplitter.
It takes in a string in which key-value pairs are delimited by some delimiter (a character, a string or even a regex pattern) and returns Map instance with key-value pair in same order as in original string.
public void DemoMapSplitter() { String test = “India=Hocket#England=Cricket”; // Initialising Guava LinkedHashMap Collection Map<String, String> myTestMap = Maps.
newLinkedHashMap(); myMap.
put(“India”, “Hockey”); myMap.
put(“England”, “Cricket”); String delimiter = “#”; String seperator = “=”; Splitter.
MapSplitter mapSplitter = Splitter.
on(delimiter).
withKeyValueSeperator(seperator); Map<String, String> myExpectedMap = mapSplitter.
split(test); assertThat(myTestMap, myExpectedMap);}2.
5 Precondition ClassPrecondition class provides collection of static methods to check the state of our code.
Preconditions are important because they guarantee out expectations for successful code are met.
For example:1.
Checking for null conditions.
You can always writeif (testObj == null) throw new IllegalArgumentException(“testObj is null”);Using Precondition class makes it more concise and easy-to-usecheckNotNull(testObj, “testObj is null”);2.
Checking for valid arguments.
public void demoPrecondition { private int age; public demoPrecondition(int age) { checkArgument(age > 0, “Invalid Age”); this.
age = age; }}checkArgument(exp, msg) evaluates state of variable passed as parameter to a method.
It evaluates a boolean expression exp and throws IllegalArgumentException if expression evaluates to false.
3.
Checking state of an objectpublic void demoPrecondition { private String name; public demoPrecondition(String name) { this.
name = checkNotNull(name, “Anonamous”); } public void Capitalize() { checkState(validate(), “Empty Name”); } private bool validate() { this.
name.
length() > 0; } }checkState(exp, msg) evaluates state of object not argument passed to a method.
It evaluates a boolean expression exp and throws IllegalArgumentException if expression evaluates to false.
4.
Checking valid element indexpublic void demoPrecondition { int size; private int [] price; public demoPrecondition(int size) { this.
size = checkArgument(size > 0, “size must be greater than 0”); this.
price = new int[this.
size]; } public void updateItem(int index, int value) { int indexToBeUpdated = checkElementIndex(index, this.
size, “Illegal Index Access”); } }Thanks for the read.
In the next post, we’ll talk about Functional Programming in Guava.
.. More details