58696
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package jdk.jpackage.internal;
|
|
27 |
|
|
28 |
import java.util.ArrayList;
|
|
29 |
import java.util.List;
|
|
30 |
import java.util.Objects;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* Dotted numeric version string.
|
|
34 |
* E.g.: 1.0.37, 10, 0.5
|
|
35 |
*/
|
|
36 |
class DottedVersion implements Comparable<String> {
|
|
37 |
|
|
38 |
public DottedVersion(String version) {
|
|
39 |
components = parseVersionString(version);
|
|
40 |
value = version;
|
|
41 |
}
|
|
42 |
|
|
43 |
@Override
|
|
44 |
public int compareTo(String o) {
|
|
45 |
int result = 0;
|
|
46 |
int[] otherComponents = parseVersionString(o);
|
|
47 |
for (int i = 0; i < Math.min(components.length, otherComponents.length)
|
|
48 |
&& result == 0; ++i) {
|
|
49 |
result = components[i] - otherComponents[i];
|
|
50 |
}
|
|
51 |
|
|
52 |
if (result == 0) {
|
|
53 |
result = components.length - otherComponents.length;
|
|
54 |
}
|
|
55 |
|
|
56 |
return result;
|
|
57 |
}
|
|
58 |
|
|
59 |
private static int[] parseVersionString(String version) {
|
|
60 |
Objects.requireNonNull(version);
|
|
61 |
if (version.isEmpty()) {
|
|
62 |
throw new IllegalArgumentException("Version may not be empty string");
|
|
63 |
}
|
|
64 |
|
|
65 |
int lastNotZeroIdx = -1;
|
|
66 |
List<Integer> components = new ArrayList<>();
|
|
67 |
for (var component : version.split("\\.", -1)) {
|
|
68 |
if (component.isEmpty()) {
|
|
69 |
throw new IllegalArgumentException(String.format(
|
|
70 |
"Version [%s] contains a zero lenght component", version));
|
|
71 |
}
|
|
72 |
|
|
73 |
int num = Integer.parseInt(component);
|
|
74 |
if (num < 0) {
|
|
75 |
throw new IllegalArgumentException(String.format(
|
|
76 |
"Version [%s] contains invalid component [%s]", version,
|
|
77 |
component));
|
|
78 |
}
|
|
79 |
|
|
80 |
if (num != 0) {
|
|
81 |
lastNotZeroIdx = components.size();
|
|
82 |
}
|
|
83 |
components.add(num);
|
|
84 |
}
|
|
85 |
|
|
86 |
if (lastNotZeroIdx + 1 != components.size()) {
|
|
87 |
// Strip trailing zeros.
|
|
88 |
components = components.subList(0, lastNotZeroIdx + 1);
|
|
89 |
}
|
|
90 |
|
|
91 |
return components.stream().mapToInt(Integer::intValue).toArray();
|
|
92 |
}
|
|
93 |
|
|
94 |
@Override
|
|
95 |
public String toString() {
|
|
96 |
return value;
|
|
97 |
}
|
|
98 |
|
|
99 |
final private int[] components;
|
|
100 |
final private String value;
|
|
101 |
}
|