forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersion.java
More file actions
116 lines (94 loc) · 3.25 KB
/
Copy pathVersion.java
File metadata and controls
116 lines (94 loc) · 3.25 KB
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
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC 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.
package org.openqa.selenium.docker;
import org.openqa.selenium.internal.Require;
/**
* Models simple version strings. Does not attempt to be semver compatible,
* and gleefully fails to handle non-numerical values, except accidentally.
* This is sufficient for handling the version strings we care about from
* Docker.
*/
class Version {
private final String versionString;
private final String[] segments;
public Version(String versionString) {
this.versionString = Require.nonNull("Version string", versionString);
this.segments = versionString.split("\\.");
}
public boolean equalTo(Version other) {
int max = Math.max(segments.length, other.segments.length);
for (int i = 0; i < max; i++) {
if (compare(segments, other.segments, i) != 0) {
return false;
}
}
return true;
}
public boolean isLessThan(Version other) {
int max = Math.max(segments.length, other.segments.length);
for (int i = 0; i < max; i++) {
if (compare(segments, other.segments, i) > 0) {
return false;
}
}
return !equalTo(other);
}
public boolean isGreaterThan(Version other) {
int max = Math.max(segments.length, other.segments.length);
for (int i = 0; i < max; i++) {
if (compare(segments, other.segments, i) < 0) {
return false;
}
}
return !equalTo(other);
}
public String toString() {
return versionString;
}
// Returns a negative integer, zero, or a positive integer as the first
// argument is less than, equal to, or greater than the second. We attempt
// numerical comparisons first, and then a lexical comparison
private int compare(String[] ours, String[] theirs, int index) {
try {
long mine = index < ours.length ? Long.parseLong(ours[index]) : 0L;
long others = index < theirs.length ? Long.parseLong(theirs[index]) : 0L;
return Long.compare(mine, others);
} catch (NumberFormatException e) {
String mine = index < ours.length ? ours[index] : "";
if (mine == null) {
mine = "";
}
String others = index < theirs.length ? theirs[index] : "";
if (others == null) {
others = "";
}
return mine.compareTo(others);
}
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Version)) {
return false;
}
Version that = (Version) o;
return this.equalTo(that);
}
@Override
public int hashCode() {
return versionString.hashCode();
}
}