-
-
Notifications
You must be signed in to change notification settings - Fork 762
/
Version.java
177 lines (153 loc) · 5.73 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
/* Jackson JSON-processor.
*
* Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi
*/
package com.fasterxml.jackson.core;
import java.util.Objects;
/**
* Object that encapsulates versioning information of a component.
* Version information includes not just version number but also
* optionally group and artifact ids of the component being versioned.
*<p>
* Note that optional group and artifact id properties are new with Jackson 2.0:
* if provided, they should align with Maven artifact information.
*/
public class Version
implements Comparable<Version>, java.io.Serializable
{
private static final long serialVersionUID = 1L;
private final static Version UNKNOWN_VERSION = new Version(0, 0, 0, null, null, null);
protected final int _majorVersion;
protected final int _minorVersion;
protected final int _patchLevel;
protected final String _groupId;
protected final String _artifactId;
/**
* Additional information for snapshot versions; null for non-snapshot
* (release) versions.
*/
protected final String _snapshotInfo;
/**
* @param major Major version number
* @param minor Minor version number
* @param patchLevel patch level of version
* @param snapshotInfo Optional additional string qualifier
*
* @since 2.1
* @deprecated Use variant that takes group and artifact ids
*/
@Deprecated
public Version(int major, int minor, int patchLevel, String snapshotInfo)
{
this(major, minor, patchLevel, snapshotInfo, null, null);
}
public Version(int major, int minor, int patchLevel, String snapshotInfo,
String groupId, String artifactId)
{
_majorVersion = major;
_minorVersion = minor;
_patchLevel = patchLevel;
_snapshotInfo = snapshotInfo;
_groupId = (groupId == null) ? "" : groupId;
_artifactId = (artifactId == null) ? "" : artifactId;
}
/**
* Method returns canonical "not known" version, which is used as version
* in cases where actual version information is not known (instead of null).
*
* @return Version instance to use as a placeholder when actual version is not known
* (or not relevant)
*/
public static Version unknownVersion() { return UNKNOWN_VERSION; }
/**
* @return {@code True} if this instance is the one returned by
* call to {@link #unknownVersion()}
*
* @since 2.7 to replace misspelled {@link #isUknownVersion()}
*/
public boolean isUnknownVersion() { return (this == UNKNOWN_VERSION); }
public boolean isSnapshot() {
return (_snapshotInfo != null) && (_snapshotInfo.length() > 0);
}
/**
* @return {@code True} if this instance is the one returned by
* call to {@link #unknownVersion()}
*
* @deprecated Since 2.7 use correctly spelled method {@link #isUnknownVersion()}
*/
@Deprecated
public boolean isUknownVersion() { return isUnknownVersion(); }
public int getMajorVersion() { return _majorVersion; }
public int getMinorVersion() { return _minorVersion; }
public int getPatchLevel() { return _patchLevel; }
public String getGroupId() { return _groupId; }
public String getArtifactId() { return _artifactId; }
public String toFullString() {
return _groupId + '/' + _artifactId + '/' + toString();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(_majorVersion).append('.');
sb.append(_minorVersion).append('.');
sb.append(_patchLevel);
if (isSnapshot()) {
sb.append('-').append(_snapshotInfo);
}
return sb.toString();
}
@Override
public int hashCode() {
return _artifactId.hashCode() ^ _groupId.hashCode()
^ Objects.hashCode(_snapshotInfo)
+ _majorVersion - _minorVersion + _patchLevel;
}
@Override
public boolean equals(Object o)
{
if (o == this) return true;
if (o == null) return false;
if (o.getClass() != getClass()) return false;
Version other = (Version) o;
return (other._majorVersion == _majorVersion)
&& (other._minorVersion == _minorVersion)
&& (other._patchLevel == _patchLevel)
&& Objects.equals(other._snapshotInfo, _snapshotInfo)
&& other._artifactId.equals(_artifactId)
&& other._groupId.equals(_groupId)
;
}
@Override
public int compareTo(Version other)
{
if (other == this) return 0;
int diff = _groupId.compareTo(other._groupId);
if (diff == 0) {
diff = _artifactId.compareTo(other._artifactId);
if (diff == 0) {
diff = _majorVersion - other._majorVersion;
if (diff == 0) {
diff = _minorVersion - other._minorVersion;
if (diff == 0) {
diff = _patchLevel - other._patchLevel;
if (diff == 0) {
// Snapshot: non-snapshot AFTER snapshot, otherwise alphabetical
if (isSnapshot()) {
if (other.isSnapshot()) {
diff = _snapshotInfo.compareTo(other._snapshotInfo);
} else {
diff = -1;
}
} else if (other.isSnapshot()) {
diff = 1;
} else {
diff = 0;
}
}
}
}
}
}
return diff;
}
}