Version 1.3.4
[nikiroo-utils.git] / src / be / nikiroo / utils / Version.java
CommitLineData
32ae2079
NR
1package be.nikiroo.utils;
2
3import java.io.ByteArrayOutputStream;
4import java.io.IOException;
5import java.io.InputStream;
6
7/**
8 * This class describe a program {@link Version}.
9 *
10 * @author niki
11 */
c108bc7d 12public class Version implements Comparable<Version> {
32ae2079
NR
13 private String version;
14 private int major;
15 private int minor;
16 private int patch;
17
c108bc7d
NR
18 /**
19 * Create a new, empty {@link Version}.
20 *
21 */
22 public Version() {
23 }
24
32ae2079
NR
25 /**
26 * Create a new {@link Version} with the given values.
27 *
28 * @param major
29 * the major version
30 * @param minor
31 * the minor version
32 * @param patch
33 * the patch version
34 */
35 public Version(int major, int minor, int patch) {
36 this.major = major;
37 this.minor = minor;
38 this.patch = patch;
39 this.version = String.format("%d.%d.%d", major, minor, patch);
40 }
41
42 /**
43 * Create a new {@link Version} with the given value, which must be in the
44 * form <tt>MAJOR.MINOR.PATCH</tt>.
45 *
46 * @param version
47 * the version (<tt>MAJOR.MINOR.PATCH</tt>)
48 */
49 public Version(String version) {
32ae2079
NR
50 try {
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]);
b0376a37 55 this.version = version;
32ae2079
NR
56 } catch (Exception e) {
57 this.major = 0;
58 this.minor = 0;
59 this.patch = 0;
b0376a37 60 this.version = null;
32ae2079
NR
61 }
62 }
63
64 /**
65 * The 'major' version.
66 * <p>
67 * This version should only change when API-incompatible changes are made to
68 * the program.
69 *
70 * @return the major version
71 */
72 public int getMajor() {
73 return major;
74 }
75
76 /**
77 * The 'minor' version.
78 * <p>
79 * This version should only change when new, backwards-compatible
80 * functionality has been added to the program.
81 *
82 * @return the minor version
83 */
84 public int getMinor() {
85 return minor;
86 }
87
88 /**
89 * The 'patch' version.
90 * <p>
91 * This version should change when backwards-compatible bugfixes have been
92 * added to the program.
93 *
94 * @return the patch version
95 */
96 public int getPatch() {
97 return patch;
98 }
99
100 /**
101 * Check if this {@link Version} is "empty" (i.e., the version was not
102 * parse-able or not given).
103 * <p>
104 * An empty {@link Version} is always <tt>0.0.0</tt>.
105 *
c108bc7d 106 * @return TRUE if it is empty
32ae2079
NR
107 */
108 public boolean isEmpty() {
109 return major == 0 && minor == 0 && patch == 0;
110 }
111
c108bc7d
NR
112 /**
113 * Check if we are more recent than the given {@link Version}.
114 *
115 * @param o
116 * the other {@link Version}
117 * @return TRUE if this {@link Version} is more recent than the given one
118 */
119 public boolean isNewerThan(Version o) {
120 if (major > o.major) {
121 return true;
122 }
123
124 if (major == o.major && minor > o.minor) {
125 return true;
126 }
127
128 if (major == o.major && minor == o.minor && patch > o.patch) {
129 return true;
130 }
131
132 return false;
133 }
134
135 /**
136 * Check if we are older than the given {@link Version}.
137 *
138 * @param o
139 * the other {@link Version}
140 * @return TRUE if this {@link Version} is older than the given one
141 */
142 public boolean isOlderThan(Version o) {
143 return !equals(o) && !isNewerThan(o);
144 }
145
32ae2079
NR
146 /**
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).
151 * <p>
152 * If it does not, return an empty {@link Version} object.
153 *
154 * @return the {@link Version} of the program, or an empty {@link Version}
155 * (does not return NULL)
156 */
3341f018 157 public static Version getCurrentVersion() {
32ae2079
NR
158 String version = null;
159
160 InputStream in = IOUtils.openResource("VERSION");
161 if (in != null) {
162 try {
163 ByteArrayOutputStream ba = new ByteArrayOutputStream();
164 IOUtils.write(in, ba);
165 in.close();
166
167 version = ba.toString("UTF-8").trim();
168 } catch (IOException e) {
169 }
170 }
171
3341f018 172 return new Version(version);
32ae2079
NR
173 }
174
c108bc7d
NR
175 public int compareTo(Version o) {
176 if (equals(o)) {
177 return 0;
178 } else if (isNewerThan(o)) {
179 return 1;
180 } else {
181 return -1;
182 }
183 }
184
185 @Override
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;
190 }
191
192 return false;
193 }
194
195 @Override
196 public int hashCode() {
197 return version == null ? 0 : version.hashCode();
198 }
199
32ae2079
NR
200 /**
201 * Return a user-readable form of this {@link Version}.
202 */
203 @Override
204 public String toString() {
205 return version == null ? "[unknown]" : version;
206 }
207}