Enterprise Java

Guava 15 – New features

A new version of the Guava library was released earlier this month and contains several new features and improvements.

Here is an overview of some of the significant API additions in this release:

1. Escapers

Escapers allow you to “escape” special characters in a string in order to make the string conform to a particular format. For example, in XML, the < character must be converted into &lt; for inclusion in XML elements. Guava provides the following Escapers:

You can also build your own Escaper. Here is an example of various Escapers in action:

// escaping HTML
HtmlEscapers.htmlEscaper().escape("echo foo > file &");
// [result] echo foo > file &

// escaping XML attributes and content
XmlEscapers.xmlAttributeEscaper().escape("foo \"bar\"");
// [result] echo "bar"

XmlEscapers.xmlContentEscaper().escape("foo \"bar\"");
// [result] foo "bar"

// Custom Escaper
// escape single quote with another single quote
// and escape ampersand with backslash
Escaper myEscaper = Escapers.builder()
                            .addEscape('\'', "''")
                            .addEscape('&', "\&")

2. StandardSystemProperty

StandardSystemProperty is an enum of Java system properties such as java.version, java.home etc. The great thing about this is that you no longer need to remember what the system properties are called because you simply use the enum! Here is an example:

// [result] 1.7.0_25

// [result] java.version

3. EvictingQueue

The EvictingQueue is a non-blocking queue which removes elements from the head of the queue when it is full and you attempt to insert a new element. Example:

// create an EvictingQueue with a size of 3
EvictingQueue<String> q = EvictingQueue.create(3);
// the head of the queue is evicted after adding the fourth element
// queue contains: [two, three, four]

4. fileTreeTraverser

As its name suggests, Files.fileTreeTraverser allows you to traverse a file tree.

FluentIterable<File> iterable = Files.fileTreeTraverser().breadthFirstTraversal(new File("/var/tmp"));
for (File f : iterable) {

(Note: Java 7’s Files.walkFileTree also traverses a file tree and I showed you to use it in one of my earlier posts: Java 7: Deleting a Directory by Walking the File Tree. I’d recommend this approach if you are using Java 7.)

The full release notes of Guava 15 can be found here.

Reference: Guava 15 – New features from our JCG partner Fahd Shariff at the fahd.blog blog.

Fahd Shariff

Fahd is a software engineer working in the financial services industry. He is passionate about technology and specializes in Java application development in distributed environments.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
Stefano Baghino
Stefano Baghino
10 years ago

I think there’s a little mistake on line 7 of the first snippet of code: shouldn’t the result be ‘foo “bar”‘ and not ‘echo “bar”‘?

Fahd Shariff
10 years ago

Well spotted, Stefano! Yes, it is incorrect. The result of the xmlAttributeEscaper should be: foo "bar"

Also, the result of the htmlEscaper hasn’t come out correctly here. It should be: echo foo > file &

Take a look at the original post: http://fahdshariff.blogspot.co.uk/2013/09/guava-15-new-features.html


Fahd Shariff
10 years ago
Reply to  Fahd Shariff

hmm, it looks like my comment above didn’t display correctly :(

Should have been:

foo & quot; bar & quot;


echo foo & gt; file & amp;

10 years ago

Happy to see EvictingQueue in Guava! I can finally remove dependency on Apache Commons (I’ve been using CircularFifoBuffer so far).

Back to top button