About 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.

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('&', "\&")
                            .build();

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:

StandardSystemProperty.JAVA_VERSION.value();
// [result] 1.7.0_25

StandardSystemProperty.JAVA_VERSION.key();
// [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);
q.add("one");
q.add("two");
q.add("three");
q.add("four");
// 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) {
    System.out.println(f.getAbsolutePath());
}

(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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

4 Responses to "Guava 15 – New features"

  1. Stefano Baghino says:

    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”‘?

  2. Michal says:

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

Leave a Reply


eight − 2 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close