001package org.dcm4che3.conf.api.upgrade;
002
003import java.util.regex.Matcher;
004import java.util.regex.Pattern;
005
006/**
007 * Major-minor-patch version
008 */
009public class MMPVersion implements Comparable<MMPVersion> {
010
011    /**
012     * Matches both 8.2.1-02 and 8.1-33
013     * ignores the .1 in  8.2.1-02
014     */
015    private static Pattern stringVersionFormat = Pattern.compile("([0-9]+)\\.([0-9]+)(\\.[0-9]+)?(-([0-9]+))?");
016    //  "(?<major>[0-9]+)\\.(?<minor>[0-9]+)(\\.([0-9]+))?-(?<patch>[0-9]+)");
017
018    private int major;
019    private int minor;
020    private int patch;
021
022
023    public static MMPVersion fromFixUpToAnno(FixUpTo anno) throws IllegalArgumentException {
024        return fromAnnotatedVersion(anno.value(), anno.major(), anno.minor(), anno.patch());
025    }
026
027    public static MMPVersion fromScriptVersionAnno(ScriptVersion anno) throws IllegalArgumentException {
028        return fromAnnotatedVersion(anno.value(), anno.major(), anno.minor(), anno.patch());
029    }
030
031    public static MMPVersion fromStringVersion(String value) throws IllegalArgumentException {
032        MMPVersion mmpVersion = new MMPVersion();
033        Matcher matcher = stringVersionFormat.matcher(value);
034        if (!matcher.matches())
035            throw new IllegalArgumentException("Unexpected version format: " + value);
036
037        mmpVersion.major = Integer.parseInt(matcher.group(1));
038        mmpVersion.minor = Integer.parseInt(matcher.group(2));
039        String group = matcher.group(5);
040        if (group != null) {
041            mmpVersion.patch = Integer.parseInt(group);
042        }
043        return mmpVersion;
044    }
045
046    private static MMPVersion fromAnnotatedVersion(String value, int major, int minor, int patch) {
047        if (value.isEmpty()) {
048            MMPVersion mmpVersion = new MMPVersion();
049            mmpVersion.major = major;
050            mmpVersion.minor = minor;
051            mmpVersion.patch = patch;
052
053            if (mmpVersion.major < 0 || mmpVersion.minor < 0 || mmpVersion.patch < 0)
054                throw new IllegalArgumentException("Major, minor, and patch values of a version must not be negative - violated by " + mmpVersion);
055            return mmpVersion;
056
057        } else {
058            return fromStringVersion(value);
059        }
060    }
061
062    public int getMajor() {
063        return major;
064    }
065
066    public void setMajor(int major) {
067        this.major = major;
068    }
069
070    public int getMinor() {
071        return minor;
072    }
073
074    public void setMinor(int minor) {
075        this.minor = minor;
076    }
077
078    public int getPatch() {
079        return patch;
080    }
081
082    public void setPatch(int patch) {
083        this.patch = patch;
084    }
085
086    @Override
087    public String toString() {
088        return major + "." + minor + "-" + patch;
089    }
090
091    @Override
092    public int compareTo(MMPVersion otherVersion) {
093        if (major < otherVersion.major) return -1;
094        if (major > otherVersion.major) return 1;
095        if (minor < otherVersion.minor) return -1;
096        if (minor > otherVersion.minor) return 1;
097        if (patch < otherVersion.patch) return -1;
098        if (patch > otherVersion.patch) return 1;
099        return 0;
100    }
101
102}