Templates by BIGtheme NET

An Overview of Guava

What’s Guava :
1) Extensive suite of common libraries that is used in many Java projects.
2) These utilities relate to: collections, concurrency, primitives, reflection, comparison,
I/O, hashing, networking, strings, math, in-memory caching, in-memory publish/subscribe…
and various basic data types.
3) JDK required is Java 6+.

Before jumping to Guava, we will have brief about Functional Interfaces.
These Functional Interfaces help us to understand Guava better.

Functional Interfaces :
1) Are interfaces defining a single method.
2) Also known as Single Abstract Method (SAM).
3) Provide the basis for Lambda Expressions in Java8.
Cool Fact:
Anywhere you specify one of these interfaces as a method parameter, you’ll be able to pass
a Lambda expression in Java8!
4) Can be thought of as interfaces specifying a callback object

Some Examples on Functional Interface :
These examples are Java 8 functional interfaces.
1) Functional Interface with single method argument.
This method is to check, the given order is new or not.

@FunctionalInterface
public interface IOrder {
  public boolean check(Order o);
}

We can create lambda expression, to use the above functional interface.

 IOrder newOrder = (Order o) -> o.getStatus().equals("NEW");
(or)
IOrder newOrder = (o) -> o.getStatus().equals("NEW"); 

1) Here, the right side of the expression is simply the body of the check method – checking
the status of the passed in Order.
2) The real power of using lambdas comes when you start creating a multitude of them representing
real-world behavioral functions.
3) For example, in addition to what we’ve already seen, here are the lambda expressions for finding
out big Order.

// Lambda for big Order
IOrder bigOrderLambda = (Order o) -> o.getQuantity() > 10000000;
 
// Lambda that checks if the Order is a new large Order
ITrade issuerBigNewTradeLambda = (o) -> {
    return o.getIssuer().equals("Honda") &&
           o.getQuantity() > 10000000 &&
           o.getStatus().equals("NEW");
  };

The Google “Guava” libraries provide several very useful SAMs, all under the “com.google.common.base” package, including the two:
1) Function – specifies a method which accepts a instance of type “T” and returns
an instance of type “R”.
2) Predicate – specifies a method that is passed an instance of type “T” and returns true
or false.
3) Supplier – specifies a method which can return a value.

Please take a look also at the utility method helper classes associated with each of these SAMs:
1) Functions – utility methods operating on Function instances
2) Predicates – utility methods operating on Predicate instances
3) Suppliers – utility methods operating on Supplier instances

Note: the SAMs above were born in Guava, but have pretty much been cloned in Java8,
so any use you make of them now should be easy to port when we move to Java8.

I think you’ll pretty quickly see how these interfaces can be used in a wide variety of cases
to inject and combine reusable building blocks of functionality.

*** Venkat – Happy leaning ****