1
+ package org .scm4j .commons ;
2
+
3
+ import org .apache .commons .lang3 .StringUtils ;
4
+
5
+ public class Version {
6
+
7
+ public static final String SNAPSHOT = "-SNAPSHOT" ;
8
+
9
+ private final String minor ;
10
+ private final String prefix ;
11
+ private final String snapshot ;
12
+ private final String patch ;
13
+ private final String verStr ;
14
+ private final boolean isEmpty ;
15
+ private final boolean isSemantic ;
16
+
17
+ public Version (String verStr ) {
18
+ this .verStr = verStr ;
19
+ if (verStr .isEmpty ()) {
20
+ snapshot = "" ;
21
+ prefix = "" ;
22
+ minor = "" ;
23
+ patch = "" ;
24
+ isEmpty = true ;
25
+ isSemantic = false ;
26
+ } else {
27
+ isEmpty = false ;
28
+ if (verStr .contains (SNAPSHOT )) {
29
+ snapshot = SNAPSHOT ;
30
+ verStr = verStr .replace (SNAPSHOT , "" );
31
+ } else {
32
+ snapshot = "" ;
33
+ }
34
+ if (verStr .lastIndexOf ("." ) > 0 ) {
35
+ patch = verStr .substring (verStr .lastIndexOf ("." ) + 1 , verStr .length ());
36
+ verStr = verStr .substring (0 , verStr .lastIndexOf ("." ));
37
+ if (verStr .lastIndexOf ("." ) > 0 ) {
38
+ minor = verStr .substring (verStr .lastIndexOf ("." ) + 1 , verStr .length ());
39
+ } else {
40
+ minor = verStr ;
41
+ }
42
+ prefix = verStr .substring (0 , verStr .lastIndexOf ("." ) + 1 );
43
+ } else {
44
+ prefix = "" ;
45
+ minor = verStr ;
46
+ patch = "" ;
47
+ }
48
+ isSemantic = StringUtils .isNumeric (minor );
49
+ }
50
+ }
51
+
52
+ public String getPatch () {
53
+ return patch ;
54
+ }
55
+
56
+ public String getMinor () {
57
+ return minor ;
58
+ }
59
+
60
+ public String getSnapshot () {
61
+ return snapshot ;
62
+ }
63
+
64
+ @ Override
65
+ public String toString () {
66
+ if (!isSemantic ) {
67
+ return verStr ;
68
+ }
69
+ return prefix + minor + (patch .isEmpty () ? "" : "." + patch ) + snapshot ;
70
+ }
71
+
72
+ public Version toPreviousPatch () {
73
+ if (patch .isEmpty ()) {
74
+ return new Version (prefix + minor + ".0" + snapshot );
75
+ }
76
+ if (!StringUtils .isNumeric (patch )) {
77
+ return this ;
78
+ }
79
+ int patchInt = Integer .parseInt (patch ) - 1 ;
80
+ return new Version (prefix + minor + "." + Integer .toString (patchInt ) + snapshot );
81
+ }
82
+
83
+ public Version toNextPatch () {
84
+ if (patch .isEmpty ()) {
85
+ return new Version (prefix + minor + ".1" + snapshot );
86
+ }
87
+ if (!StringUtils .isNumeric (patch )) {
88
+ return this ;
89
+ }
90
+ int patchInt = Integer .parseInt (patch ) + 1 ;
91
+ return new Version (prefix + minor + "." + Integer .toString (patchInt ) + snapshot );
92
+ }
93
+
94
+ public Version toPreviousMinor () {
95
+ checkSemantic ();
96
+ return new Version (prefix + Integer .toString (Integer .parseInt (minor ) - 1 ) + "." + patch + snapshot );
97
+ }
98
+
99
+ public Version toNextMinor () {
100
+ checkSemantic ();
101
+ return new Version (prefix + Integer .toString (Integer .parseInt (minor ) + 1 ) + "." + patch + snapshot );
102
+ }
103
+
104
+ private void checkSemantic () {
105
+ if (!isSemantic ) {
106
+ throw new IllegalArgumentException ("wrong version" + verStr );
107
+ }
108
+ }
109
+
110
+ @ Override
111
+ public boolean equals (Object obj ) {
112
+ if (this == obj )
113
+ return true ;
114
+ if (obj == null )
115
+ return false ;
116
+ if (getClass () != obj .getClass ())
117
+ return false ;
118
+ Version other = (Version ) obj ;
119
+ if (verStr == null ) {
120
+ if (other .verStr != null )
121
+ return false ;
122
+ } else if (!verStr .equals (other .verStr ))
123
+ return false ;
124
+ return true ;
125
+ }
126
+
127
+ @ Override
128
+ public int hashCode () {
129
+ final int prime = 31 ;
130
+ int result = 1 ;
131
+ result = prime * result + ((verStr == null ) ? 0 : verStr .hashCode ());
132
+ return result ;
133
+ }
134
+
135
+ public Boolean isEmpty () {
136
+ return isEmpty ;
137
+ }
138
+
139
+ public boolean isExact () {
140
+ return isSemantic && !isSnapshot ();
141
+ }
142
+
143
+ public boolean isSnapshot () {
144
+ return !snapshot .isEmpty ();
145
+ }
146
+
147
+ public String toSnapshotString () {
148
+ return prefix + minor + (patch .isEmpty () ? "" : "." + patch ) + SNAPSHOT ;
149
+ }
150
+
151
+ public Version toSnapshot () {
152
+ return new Version (toSnapshotString ());
153
+ }
154
+
155
+ public String toReleaseString () {
156
+ if (!StringUtils .isNumeric (minor )) {
157
+ return verStr ;
158
+ }
159
+ return prefix + minor + (patch .isEmpty () ? "" : "." + patch );
160
+ }
161
+
162
+ public Boolean isGreaterThan (Version other ) {
163
+ if (other .isEmpty ()) {
164
+ return !isEmpty ();
165
+ }
166
+ if (!isSemantic || !StringUtils .isNumeric (getMinor ())) {
167
+ return false ;
168
+ }
169
+ if (!other .isSemantic ()) {
170
+ return true ;
171
+ }
172
+
173
+ int minor = Integer .parseInt (getMinor ());
174
+ int otherMinor = Integer .parseInt (other .getMinor ());
175
+ if (minor > otherMinor ) {
176
+ return true ;
177
+ }
178
+ if (minor < otherMinor ) {
179
+ return false ;
180
+ }
181
+
182
+ if (!StringUtils .isNumeric (getPatch ()) || !StringUtils .isNumeric (other .getPatch ())) {
183
+ return false ;
184
+ }
185
+
186
+ int patch = Integer .parseInt (getPatch ());
187
+ int otherPatch = Integer .parseInt (other .getPatch ());
188
+
189
+ return patch > otherPatch ;
190
+
191
+ }
192
+
193
+ private boolean isSemantic () {
194
+ return isSemantic ;
195
+ }
196
+
197
+ public String getReleaseNoPatchString () {
198
+ return prefix + minor ;
199
+ }
200
+
201
+ public Version toRelease () {
202
+ if (!isSemantic ) {
203
+ return this ;
204
+ }
205
+ return new Version (prefix + minor + (patch .isEmpty () ? "" : "." + patch ));
206
+ }
207
+ }
0 commit comments