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 Show documentation
Show all versions of activeobjects Show documentation
This is the full Active Objects library, if you don't know which one to use, you probably want this one.
The newest version!
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);
}
}