Version 1.3.1: new Version class
[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 */
12public class Version {
13 private String version;
14 private int major;
15 private int minor;
16 private int patch;
17
18 /**
19 * Create a new {@link Version} with the given values.
20 *
21 * @param major
22 * the major version
23 * @param minor
24 * the minor version
25 * @param patch
26 * the patch version
27 */
28 public Version(int major, int minor, int patch) {
29 this.major = major;
30 this.minor = minor;
31 this.patch = patch;
32 this.version = String.format("%d.%d.%d", major, minor, patch);
33 }
34
35 /**
36 * Create a new {@link Version} with the given value, which must be in the
37 * form <tt>MAJOR.MINOR.PATCH</tt>.
38 *
39 * @param version
40 * the version (<tt>MAJOR.MINOR.PATCH</tt>)
41 */
42 public Version(String version) {
43 this.version = version;
44 try {
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) {
50 this.major = 0;
51 this.minor = 0;
52 this.patch = 0;
53 }
54 }
55
56 /**
57 * The 'major' version.
58 * <p>
59 * This version should only change when API-incompatible changes are made to
60 * the program.
61 *
62 * @return the major version
63 */
64 public int getMajor() {
65 return major;
66 }
67
68 /**
69 * The 'minor' version.
70 * <p>
71 * This version should only change when new, backwards-compatible
72 * functionality has been added to the program.
73 *
74 * @return the minor version
75 */
76 public int getMinor() {
77 return minor;
78 }
79
80 /**
81 * The 'patch' version.
82 * <p>
83 * This version should change when backwards-compatible bugfixes have been
84 * added to the program.
85 *
86 * @return the patch version
87 */
88 public int getPatch() {
89 return patch;
90 }
91
92 /**
93 * Check if this {@link Version} is "empty" (i.e., the version was not
94 * parse-able or not given).
95 * <p>
96 * An empty {@link Version} is always <tt>0.0.0</tt>.
97 *
98 * @return TRUS if it is empty
99 */
100 public boolean isEmpty() {
101 return major == 0 && minor == 0 && patch == 0;
102 }
103
104 /**
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).
109 * <p>
110 * If it does not, return an empty {@link Version} object.
111 *
112 * @return the {@link Version} of the program, or an empty {@link Version}
113 * (does not return NULL)
114 */
115 public static String getCurrentVersion() {
116 String version = null;
117
118 InputStream in = IOUtils.openResource("VERSION");
119 if (in != null) {
120 try {
121 ByteArrayOutputStream ba = new ByteArrayOutputStream();
122 IOUtils.write(in, ba);
123 in.close();
124
125 version = ba.toString("UTF-8").trim();
126 } catch (IOException e) {
127 }
128 }
129
130 return version;
131 }
132
133 /**
134 * Return a user-readable form of this {@link Version}.
135 */
136 @Override
137 public String toString() {
138 return version == null ? "[unknown]" : version;
139 }
140}