org.n3r.eql.map.MapperFactoryCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eql Show documentation
Show all versions of eql Show documentation
a simple wrapper framework for jdbc to seperate sql and java code
package org.n3r.eql.map;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Sets;
import lombok.val;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
public class MapperFactoryCache {
private static LoadingCache, Optional> fromDbMappers =
CacheBuilder.newBuilder().build(new CacheLoader, Optional>() {
@Override public Optional load(Class> aClass) {
for (val mapper : fromDbMapperSet) {
if (mapper.support(aClass)) return Optional.of(mapper);
}
return Optional.empty();
}
});
private static LoadingCache, Optional> toDbMappers =
CacheBuilder.newBuilder().build(new CacheLoader, Optional>() {
@Override public Optional load(Class> aClass) {
for (val mapper : toDbMapperSet) {
if (mapper.support(aClass)) return Optional.of(mapper);
}
return Optional.empty();
}
});
private static Set fromDbMapperSet = Sets.newHashSet();
private static Set toDbMapperSet = Sets.newHashSet();
static {
for (val mapperFactory : ServiceLoader.load(MapperFactory.class)) {
mapperFactory.addFromDbMapper(fromDbMapperSet);
mapperFactory.addToDbMapper(toDbMapperSet);
}
}
public static Optional getFromDbMapper(Class> aClass) {
return fromDbMappers.getUnchecked(aClass);
}
public static Optional getToDbMapper(Class> aClass) {
return toDbMappers.getUnchecked(aClass);
}
}