This is a mirror of official site: http://jasper-net.blogspot.com/

Google’s guava java: the easy parts

| Sunday, October 17, 2010
Google’s guava is one of the best, most useful all-purpose java libraries to come out in a long time. It’s surprising that it isn’t standard in most projects now. Maybe there’s too much in there. If the functional and concurrency packages seem like too big an investment of time to understand and use in your code, just focus first on the easy parts. Think of guava as the next generation of apache commons utilities, with lots more stuff if and when you want to explore it.

Here are some of my favorite simple, yet handy routines in guava:

List<String> lines = Files.readLines(file, charset);

This is the same as FileUtils.readLines() in apache commons io, but updated to use generics. Files doesn’t quite have all the things that FileUtils has, but there are many other helpful methods like deleteRecursively(), createParentsDir(), and move().

boolean isEmpty = Strings.isNullOrEmpty(yourString);

You’ve probably written something like this already, perhaps even several versions scattered in different projects. Why not just use one from an established api? You might also find emptyToNull() or repeat() useful while you’re looking around the Strings class.

Preconditions.checkArgument(boolean expression);

Read more: Coppery Keen Claws

Posted via email from .NET Info

0 comments: