forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVersion.java
182 lines (151 loc) · 5.6 KB
/
Version.java
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
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.gradle;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Encapsulates comparison and printing logic for an x.y.z version.
*/
public final class Version implements Comparable<Version> {
private final int major;
private final int minor;
private final int revision;
private final int id;
// used to identify rebase to OpenSearch 1.0.0
public static final int MASK = 0x08000000;
/**
* Specifies how a version string should be parsed.
*/
public enum Mode {
/**
* Strict parsing only allows known suffixes after the patch number: "-alpha", "-beta" or "-rc". The
* suffix "-SNAPSHOT" is also allowed, either after the patch number, or after the other suffices.
*/
STRICT,
/**
* Relaxed parsing allows any alphanumeric suffix after the patch number.
*/
RELAXED
}
private static final Pattern pattern = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(-alpha\\d+|-beta\\d+|-rc\\d+)?(-SNAPSHOT)?");
private static final Pattern relaxedPattern = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(-[a-zA-Z0-9_]+|\\+[a-zA-Z0-9_]+)*?");
public Version(int major, int minor, int revision) {
Objects.requireNonNull(major, "major version can't be null");
Objects.requireNonNull(minor, "minor version can't be null");
Objects.requireNonNull(revision, "revision version can't be null");
this.major = major;
this.minor = minor;
this.revision = revision;
// currently snapshot is not taken into account
int id = major * 10000000 + minor * 100000 + revision * 1000;
// identify if new OpenSearch version 1
this.id = major == 1 || major == 2 || major == 3 ? id ^ MASK : id;
}
private static int parseSuffixNumber(String substring) {
if (substring.isEmpty()) {
throw new IllegalArgumentException("Invalid suffix, must contain a number e.x. alpha2");
}
return Integer.parseInt(substring);
}
public static Version fromString(final String s) {
return fromString(s, Mode.STRICT);
}
public static Version fromString(final String s, final Mode mode) {
Objects.requireNonNull(s);
Matcher matcher = mode == Mode.STRICT ? pattern.matcher(s) : relaxedPattern.matcher(s);
if (matcher.matches() == false) {
String expected = mode == Mode.STRICT
? "major.minor.revision[-(alpha|beta|rc)Number][-SNAPSHOT]"
: "major.minor.revision[-extra]";
throw new IllegalArgumentException("Invalid version format: '" + s + "'. Should be " + expected);
}
return new Version(Integer.parseInt(matcher.group(1)), parseSuffixNumber(matcher.group(2)), parseSuffixNumber(matcher.group(3)));
}
@Override
public String toString() {
return String.valueOf(getMajor()) + "." + String.valueOf(getMinor()) + "." + String.valueOf(getRevision());
}
public boolean before(Version compareTo) {
return id < compareTo.getId();
}
public boolean before(String compareTo) {
return before(fromString(compareTo));
}
public boolean onOrBefore(Version compareTo) {
return id <= compareTo.getId();
}
public boolean onOrBefore(String compareTo) {
return onOrBefore(fromString(compareTo));
}
public boolean onOrAfter(Version compareTo) {
return id >= compareTo.getId();
}
public boolean onOrAfter(String compareTo) {
return onOrAfter(fromString(compareTo));
}
public boolean after(Version compareTo) {
return id > compareTo.getId();
}
public boolean after(String compareTo) {
return after(fromString(compareTo));
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Version version = (Version) o;
return major == version.major && minor == version.minor && revision == version.revision;
}
@Override
public int hashCode() {
return Objects.hash(major, minor, revision, id);
}
public int getMajor() {
return major;
}
public int getMinor() {
return minor;
}
public int getRevision() {
return revision;
}
protected int getId() {
return id;
}
@Override
public int compareTo(Version other) {
return Integer.compare(getId(), other.getId());
}
}