ted.neward@newardassociates.com | Blog: http://blogs.newardassociates.com | Github: tedneward | LinkedIn: tedneward
Let's take a look at Guava
most commonly used parts
rationale for its existence
additional resources for its use
https://github.com/google/guava
Guava is a set of Google's extensions to Java
not extensions in the language sense, but the library sense
provides a number of interesting libraries
caching
collections
concurrent utilities
and more
as such, there's a fair amount of ground to cover
Basic utilities: Make using the Java language more pleasant.
Functional idioms
Primitives: operations on primitive types not provided by the JDK.
Strings: A few extremely useful string utilities: splitting, joining, padding, and more.
Collections: Guava's extensions to the JDK collections ecosystem.
Graphs: a library for modeling graph-structured data.
Caches: Local caching, done right, and supporting a wide variety of expiration behaviors.
Concurrency: Powerful, simple abstractions to make it easier to write correct concurrent code.
Ranges: Guava's powerful API for dealing with ranges on Comparable types.
I/O: Simplified I/O operations, especially on whole I/O streams and files.
Hashing: Tools for more sophisticated hashes.
EventBus: Publish-subscribe-style communication between components.
Math: Optimized, thoroughly tested math utilities not provided by the JDK.
Reflection: Guava utilities for Java's reflective capabilities.
https://github.com/google/guava/wiki/UseGuavaInYourBuild
download directly
or through a build tool
or build it yourself
Guava via Maven
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>33.4.8-jre</version> <!-- or, for Android: <version>33.4.8-android</version> --> </dependency>
Guava via Gradle
dependencies { // Pick one: // 1. Use Guava in your implementation only: implementation("com.google.guava:guava:33.4.8-jre") // 2. Use Guava types in your public API: api("com.google.guava:guava:33.4.8-jre") // 3. Android - Use Guava in your implementation only: implementation("com.google.guava:guava:33.4.8-android") // 4. Android - Use Guava types in your public API: api("com.google.guava:guava:33.4.8-android") }
Guava via source
$ git clone https://github.com/google/guava $ ./mvnw
Objects
: common Object operations
equal, hashCode, firstNonNull
toStringHelper – fluent interface for implementing toString()
Equivalences
: equals-vs-identity distinction factory
Defaults
: provide default values for JLS types
Splitter
/Joiner
: extension methods to split/join collections
Preconditions
: series of assertion-like check methods
useful for ensuring preconditions/postconditions/etc
Throwables
: extension methods making Throwable easier
Strings
: another extension class for Strings
CaseFormat
: enum (with methods) to convert between different upper/lower case formats
LOWER_CAMEL
, LOWER_HYPHEN
, etc
Ascii
: methods pertaining to the ASCII character set
Charsets
: six common Charset objects
CharMatcher
: Fluent interface for matching/parsing character strings
this is a wonderful (yet not 100%) replacement for regex
Supplier
: interface describing the production of T
Suppliers
: useful methods around Suppliers
most of these are to compose Suppliers in interesting ways
Function
, Predicate
Function is a wrapper around T apply(F in);
Predicate is a wrapper around bool apply(T in);
Functions
: series of extension methods around Function
NOT a full functional library
Predicates
: ditto for Predicate
more extensive than Functions, but still incomplete
"excessive use... the Guava team weeps"
code performing operations on behalf of clients
RPC, web servers, chat servers, etc
Service
: interface defining basic Service operations
start(), stop(), startAndWait(), stopAndWait(), state()
Two base classes commonly used for default behavior
AbstractExecutionThreadService
: uses a single thread
AbstractService
: manage threading manually (harder)
(both are in com.google.common.concurrent.util
)
FinalizableReferenceQueue
FinalizableSoftReference
FinalizableWeakReference
FinalizablePhantomReference
these only matter if you use Soft/Weak/Phantom already
Source: https://github.com/google/guava
User guide: https://github.com/google/guava/wiki
Guava announce list: https://groups.google.com/group/guava-announce
Guava discussion list: https://groups.google.com/group/guava-discuss
Guava's a good base of functionality to consider
it's just a jar file, so most of its features are "opt-in"
most developers will see best results from starting with .base
and .collect
packages and working from there
it's a huge pile of functionality, well-tested
Google promises continued enhancements and fixes
Architect, Engineering Manager/Leader, "force multiplier"
http://www.newardassociates.com
http://blogs.newardassociates.com
Sr Distinguished Engineer, Capital One
Educative (http://educative.io) Author
Performance Management for Engineering Managers
Books
Developer Relations Activity Patterns (w/Woodruff, et al; APress, forthcoming)
Professional F# 2.0 (w/Erickson, et al; Wrox, 2010)
Effective Enterprise Java (Addison-Wesley, 2004)
SSCLI Essentials (w/Stutz, et al; OReilly, 2003)
Server-Based Java Programming (Manning, 2000)