94387373e980daf30b3ca4de01cfa72e490a0307
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
implements Comparable
<Version
> {
13 private String version
;
19 * Create a new, empty {@link Version}.
26 * Create a new {@link Version} with the given values.
35 public Version(int major
, int minor
, int patch
) {
39 this.version
= String
.format("%d.%d.%d", major
, minor
, patch
);
43 * Create a new {@link Version} with the given value, which must be in the
44 * form <tt>MAJOR.MINOR.PATCH</tt>.
47 * the version (<tt>MAJOR.MINOR.PATCH</tt>)
49 public Version(String version
) {
51 String
[] tab
= version
.split("\\.");
52 this.major
= Integer
.parseInt(tab
[0]);
53 this.minor
= Integer
.parseInt(tab
[1]);
54 this.patch
= Integer
.parseInt(tab
[2]);
55 this.version
= version
;
56 } catch (Exception e
) {
65 * The 'major' version.
67 * This version should only change when API-incompatible changes are made to
70 * @return the major version
72 public int getMajor() {
77 * The 'minor' version.
79 * This version should only change when new, backwards-compatible
80 * functionality has been added to the program.
82 * @return the minor version
84 public int getMinor() {
89 * The 'patch' version.
91 * This version should change when backwards-compatible bugfixes have been
92 * added to the program.
94 * @return the patch version
96 public int getPatch() {
101 * Check if this {@link Version} is "empty" (i.e., the version was not
102 * parse-able or not given).
104 * An empty {@link Version} is always <tt>0.0.0</tt>.
106 * @return TRUE if it is empty
108 public boolean isEmpty() {
109 return major
== 0 && minor
== 0 && patch
== 0;
113 * Check if we are more recent than the given {@link Version}.
116 * the other {@link Version}
117 * @return TRUE if this {@link Version} is more recent than the given one
119 public boolean isNewerThan(Version o
) {
120 if (major
> o
.major
) {
124 if (major
== o
.major
&& minor
> o
.minor
) {
128 if (major
== o
.major
&& minor
== o
.minor
&& patch
> o
.patch
) {
136 * Check if we are older than the given {@link Version}.
139 * the other {@link Version}
140 * @return TRUE if this {@link Version} is older than the given one
142 public boolean isOlderThan(Version o
) {
143 return !equals(o
) && !isNewerThan(o
);
147 * Return the version of the running program if it follows the VERSION
148 * convention (i.e., if it has a file called VERSION containing the version
149 * as a {@link String} in its binary root, and if this {@link String}
150 * follows the Major/Minor/Patch convention).
152 * If it does not, return an empty {@link Version} object.
154 * @return the {@link Version} of the program, or an empty {@link Version}
155 * (does not return NULL)
157 public static Version
getCurrentVersion() {
158 String version
= null;
160 InputStream in
= IOUtils
.openResource("VERSION");
163 ByteArrayOutputStream ba
= new ByteArrayOutputStream();
164 IOUtils
.write(in
, ba
);
167 version
= ba
.toString("UTF-8").trim();
168 } catch (IOException e
) {
172 return new Version(version
);
175 public int compareTo(Version o
) {
178 } else if (isNewerThan(o
)) {
186 public boolean equals(Object obj
) {
187 if (obj
instanceof Version
) {
188 Version o
= (Version
) obj
;
189 return o
.major
== major
&& o
.minor
== minor
&& o
.patch
== patch
;
196 public int hashCode() {
197 return version
== null ?
0 : version
.hashCode();
201 * Return a user-readable form of this {@link Version}.
204 public String
toString() {
205 return version
== null ?
"[unknown]" : version
;