aboutsummaryrefslogtreecommitdiffstats
path: root/config-provisioning/src/main/java/com/yahoo/config/provision/Version.java
blob: bd6e7d628335c577d136029244f9595257f60625 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.provision;

/**
 * The {@link Version} class is used in providing versioned config for applications.
 *
 * A {@link Version} object has three components:
 *
 * * Major version. A non-negative integer.
 * * Minor version. A non-negative integer.
 * * Micro version. A non-negative integer.
 *
 * @author Vegard Sjonfjell
 * @since 5.39
 * Loosely based on component/Version.java
 * {@link Version} objects are immutable.
 * @deprecated use com.yahoo.component.Version
 */
// TODO: Replace usage of this by com.yahoo.component.Version
@Deprecated
public final class Version implements Comparable<Version> {

    private final int major;
    private final int minor;
    private final int micro;
    private final String stringValue;

    /**
     * @see #fromIntValues
     */
    private Version(int major, int minor, int micro) {
        this.major = major;
        this.minor = minor;
        this.micro = micro;
        stringValue = toSerializedForm();
        verify();
    }

    /**
     * @see #fromString
     */
    private Version(String versionString) {
        try {
            String[] components = versionString.split("\\.", 3);
            assert (components.length == 3);
            major = Integer.parseInt(components[0]);
            minor = Integer.parseInt(components[1]);
            micro = Integer.parseInt(components[2]);
            stringValue = toSerializedForm();
            verify();
        } catch (AssertionError | ArrayIndexOutOfBoundsException | IllegalArgumentException e) {
            throw new IllegalArgumentException(String.format("Invalid version specification: \"%s\": %s", versionString, e.getMessage()));
        }
    }

    public com.yahoo.component.Version toVersion() {
        return new com.yahoo.component.Version(major, minor, micro);
    }

    /**
     * Verifies that the numerical components in a version are legal.
     * Must be called on construction after the component values are set
     *
     * @throws IllegalArgumentException if one of the numerical components are negative.
     */
    private void verify() {
        if (major < 0)
            throw new IllegalArgumentException("Negative major value");
        if (minor < 0)
            throw new IllegalArgumentException("Negative minor value");
        if (micro < 0)
            throw new IllegalArgumentException("Negative micro value");
    }

    public String toSerializedForm() {
        return String.format("%d.%d.%d", major, minor, micro);
    }

    /**
     * Creates a {@link Version} object from the specified components.
     *
     * @param major major component of the version identifier.
     * @param minor minor component of the version identifier.
     * @param micro micro component of the version identifier.
     * @throws IllegalArgumentException if one of the numerical components are negative.
     * @return {@link Version} identifier object constructed from integer components.
     */
    public static Version fromIntValues(int major, int minor, int micro) {
        return new Version(major, minor, micro);
    }

    /**
     * Creates a version object from the specified string. Version strings are in the format major.minor.micro
     *
     * @param versionString String representation of the version.
     * @throws IllegalArgumentException If version string is improperly formatted.
     * @return {@link Version} object constructed from string representation.
     */
    public static Version fromString(String versionString) {
        return new Version(versionString);
    }

    public static Version from(com.yahoo.component.Version version) {
        return new Version(version.getMajor(), version.getMinor(), version.getMicro());
    }

    /**
     * Returns a string representation of this version identifier, encoded as major.minor.micro
     */
    public String toString() { return stringValue; }

    /**
     * Returns major version component
     */
    public int getMajor() { return major; }

    /**
     * Returns minor version component
     */
    public int getMinor() { return minor; }

    /**
     * Returns micro version component
     */
    public int getMicro() { return micro; }

    @Override
    public int hashCode() { return stringValue.hashCode(); }

    /**
     * Performs an equality test between this {@link Version} object and another.
     *
     * A version is considered to be equal to another version if the
     * major, minor and micro components are equal.
     *
     * @param object The {@link Version} object to be compared to this version.
     * @return <code>true</code> if object is a
     *         {@link Version} and is equal to this object;
     *         <code>false</code> otherwise.
     */
    @Override
    public boolean equals(Object object) {
        if (object == null || object.getClass() != this.getClass()) {
            return false;
        }

        Version other = (Version)object;
        return this.major == other.major && this.minor == other.minor && this.micro == other.micro;
    }

    /**
     * Compares this {@link Version} object to another.
     *
     * A version is considered to be less than another version if its
     * major component is less than the other version's major component, or the
     * major components are equal and its minor component is less than the other
     * version's minor component, or the major and minor components are equal
     * and its micro component is less than the other version's micro component.
     *
     * A version is considered to be equal to another version if the
     * major, minor and micro components are equal.
     *
     * @param other the {@link Version} object to be compared to this version.
     * @return A negative integer, zero, or a positive integer if this object is
     *         less than, equal to, or greater than the specified {@link Version} object.
     * @throws ClassCastException if the specified object is not a {@link Version}.
     */
    @Override
    public int compareTo(Version other) {
        if (this == other) return 0;

        int comparison = Integer.compare(getMajor(), other.getMajor());
        if (comparison != 0) {
            return comparison;
        }

        comparison = Integer.compare(getMinor(), other.getMinor());
        if (comparison != 0) {
            return comparison;
        }

        return Integer.compare(getMicro(), other.getMicro());
    }
}