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

dev.fitko.fitconnect.api.config.Version Maven / Gradle / Ivy

Go to download

Library that provides client access to the FIT-Connect api-endpoints for sending, subscribing and routing

The newest version!
package dev.fitko.fitconnect.api.config;

import java.util.Arrays;

import static java.lang.String.valueOf;

/**
 * Version class to compare SemVer versions.
 */
public final class Version {

    public static final char VERSION_SEPARATOR_CHAR = '.';
    public static final String VERSION_SEPARATOR_STRING = "\\.";

    private final int major;
    private final int minor;
    private final int patch;

    public Version(String version) {
        final int[] components = getComponents(version);
        this.major = components[0];
        this.minor = components[1];
        this.patch = components[2];
    }

    public Version(int major, int minor, int patch) {
        this.major = major;
        this.minor = minor;
        this.patch = patch;
    }

    /**
     * Gets the original version as string
     *
     * @return version with major, minor and patch group
     */
    public String getVersionAsString() {
        return major + valueOf(VERSION_SEPARATOR_CHAR) + minor + VERSION_SEPARATOR_CHAR + patch;
    }

    /**
     * Test if the version is greater than a given other version.
     *
     * @param otherVersion the other version to compare with
     * @return true if version > otherVersion
     */
    public boolean isGreaterThan(Version otherVersion) {

        if (this.major > otherVersion.major) {
            return true;
        } else if (this.major < otherVersion.major) {
            return false;
        }

        if (this.minor > otherVersion.minor) {
            return true;
        } else if (this.minor < otherVersion.minor) {
            return false;
        }

        if (this.patch > otherVersion.patch) {
            return true;
        } else if (this.patch < otherVersion.patch) {
            return false;
        }

        return false;
    }

    /**
     * Test if the version is greater or equal than a given other version.
     *
     * @param otherVersion the other version to compare with
     * @return true if version >= otherVersion
     */
    public boolean isGreaterOrEqualThan(Version otherVersion) {
        return isGreaterThan(otherVersion) || isEqualTo(otherVersion);
    }

    private int[] getComponents(String version) {
        long dotCount = version.chars().filter(c -> c == VERSION_SEPARATOR_CHAR).count();
        if (dotCount != 2) {
            throw new IllegalArgumentException("Version " + version + " does not contain major, minor, patch version");
        }
        return Arrays.stream(version.split(VERSION_SEPARATOR_STRING))
                .mapToInt(Integer::parseInt)
                .toArray();
    }
    private boolean isEqualTo(Version otherVersion) {
        return this.major == otherVersion.major &&
                this.minor == otherVersion.minor &&
                this.patch == otherVersion.patch;
    }

    @Override
    public String toString() {
        return getVersionAsString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy