All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.n3r.eql.map.MapperFactoryCache Maven / Gradle / Ivy

There is a newer version: 0.1.19
Show newest version
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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy