humanize.spi.cache.GuavaCacheProvider Maven / Gradle / Ivy
The newest version!
package humanize.spi.cache;
import humanize.config.ConfigLoader;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheBuilderSpec;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
/**
* {@link CacheProvider} implementation that uses Guava caches as in memory
* storage.
*
* @author mfornos
*
*/
public class GuavaCacheProvider implements CacheProvider
{
private static final CacheBuilderSpec spec = initSpec();
private static CacheBuilderSpec initSpec()
{
final Properties properties = ConfigLoader.loadProperties();
return CacheBuilderSpec.parse(properties.getProperty(ConfigLoader.CACHE_BUILDER_SPEC));
}
private final Cache bundles;
private final LoadingCache> formats;
private final LoadingCache> stringCaches;
public GuavaCacheProvider()
{
bundles = CacheBuilder.from(spec). build();
formats = CacheBuilder.from(spec).build(new CacheLoader>()
{
@Override
public Cache load(String cache) throws Exception
{
return CacheBuilder.from(spec). build();
}
});
stringCaches = CacheBuilder.from(spec).build(new CacheLoader>()
{
@Override
public Cache load(String cache) throws Exception
{
return CacheBuilder.from(spec). build();
}
});
}
@Override
public boolean containsBundle(Locale locale)
{
return bundles.getIfPresent(locale) != null;
}
@Override
public boolean containsFormat(String cache, Locale locale)
{
return getFormatsCache(cache).getIfPresent(locale) != null;
}
@Override
public boolean containsStrings(String cache, Locale locale)
{
return getStringCache(cache).getIfPresent(locale) != null;
}
@Override
public ResourceBundle getBundle(Locale locale, Callable getCall)
{
try
{
return bundles.get(locale, getCall);
} catch (ExecutionException e)
{
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
@Override
public T getFormat(String cache, Locale locale, Callable getCall)
{
try
{
return (T) getFormatsCache(cache).get(locale, getCall);
} catch (ExecutionException e)
{
throw new RuntimeException(e);
}
}
@Override
public String[] getStrings(String cache, Locale locale, Callable getCall)
{
try
{
return getStringCache(cache).get(locale, getCall);
} catch (ExecutionException e)
{
throw new RuntimeException(e);
}
}
@Override
public ResourceBundle putBundle(Locale locale, ResourceBundle bundle)
{
bundles.put(locale, bundle);
return bundle;
}
@Override
public T putFormat(String cache, Locale locale, T format)
{
Cache numberFormatCache = getFormatsCache(cache);
numberFormatCache.put(locale, format);
return format;
}
@Override
public String[] putStrings(String cache, Locale locale, String[] value)
{
Cache stringCache = getStringCache(cache);
stringCache.put(locale, value);
return value;
}
@SuppressWarnings("unchecked")
private Cache getFormatsCache(String cache)
{
try
{
return (Cache) formats.get(cache);
} catch (ExecutionException e)
{
throw new RuntimeException(e);
}
}
private Cache getStringCache(String cache)
{
try
{
return stringCaches.get(cache);
} catch (ExecutionException e)
{
throw new RuntimeException(e);
}
}
}