Slides for presentation on Google Guava I gave at the Near Infinity (www.nearinfinity.com) 2013 spring conference.
The associated sample code is on GitHub at https://github.com/sleberknight/google-guava-samples
22. FP in Guava
FluentIterable<Integer> squaresOfEvens = FluentIterable.from(numbers)
.filter(new Predicate<Integer>() {
@Override
public boolean apply(@Nullable Integer input) {
checkNotNull(input, "nulls are not allowed here!");
return input % 2 == 0;
}
})
.transform(new Function<Integer, Integer>() {
@Nullable
@Override
public Integer apply(@Nullable Integer input) {
checkNotNull(input, "nulls are not allowed here!");
return input * input;
}
});
// [ 4, 16, 36, 64, 100 ]
WAT?
23. Until Java 8...
List<Integer> squaresOfEvens = Lists.newArrayList();
for (Integer number : numbers) {
if (number % 2 == 0) {
squaresOfEvens.add(number * number);
}
}
// [ 4, 16, 36, 64, 100 ]
24. "Excessive use of Guava's functional programming idioms
can lead to verbose, confusing, unreadable, and inefficient
code.These are by far the most easily (and most
commonly) abused parts of Guava, and when you go to
preposterous lengths to make your code "a one-liner," the
Guava team weeps."
25. ListenableFuture
// setup...
ExecutorService delegate = Executors.newFixedThreadPool(MAX_THREADS);
ListeningExecutorService executorService =
MoreExecutors.listeningDecorator(delegate);
// submit tasks...
ListenableFuture<WorkResult> future = executorService.submit(worker);
Futures.addCallback(future, new FutureCallback<WorkResult>() {
@Override
public void onSuccess(WorkResult result) {
// do something after success...
}
@Override
public void onFailure(Throwable t) {
// handle error...
}
}, executorService););
27. "Guava deprecates, and yes, deletes
unwanted features over time. It is
important to us that when you see a
feature in the Javadocs, it represents
the Guava team's best work, and not
a feature that in retrospect was a bad
idea."
30. Photo Attributions
* this one is iStockPhoto (paid) -->
http://www.morguefile.com/archive/display/138854
http://www.flickr.com/photos/hermansaksono/4297175782/
http://commons.wikimedia.org/wiki/File:Guava_ID.jpg
http://www.flickr.com/photos/88845568@N00/2076930689/
http://www.flickr.com/photos/mohannad_khatib/6352720649/
32. My Info
scott dot leberknight at nearinfinity dot com
twitter.com/sleberknight www.sleberknight.com/blog
www.nearinfinity.com/blogs/scott_leberknight/all/
scott dot leberknight at gmail dot com