Google Guava and Its Two Fantastic Libraries: Graph and Eventbus
The main idea behind it was to include generics introduced in JDK 1.5 into Java Collections Framework, or JCF, and enhance its capability.
Join the DZone community and get the full member experience.Join For Free
Guava is an open-source library that developed with Google to meet the daily needs of programmers; actually, Guava prevents code duplication that may be written by programmers all around the world. In addition to eliminating boilerplate codes, Guava makes programming easier and more efficient with better performance. Personally, I think Google used Artificial Intelligence to recognize Java developer's requirements by using their daily searches. Google Guava is a Google project, mainly developed by Google's developers, but it's been open-sourced now. The main idea behind it was to include generics introduced in JDK 1.5 into Java Collections Framework, or JCF, and enhance its capability. In general, Guava consists of three major components:
First, basic utilities to reduce manual labor to implement common methods and behaviors and sometimes boilerplate codes.
Second, a complementary component for current collection frameworks such as Java collections framework (JCF) formerly called the Google Collections Library and apache commons-collections, actually Guava is an extension for JCF but it has many more features than apache common collections you can see a comparison between Google Guava and Apache Commons Collections here.
Third, other utilities which provide convenient and productive features such as functional programming, you can a list of some important Guava utilities in the below table.
|Basic utilities||Using and avoiding null||
||Preconditions||Test preconditions for your methods more easily|
||Common object methods||Simplify implementing
|Ordering||Guava's powerful "fluent
|Collections||Immutable collections||for defensive programming, constant collections, and improved efficiency|
|Collections||New collection types||for use cases that the JDK collections don't address as well as they could: multisets, multimaps, tables, bidirectional maps, and more|
|Collections||Powerful collection utilities||for common operations not provided in
|Collections||Extension utilities||writing a
|Graphs||Graph||a graph whose edges are anonymous entities with no identity or information of their own|
|Graphs||ValueGraph||a graph whose edges have associated non-unique values|
|Graphs||Network||a graph whose edges are unique objects|
|Caches||Caches||Local caching, done right, and supporting a wide variety of expiration behaviors|
|I/O||I/O||Simplified I/O operations, especially on whole I/O streams and files, for Java 5 and 6|
|Event Bus||EventBus||Publish-subscribe-style communication between components without requiring the components to explicitly register with one another.|
|reflection||Reflection||Guava utilities for Java's reflective capabilities|
How to Add Guava to a Project
Thank god, Maven has made life easy; just add the following dependency to your project :
A graph is a data structure consisting of nodes and edges. The nodes are sometimes also referred to as vertices and the edges are lines or arcs that connect any two nodes in the graph. Graphs are a relatively complex data structure that every programmer has encountered at least once and every programmer has to provide their own codes to manipulate and traverse it. Guava graphs come with some utility classes to make programmers convenient to work with graphs.
'Graph<Object>' is the base interface in the package:
MutableGraph and ImmutableGraph are the two major implementations of it.
You can build a graph with any type with singleton class named
Add Nodes, Edge
Now, you have a graph named 'myGraph.' Time to add nodes and edges to it.
First, you can add nodes and then add an edge between them:
or directly add edges :
Remove Nodes, Edges
It's easy! Exactly do the opposite of the previous code:
com.google.common.graph.Traverser<N> is a magical class that can traverse the nodes that are reachable from a specified (set of) start node(s).
Traverser has following methods:
|breadthFirst(Iterable<? extends N> startNodes)||Iterable over the nodes reachable from any of the startNodes, in the order of a breadth-first traversal|
|breadthFirst(N startNode)||Iterable over the nodes reachable from startNode, in the order of a breadth-first traversal|
|depthFirstPostOrder(Iterable<? extends N> startNodes)||Iterable over the nodes reachable from any of the startNodes, in the order of a depth-first post-order traversal|
|depthFirstPostOrder(N startNode)||Iterable over the nodes reachable from startNode, in the order of a depth-first post-order traversal|
|depthFirstPreOrder(Iterable<? extends N> startNodes)||Iterable over the nodes reachable from any of the startNodes, in the order of a depth-first pre-order traversal|
|forGraph(SuccessorsFunction<N> graph)||Creates a new traverser for the given general graph.|
|forTree(SuccessorsFunction<N> tree)||Creates a new traverser for a directed acyclic graph that has at most one path from the start node(s) to any node reachable from the start node(s), and has no paths from any start node to any other start node, such as a tree or forest|
Let's create and traverse a simple graph using Guava:
I tried to explain the concepts in a very simple and practical way, and also tried as much as possible not to get too involved in theoretical and academic topics .but if you need more information and more theoretical concepts you can find them here.
An Eventbus is a mechanism that allows different components to communicate with each other without knowing about each other. A component can send an Event to the Eventbus without knowing who will pick it up or how many others will pick it up. Components can also listen to Events on an Eventbus, without knowing who sent the Events. That way, components can communicate without depending on each other. Also, it is very easy to substitute a component. As long as the new component understands the Events that are being sent and received, the other components will never know.
for using Eventbus, first of all, for registering listeners and posting events you need a Bus:
or every publish-subscribe system, Listeners are a must. Listeners can be created easily with the following codes:
As you can see, the handler method has annotated with
@Subscribe; now Listener should be added to the Eventbus and posting event. Here is a simple example:
OK! everything looks great. but this code was very simple. Let's solve a real-world problem: take a look at a sample from hackernoon. In this example, we would demonstrate how to use an Eventbus for simulating the payment process:
in this example we have two listeners, PaymentService and RecipieSender that are responsible for the transaction and sending recipes to Buyer and Seller:
So, What Is Next?!
Guava EventBus has amazing integration with Apache Camel you can read more about it here.
Opinions expressed by DZone contributors are their own.