Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
Openfire
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Openfire
Commits
5df5dab9
Commit
5df5dab9
authored
Nov 17, 2015
by
Tom Evans
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #371 from sco0ter/version
Various improvements in Version class
parents
84b557bd
23c84bf0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
127 additions
and
61 deletions
+127
-61
Version.java
src/java/org/jivesoftware/util/Version.java
+108
-53
VersionTest.java
src/test/java/org/jivesoftware/util/VersionTest.java
+19
-8
No files found.
src/java/org/jivesoftware/util/Version.java
View file @
5df5dab9
...
@@ -19,39 +19,43 @@
...
@@ -19,39 +19,43 @@
package
org
.
jivesoftware
.
util
;
package
org
.
jivesoftware
.
util
;
import
java.util.StringTokenizer
;
import
java.util.Objects
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
/**
/**
* Holds version information for Openfire.
* Holds version information for Openfire.
*
*
* @author Iain Shigeoka
* @author Iain Shigeoka
*/
*/
public
class
Version
implements
Comparable
<
Version
>
{
public
final
class
Version
implements
Comparable
<
Version
>
{
private
static
final
Pattern
PATTERN
=
Pattern
.
compile
(
"(\\d+)\\.(\\d+)\\.(\\d+)(?:\\s+(\\w+))?(?:\\s+(\\d+))?"
);
/**
/**
* The major number (ie 1.x.x).
* The major number (ie 1.x.x).
*/
*/
private
int
major
;
private
final
int
major
;
/**
/**
* The minor version number (ie x.1.x).
* The minor version number (ie x.1.x).
*/
*/
private
int
minor
;
private
final
int
minor
;
/**
/**
* The micro version number (ie x.x.1).
* The micro version number (ie x.x.1).
*/
*/
private
int
micro
;
private
final
int
micro
;
/**
/**
* A status release number or -1 to indicate none.
* A status release number or -1 to indicate none.
*/
*/
private
int
statusVersion
;
private
final
int
statusVersion
;
/**
/**
* The release state of the product (Release, Release Candidate).
* The release state of the product (Release, Release Candidate).
*/
*/
private
ReleaseStatus
status
;
private
final
ReleaseStatus
status
;
/**
/**
* Cached version string information
* Cached version string information
...
@@ -70,7 +74,7 @@ public class Version implements Comparable<Version> {
...
@@ -70,7 +74,7 @@ public class Version implements Comparable<Version> {
this
.
major
=
major
;
this
.
major
=
major
;
this
.
minor
=
minor
;
this
.
minor
=
minor
;
this
.
micro
=
micro
;
this
.
micro
=
micro
;
this
.
status
=
status
;
this
.
status
=
status
==
null
?
ReleaseStatus
.
Release
:
status
;
this
.
statusVersion
=
statusVersion
;
this
.
statusVersion
=
statusVersion
;
}
}
...
@@ -79,23 +83,47 @@ public class Version implements Comparable<Version> {
...
@@ -79,23 +83,47 @@ public class Version implements Comparable<Version> {
*
*
* @param source the version string
* @param source the version string
*/
*/
public
Version
(
String
source
)
{
public
Version
(
CharSequence
source
)
{
// initialize the defaults
if
(
source
!=
null
)
{
major
=
minor
=
micro
=
0
;
Matcher
matcher
=
PATTERN
.
matcher
(
source
);
status
=
ReleaseStatus
.
Release
;
if
(
matcher
.
matches
())
{
statusVersion
=
-
1
;
major
=
Integer
.
parseInt
(
matcher
.
group
(
1
));
minor
=
Integer
.
parseInt
(
matcher
.
group
(
2
));
if
(
source
!=
null
)
{
micro
=
Integer
.
parseInt
(
matcher
.
group
(
3
));
StringTokenizer
parser
=
new
StringTokenizer
(
source
,
"."
);
String
status
=
matcher
.
group
(
4
);
try
{
if
(
status
!=
null
)
{
major
=
parser
.
hasMoreTokens
()
?
Integer
.
parseInt
(
parser
.
nextToken
())
:
0
;
switch
(
status
.
toLowerCase
())
{
minor
=
parser
.
hasMoreTokens
()
?
Integer
.
parseInt
(
parser
.
nextToken
())
:
0
;
case
"rc"
:
micro
=
parser
.
hasMoreTokens
()
?
Integer
.
parseInt
(
parser
.
nextToken
())
:
0
;
this
.
status
=
ReleaseStatus
.
Release_Candidate
;
}
break
;
catch
(
NumberFormatException
nfe
)
{
case
"beta"
:
// ignore bad version
this
.
status
=
ReleaseStatus
.
Beta
;
}
break
;
}
case
"alpha"
:
this
.
status
=
ReleaseStatus
.
Alpha
;
break
;
default
:
this
.
status
=
ReleaseStatus
.
Release
;
}
}
else
{
this
.
status
=
ReleaseStatus
.
Release
;
}
String
statusVersion
=
matcher
.
group
(
5
);
if
(
statusVersion
!=
null
)
{
this
.
statusVersion
=
Integer
.
parseInt
(
statusVersion
);
}
else
{
this
.
statusVersion
=
-
1
;
}
}
else
{
this
.
major
=
this
.
minor
=
this
.
micro
=
0
;
this
.
statusVersion
=
-
1
;
this
.
status
=
ReleaseStatus
.
Release
;
}
}
else
{
this
.
major
=
this
.
minor
=
this
.
micro
=
0
;
this
.
statusVersion
=
-
1
;
this
.
status
=
ReleaseStatus
.
Release
;
}
}
}
/**
/**
...
@@ -106,24 +134,15 @@ public class Version implements Comparable<Version> {
...
@@ -106,24 +134,15 @@ public class Version implements Comparable<Version> {
*/
*/
public
String
getVersionString
()
{
public
String
getVersionString
()
{
if
(
versionString
==
null
)
{
if
(
versionString
==
null
)
{
if
(
status
!=
null
)
{
StringBuilder
sb
=
new
StringBuilder
();
if
(
status
==
ReleaseStatus
.
Release
)
{
sb
.
append
(
major
).
append
(
'.'
).
append
(
minor
).
append
(
'.'
).
append
(
micro
);
versionString
=
major
+
"."
+
minor
+
"."
+
micro
;
if
(
status
!=
ReleaseStatus
.
Release
)
{
sb
.
append
(
' '
).
append
(
status
);
if
(
statusVersion
>=
0
)
{
sb
.
append
(
' '
).
append
(
statusVersion
);
}
}
else
{
if
(
statusVersion
>=
0
)
{
versionString
=
major
+
"."
+
minor
+
"."
+
micro
+
" "
+
status
.
toString
()
+
" "
+
statusVersion
;
}
else
{
versionString
=
major
+
"."
+
minor
+
"."
+
micro
+
" "
+
status
.
toString
();
}
}
}
else
{
versionString
=
major
+
"."
+
minor
+
"."
+
micro
;
}
}
versionString
=
sb
.
toString
();
}
}
return
versionString
;
return
versionString
;
}
}
...
@@ -184,7 +203,7 @@ public class Version implements Comparable<Version> {
...
@@ -184,7 +203,7 @@ public class Version implements Comparable<Version> {
private
String
status
;
private
String
status
;
private
ReleaseStatus
(
String
status
)
{
ReleaseStatus
(
String
status
)
{
this
.
status
=
status
;
this
.
status
=
status
;
}
}
...
@@ -203,15 +222,51 @@ public class Version implements Comparable<Version> {
...
@@ -203,15 +222,51 @@ public class Version implements Comparable<Version> {
return
this
.
compareTo
(
otherVersion
)
>
0
;
return
this
.
compareTo
(
otherVersion
)
>
0
;
}
}
@Override
@Override
public
int
compareTo
(
Version
that
)
{
public
int
compareTo
(
Version
that
)
{
if
(
that
==
null
)
{
if
(
that
==
null
)
{
return
1
;
return
1
;
}
}
int
result
=
Integer
.
compare
(
getMajor
(),
that
.
getMajor
());
long
thisVersion
=
(
this
.
getMicro
()*
10
)
+
(
this
.
getMinor
()*
1000
)
+
(
this
.
getMajor
()*
100000
);
if
(
result
==
0
)
{
long
thatVersion
=
(
that
.
getMicro
()*
10
)
+
(
that
.
getMinor
()*
1000
)
+
(
that
.
getMajor
()*
100000
);
result
=
Integer
.
compare
(
getMinor
(),
that
.
getMinor
());
if
(
result
==
0
)
{
return
thisVersion
==
thatVersion
?
0
:
thisVersion
>
thatVersion
?
1
:
-
1
;
result
=
Integer
.
compare
(
getMicro
(),
that
.
getMicro
());
}
if
(
result
==
0
)
{
result
=
that
.
getStatus
().
compareTo
(
getStatus
());
if
(
result
==
0
)
{
result
=
Integer
.
compare
(
getStatusVersion
(),
that
.
getStatusVersion
());
}
}
}
}
return
result
;
}
@Override
public
boolean
equals
(
Object
o
)
{
if
(
o
==
this
)
{
return
true
;
}
if
(!(
o
instanceof
Version
))
{
return
false
;
}
Version
other
=
(
Version
)
o
;
return
Objects
.
equals
(
major
,
other
.
major
)
&&
Objects
.
equals
(
minor
,
other
.
minor
)
&&
Objects
.
equals
(
micro
,
other
.
micro
)
&&
Objects
.
equals
(
statusVersion
,
other
.
statusVersion
)
&&
Objects
.
equals
(
status
,
other
.
status
);
}
@Override
public
int
hashCode
()
{
return
Objects
.
hash
(
major
,
minor
,
micro
,
statusVersion
,
status
);
}
@Override
public
String
toString
()
{
return
getVersionString
();
}
}
}
src/test/java/org/jivesoftware/util/VersionTest.java
View file @
5df5dab9
...
@@ -25,16 +25,16 @@ public class VersionTest {
...
@@ -25,16 +25,16 @@ public class VersionTest {
@Test
@Test
public
void
testVersionWithRegularStringConstructor
()
{
public
void
testVersionWithRegularStringConstructor
()
{
Version
test
=
new
Version
(
"1.2.3"
);
Version
test
=
new
Version
(
"1.2.3
Beta 3
"
);
assertEquals
(
1
,
test
.
getMajor
());
assertEquals
(
1
,
test
.
getMajor
());
assertEquals
(
2
,
test
.
getMinor
());
assertEquals
(
2
,
test
.
getMinor
());
assertEquals
(
3
,
test
.
getMicro
());
assertEquals
(
3
,
test
.
getMicro
());
assertEquals
(
ReleaseStatus
.
Beta
,
test
.
getStatus
());
assertEquals
(
3
,
test
.
getStatusVersion
());
assertEquals
(
ReleaseStatus
.
Release
,
test
.
getStatus
());
assertEquals
(
"1.2.3 Beta 3"
,
test
.
getVersionString
());
assertEquals
(-
1
,
test
.
getStatusVersion
());
assertEquals
(
"1.2.3"
,
test
.
getVersionString
());
}
}
@Test
@Test
...
@@ -60,8 +60,11 @@ public class VersionTest {
...
@@ -60,8 +60,11 @@ public class VersionTest {
Version
test333
=
new
Version
(
"3.3.3"
);
Version
test333
=
new
Version
(
"3.3.3"
);
Version
test300
=
new
Version
(
"3.0.0"
);
Version
test300
=
new
Version
(
"3.0.0"
);
Version
test3100
=
new
Version
(
"3.10.0"
);
Version
test3100
=
new
Version
(
"3.10.0"
);
Version
test29999
=
new
Version
(
"2.99.99"
);
Version
test29999
=
new
Version
(
"2.999.999"
);
Version
test3100Alpha
=
new
Version
(
"3.10.0 Alpha"
);
Version
test3100Beta
=
new
Version
(
"3.10.0 Beta"
);
Version
test3100Beta1
=
new
Version
(
"3.10.0 Beta 1"
);
Version
test3100Beta2
=
new
Version
(
"3.10.0 Beta 2"
);
assertEquals
(-
1
,
test123
.
compareTo
(
test321
));
assertEquals
(-
1
,
test123
.
compareTo
(
test321
));
assertEquals
(
0
,
test123
.
compareTo
(
test123
));
assertEquals
(
0
,
test123
.
compareTo
(
test123
));
assertEquals
(
1
,
test321
.
compareTo
(
test123
));
assertEquals
(
1
,
test321
.
compareTo
(
test123
));
...
@@ -72,7 +75,15 @@ public class VersionTest {
...
@@ -72,7 +75,15 @@ public class VersionTest {
assertTrue
(
test3100
.
isNewerThan
(
test333
));
assertTrue
(
test3100
.
isNewerThan
(
test333
));
assertTrue
(
test3100
.
isNewerThan
(
test29999
));
assertTrue
(
test3100
.
isNewerThan
(
test29999
));
assertTrue
(
test300
.
isNewerThan
(
test29999
));
assertTrue
(
test300
.
isNewerThan
(
test29999
));
assertTrue
(
test3100Beta
.
isNewerThan
(
test3100Alpha
));
assertTrue
(
test3100Beta2
.
isNewerThan
(
test3100Beta1
));
}
}
@Test
public
void
testVersionEquals
()
{
Version
version1
=
new
Version
(
3
,
11
,
0
,
Version
.
ReleaseStatus
.
Alpha
,
-
1
);
Version
version2
=
new
Version
(
3
,
11
,
0
,
Version
.
ReleaseStatus
.
Alpha
,
-
1
);
assertEquals
(
version1
,
version2
);
assertTrue
((
version1
.
compareTo
(
version2
)
==
0
)
==
version1
.
equals
(
version2
));
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment