Using Google Guava’s Objects.ToStringHelper

I have a little trick for Guava’s com.google.common.base.MoreObjects.toStringHelper(). I configured IntelliJ IDEA to use it when auto-generating toString() methods. I assume you can do the same in Eclipse. Here’s how to do it in Intellij: go inside a class hit Alt + Insert to popup the “Generate” menu choose toString() click the “Settings” button go … Read more

Getting default value for primitive types

The Guava Libraries already contains that: http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/base/Defaults.html Calling defaultValue will return the default value for any primitive type (as specified by the JLS), and null for any other type. Use it like so: import com.google.common.base.Defaults; Defaults.defaultValue(Integer.TYPE); //will return 0

What is the difference between google’s ImmutableList and Collections.unmodifiableList ()?

No, the immutability is only applied to the amount and references of the objects in the Collection, and does not address the mutability of objects you put in the Collection. What Immutable list gains over the standard JDK Collections.unmodifiableList is that by using ImmutableList you are guaranteed that the objects referenced, their order and the … Read more

Is there an elegant way to remove nulls while transforming a Collection using Guava?

There’s already a predicate in Predicates that will help you here — Predicates.notNull() — and you can use Iterables.filter() and the fact that Lists.newArrayList() can take an Iterable to clean this up a little more. Collection<String> resourceIds = Lists.newArrayList( Iterables.filter( Iterables.transform(matchingComputers, yourFunction), Predicates.notNull() ) ); If you don’t actually need a Collection, just an Iterable, … Read more

How to avoid caching when values are null?

Just throw some Exception if user is not found and catch it in client code while using get(key) method. new CacheLoader<ObjectId, User>() { @Override public User load(ObjectId k) throws Exception { User u = DataLoader.datastore.find(User.class).field(“_id”).equal(k).get(); if (u != null) { return u; } else { throw new UserNotFoundException(); } } } From CacheLoader.load(K) Javadoc: Returns: … Read more

Should I use Java8/Guava Optional for every method that may return null?

So What’s Wrong with Optional? The question we face is: will JDK 8 Optional objects get rid of null references? And the answer is an emphatic no! So, detractors immediately question its value asking: then what is it good for that we couldn’t already do by other means? Unlike functional languages like SML or Haskell … Read more

How to solve ‘Program type already present: com.google.common.util.concurrent.ListenableFuture’?

In my case, I had to add the following configurations to app’s module build.gradle: configurations { all*.exclude group: ‘com.google.guava’, module: ‘listenablefuture’ } It happens because some dependencies use com.google.guava:guava and com.google.guava:listenablefuture together. It causes a dependency conflict.

Gradle: Override transitive dependency by version classifier

Currently classifiers are not yet taken into account when it comes to resolutionStrategies. A workaround for you might excluding the transitive Guava library when declaring your dependencies and adding the Guava cdi1.0 version explicitly: dependencies { compile (“org.acme:someDependency:1.0”){ exclude group: ‘com.google.guava’, module: ‘guava’ } compile “com.google.guava:guava:15.0:cdi1.0” }

What is the best way to cache single object within fixed timeout?

I’d use Guava’s Suppliers.memoizeWithExpiration(Supplier delegate, long duration, TimeUnit unit) public class JdkVersionService { @Inject private JdkVersionWebService jdkVersionWebService; // No need to check too often. Once a year will be good 🙂 private final Supplier<JdkVersion> latestJdkVersionCache = Suppliers.memoizeWithExpiration(jdkVersionSupplier(), 365, TimeUnit.DAYS); public JdkVersion getLatestJdkVersion() { return latestJdkVersionCache.get(); } private Supplier<JdkVersion> jdkVersionSupplier() { return new Supplier<JdkVersion>() { public … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)