net.java.ao.schema.CachingTableNameConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of activeobjects-core Show documentation
Show all versions of activeobjects-core Show documentation
This is the core library for Active Objects. It is generic and can be embedded in any environment.
As such it is generic and won't contain all connection pooling, etc.
package net.java.ao.schema;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import net.java.ao.RawEntity;
import java.util.Objects;
/**
* A table name converter that simply caches the converted table names.
* This implementation uses a {@link com.google.common.cache.LoadingCache} and is thread safe.
*
* @since 0.9
*/
public class CachingTableNameConverter implements TableNameConverter {
private final LoadingCache>, String> cache;
public CachingTableNameConverter(final TableNameConverter delegateTableNameConverter) {
Objects.requireNonNull(delegateTableNameConverter, "delegateTableNameConverter can't be null");
this.cache = CacheBuilder.newBuilder().build(new CacheLoader>, String>() {
@Override
public String load(final Class extends RawEntity>> key) throws Exception {
return delegateTableNameConverter.getName(key);
}
});
}
public String getName(Class extends RawEntity>> entityClass) {
return cache.getUnchecked(entityClass);
}
}