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
) {
43 this.version
= version
;
45 String
[] tab
= version
.split("\\.");
46 this.major
= Integer
.parseInt(tab
[0]);
47 this.minor
= Integer
.parseInt(tab
[1]);
48 this.patch
= Integer
.parseInt(tab
[2]);
49 } catch (Exception e
) {
57 * The 'major' version.
59 * This version should only change when API-incompatible changes are made to
62 * @return the major version
64 public int getMajor() {
69 * The 'minor' version.
71 * This version should only change when new, backwards-compatible
72 * functionality has been added to the program.
74 * @return the minor version
76 public int getMinor() {
81 * The 'patch' version.
83 * This version should change when backwards-compatible bugfixes have been
84 * added to the program.
86 * @return the patch version
88 public int getPatch() {
93 * Check if this {@link Version} is "empty" (i.e., the version was not
94 * parse-able or not given).
96 * An empty {@link Version} is always <tt>0.0.0</tt>.
98 * @return TRUS if it is empty
100 public boolean isEmpty() {
101 return major
== 0 && minor
== 0 && patch
== 0;
105 * Return the version of the running program if it follows the VERSION
106 * convention (i.e., if it has a file called VERSION containing the version
107 * as a {@link String} in its binary root, and if this {@link String}
108 * follows the Major/Minor/Patch convention).
110 * If it does not, return an empty {@link Version} object.
112 * @return the {@link Version} of the program, or an empty {@link Version}
113 * (does not return NULL)
115 public static String
getCurrentVersion() {
116 String version
= null;
118 InputStream in
= IOUtils
.openResource("VERSION");
121 ByteArrayOutputStream ba
= new ByteArrayOutputStream();
122 IOUtils
.write(in
, ba
);
125 version
= ba
.toString("UTF-8").trim();
126 } catch (IOException e
) {
134 * Return a user-readable form of this {@link Version}.
137 public String
toString() {
138 return version
== null ?
"[unknown]" : version
;