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

au.net.causal.maven.plugins.boxdb.db.VersionSwitchingDatabaseFactory Maven / Gradle / Ivy

There is a newer version: 3.3
Show newest version
package au.net.causal.maven.plugins.boxdb.db;

import com.google.common.collect.ImmutableMap;

import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;

public abstract class VersionSwitchingDatabaseFactory implements BoxDatabaseFactory
{
    private final String name;
    private final Map> versionFactoryMap;
    private final Supplier defaultFactory;

    protected VersionSwitchingDatabaseFactory(String name, String defaultVersion, Map> versionFactoryMap)
    {
        Objects.requireNonNull(name, "name == null");
        Objects.requireNonNull(versionFactoryMap, "versionFactoryMap == null");
        this.name = name;
        this.versionFactoryMap = ImmutableMap.copyOf(versionFactoryMap);
        defaultFactory = versionFactoryMap.get(defaultVersion);
        if (defaultFactory == null)
            throw new IllegalArgumentException("Invalid default version '" + defaultVersion + "' - does not have entry in factory map.");
    }

    @Override
    public BoxDatabase create(BoxConfiguration boxConfiguration, ProjectConfiguration projectConfiguration, BoxContext context)
    throws BoxDatabaseException
    {
        String version = boxConfiguration.getDatabaseVersion();

        if (version == null)
            return defaultFactory.get().create(boxConfiguration, projectConfiguration, context);

        Supplier factory = versionFactoryMap.get(version);
        if (factory == null)
            throw new BoxDatabaseException("Invalid version '" + version + "' for database type '" + name() + "'.  Available versions: " + versionFactoryMap.keySet());

        return factory.get().create(boxConfiguration, projectConfiguration, context);
    }

    @Override
    public String name()
    {
        return name;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy