1 package be
.nikiroo
.utils
;
3 import java
.io
.ByteArrayOutputStream
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
8 * This class describe a program {@link Version}.
12 public class Version
{
13 private String version
;
19 * Create a new {@link Version} with the given values.
28 public Version(int major
, int minor
, int patch
) {
32 this.version
= String
.format("%d.%d.%d", major
, minor
, patch
);
36 * Create a new {@link Version} with the given value, which must be in the
37 * form <tt>MAJOR.MINOR.PATCH</tt>.
40 * the version (<tt>MAJOR.MINOR.PATCH</tt>)
42 public Version(String version
) {
44 String
[] tab
= version
.split("\\.");
45 this.major
= Integer
.parseInt(tab
[0]);
46 this.minor
= Integer
.parseInt(tab
[1]);
47 this.patch
= Integer
.parseInt(tab
[2]);
48 this.version
= version
;
49 } catch (Exception e
) {
58 * The 'major' version.
60 * This version should only change when API-incompatible changes are made to
63 * @return the major version
65 public int getMajor() {
70 * The 'minor' version.
72 * This version should only change when new, backwards-compatible
73 * functionality has been added to the program.
75 * @return the minor version
77 public int getMinor() {
82 * The 'patch' version.
84 * This version should change when backwards-compatible bugfixes have been
85 * added to the program.
87 * @return the patch version
89 public int getPatch() {
94 * Check if this {@link Version} is "empty" (i.e., the version was not
95 * parse-able or not given).
97 * An empty {@link Version} is always <tt>0.0.0</tt>.
99 * @return TRUS if it is empty
101 public boolean isEmpty() {
102 return major
== 0 && minor
== 0 && patch
== 0;
106 * Return the version of the running program if it follows the VERSION
107 * convention (i.e., if it has a file called VERSION containing the version
108 * as a {@link String} in its binary root, and if this {@link String}
109 * follows the Major/Minor/Patch convention).
111 * If it does not, return an empty {@link Version} object.
113 * @return the {@link Version} of the program, or an empty {@link Version}
114 * (does not return NULL)
116 public static Version
getCurrentVersion() {
117 String version
= null;
119 InputStream in
= IOUtils
.openResource("VERSION");
122 ByteArrayOutputStream ba
= new ByteArrayOutputStream();
123 IOUtils
.write(in
, ba
);
126 version
= ba
.toString("UTF-8").trim();
127 } catch (IOException e
) {
131 return new Version(version
);
135 * Return a user-readable form of this {@link Version}.
138 public String
toString() {
139 return version
== null ?
"[unknown]" : version
;