Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Supplier instead of Callable to get objects as lazy #771

Merged
merged 1 commit into from
Sep 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package br.com.caelum.vraptor.cache;

import java.util.concurrent.Callable;
import com.google.common.base.Supplier;

/**
*
Expand Down Expand Up @@ -51,6 +51,6 @@ public interface CacheStore<K,V> {
* @param valueProvider
* @return the stored or the new value for the provided key.
*/
public V fetch(K key, Callable<V> valueProvider);
public V fetch(K key, Supplier<V> valueProvider);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@

import static com.google.common.base.Throwables.propagateIfPossible;

import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Default;

import com.google.common.base.Supplier;

@Default
@Dependent
public class DefaultCacheStore<K,V> implements CacheStore<K,V> {

private final ConcurrentMap<K,V> cache = new ConcurrentHashMap<>();

@Override
public V fetch(K key, Callable<V> valueProvider) {
public V fetch(K key, Supplier<V> valueProvider) {
if (!cache.containsKey(key)){
try {
V value = valueProvider.call();
V value = valueProvider.get();
cache.put(key, value);
return value;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Callable;

import javax.enterprise.inject.Vetoed;

import com.google.common.base.Supplier;

/**
* A LRU cache based on LinkedHashMap.
*
Expand All @@ -47,10 +48,10 @@ protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
}

@Override
public V fetch(K key, Callable<V> valueProvider) {
public V fetch(K key, Supplier<V> valueProvider) {
if (!this.containsKey(key)){
try {
V value = valueProvider.call();
V value = valueProvider.get();
put(key, value);
return value;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.google.common.base.Preconditions.checkState;

import java.util.LinkedList;
import java.util.concurrent.Callable;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
Expand All @@ -36,6 +35,7 @@
import br.com.caelum.vraptor.ioc.Container;

import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;

@ApplicationScoped
Expand Down Expand Up @@ -81,9 +81,9 @@ public <T> Converter<T> to(Class<T> clazz) {
}

private Class<? extends Converter<?>> findConverterType(final Class<?> clazz) {
return cache.fetch(clazz, new Callable<Class<? extends Converter<?>>>() {
return cache.fetch(clazz, new Supplier<Class<? extends Converter<?>>>() {
@Override
public Class<? extends Converter<?>> call() throws Exception {
public Class<? extends Converter<?>> get() {
return FluentIterable.from(classes).filter(matchConverter(clazz))
.first().or(NullConverter.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package br.com.caelum.vraptor.core;

import java.util.concurrent.Callable;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

Expand All @@ -30,6 +28,8 @@
import br.com.caelum.vraptor.interceptor.StepInvoker;
import br.com.caelum.vraptor.ioc.Container;

import com.google.common.base.Supplier;

/**
* @author Lucas Cavalcanti
* @author Alberto Souza
Expand Down Expand Up @@ -67,9 +67,9 @@ public DefaultInterceptorHandlerFactory(Container container, StepInvoker stepInv

@Override
public InterceptorHandler handlerFor(final Class<?> type) {
return cachedHandlers.fetch(type, new Callable<InterceptorHandler>() {
return cachedHandlers.fetch(type, new Supplier<InterceptorHandler>() {
@Override
public InterceptorHandler call() throws Exception {
public InterceptorHandler get() {
if(type.isAnnotationPresent(Intercepts.class) && !Interceptor.class.isAssignableFrom(type)){
return new AspectStyleInterceptorHandler(type, stepInvoker, container, customAcceptsExecutor,
acceptsExecutor, interceptorExecutor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
Expand All @@ -44,6 +43,7 @@
import br.com.caelum.vraptor.proxy.Proxifier;

import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;

/**
Expand Down Expand Up @@ -162,9 +162,9 @@ public <T> String urlFor(final Class<T> type, final Method method, Object... par
final Class<?> rawtype = proxifier.isProxyType(type) ? type.getSuperclass() : type;
final Invocation invocation = new Invocation(rawtype, method);

Route route = cache.fetch(invocation, new Callable<Route>() {
Route route = cache.fetch(invocation, new Supplier<Route>() {
@Override
public Route call() throws Exception {
public Route get() {
return FluentIterable.from(routes).filter(canHandle(rawtype, method))
.first().or(NULL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -31,6 +30,7 @@
import br.com.caelum.vraptor.cache.LRU;

import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;

/**
Expand Down Expand Up @@ -78,9 +78,9 @@ public String getFormat(final String acceptHeader) {
return DEFAULT_FORMAT;
}

return acceptToFormatCache.fetch(acceptHeader, new Callable<String>() {
return acceptToFormatCache.fetch(acceptHeader, new Supplier<String>() {
@Override
public String call() throws Exception {
public String get() {
return chooseMimeType(acceptHeader);
}
});
Expand Down