package com.atlassian.stash.internal.db;

import com.atlassian.bitbucket.util.Version;
import com.google.common.collect.Iterables;

import javax.annotation.Nonnull;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * An implementation of {@link Database} which can be populated using fields from JDBC's {@code DatabaseMetaData}.
 *
 * @since 2.2
 */
public class JdbcMetadataDatabase implements Database {

    private final int majorVersion;
    private final int minorVersion;
    private final String name;
    private final Version version;

    public JdbcMetadataDatabase(@Nonnull String name, @Nonnull Version version, int majorVersion, int minorVersion) {
        this.majorVersion = majorVersion;
        this.minorVersion = minorVersion;
        this.name = checkNotNull(name, "name");
        this.version = checkNotNull(version, "version");
    }

    @Override
    public int getMajorVersion() {
        return majorVersion;
    }

    @Override
    public int getMinorVersion() {
        return minorVersion;
    }

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

    @Override
    public int getPatchVersion() {
        return Iterables.get(version.getVersion(), 2, 0);
    }

    @Nonnull
    @Override
    public Version getVersion() {
        return version;
    }
}
