Showing posts with label guava. Show all posts
Showing posts with label guava. Show all posts

Thursday, October 18, 2012

Getting unchecked result of Future with Guava

If you like Java Executor Framework as much as I do (and hate checked exceptions as much as I do), then you probably start to swear each time you need to handle all these annoying checked exceptions of Future#get.
Future<MyMessage> response =
  camelProducerTemplate.asyncRequestBody(
    "http://veryslow.com/esb/service",
    "request", MyMessage.class);
...
// Do something else waiting for the HTTP response.
...
try {
  handleResponse(response.get());
} catch (InterruptedException e) {
  throw new RuntimeException(e);
} catch (ExecutionException e) {
  throw new RuntimeException(e);
}
Handling checked exceptions of Future is especially annoying, as Executor Framework is widely used when implementing popular Request-Reply pattern in messaging systems.

Fortunately Guava's Futures utility provides glue code for handling Future#get exceptions. This extremely useful method is called getUnchecked.
import static com.google.common.util.concurrent.Futures.getUnchecked;
...
Future<MyMessage> response =
  camelProducerTemplate.asyncRequestBody(
    "http://veryslow.com/esb/service",
    "request", MyMessage.class);
...
// Do something else waiting for the HTTP response.
...
handleResponse(getUnchecked(response));

Thank you Google.

Sunday, July 4, 2010

Hello Huava

Guava is cool. Nice idea to have all the missing Java utils in one place.

However Google guys don't included in Guava all the utils I dream of. That's why I created Huava - project where I put my magic utils designed in the fashion similar to Guava.

Iterable transformations using Guava

I love Groovy for spread operator (for example users*.name). Google Guava allows you to do the same using the Function interface and Iterables.transform() method.

Function<Date, Integer> YEAR = new Function<Date, Integer>() {

  public Integer apply(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.YEAR);
  }

};

Date[] dates = {new Date(), new Date(), new Date()};
Iterable<Integer> yearsIterable = Iterables.transform(Lists.newArrayList(dates), YEAR);
List<Integer> years = Lists.newArrayList(yearsIterable);

System.out.println(years);

Saturday, July 3, 2010

Guava Files util - my top methods

Guava makes Java code more Groovy-like. Now say hello to the Spring IO utils and Ant. Introducing com.google.common.io.Files:

// Just write this fuckin' String to the file, man...
static void write(CharSequence from, File to, Charset charset)
static void append(CharSequence from, File to, Charset charset)

// File system operations
static void copy(File from, File to) 
static void deleteDirectoryContents(File directory)
static void deleteRecursively(File file)
static void move(File from, File to) 
static void touch(File file)

// No more annoying Java IO
static void createParentDirs(File file)
static File createTempDir()
static BufferedReader newReader(File file, Charset charset)
static BufferedWriter newWriter(File file, Charset charset) 
static List<String> readLines(File file, Charset charset)

Guava Object helpers

Say hello to the Spring utils.

import com.google.common.base.Objects;

// Null-safe equals
Objects.equal("foo", null);

// Choose first not-null or throw NPE
Objects.firstNonNull("foo", null);

// Helper for Arrays.hashCode()
Objects.hashCode(1, "foo", null);

// ToStringHelper which generates - String{foo, bar, baz=qux}
String toStringTarget = "foo";
Objects.toStringHelper(toStringTarget).
   addValue(toStringTarget).addValue("bar").add("baz", "qux");