au.net.causal.maven.plugins.boxdb.db.VersionSwitchingDatabaseFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of boxdb-maven-plugin Show documentation
Show all versions of boxdb-maven-plugin Show documentation
Maven plugin to start databases using Docker and VMs
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 extends BoxDatabaseFactory> 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 extends BoxDatabaseFactory> 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;
}
}