Commit | Line | Data |
---|---|---|
32ae2079 NR |
1 | package be.nikiroo.utils; |
2 | ||
3 | import java.io.ByteArrayOutputStream; | |
4 | import java.io.IOException; | |
5 | import java.io.InputStream; | |
6 | ||
7 | /** | |
8 | * This class describe a program {@link Version}. | |
9 | * | |
10 | * @author niki | |
11 | */ | |
12 | public 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) { | |
32ae2079 NR |
43 | try { |
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]); | |
b0376a37 | 48 | this.version = version; |
32ae2079 NR |
49 | } catch (Exception e) { |
50 | this.major = 0; | |
51 | this.minor = 0; | |
52 | this.patch = 0; | |
b0376a37 | 53 | this.version = null; |
32ae2079 NR |
54 | } |
55 | } | |
56 | ||
57 | /** | |
58 | * The 'major' version. | |
59 | * <p> | |
60 | * This version should only change when API-incompatible changes are made to | |
61 | * the program. | |
62 | * | |
63 | * @return the major version | |
64 | */ | |
65 | public int getMajor() { | |
66 | return major; | |
67 | } | |
68 | ||
69 | /** | |
70 | * The 'minor' version. | |
71 | * <p> | |
72 | * This version should only change when new, backwards-compatible | |
73 | * functionality has been added to the program. | |
74 | * | |
75 | * @return the minor version | |
76 | */ | |
77 | public int getMinor() { | |
78 | return minor; | |
79 | } | |
80 | ||
81 | /** | |
82 | * The 'patch' version. | |
83 | * <p> | |
84 | * This version should change when backwards-compatible bugfixes have been | |
85 | * added to the program. | |
86 | * | |
87 | * @return the patch version | |
88 | */ | |
89 | public int getPatch() { | |
90 | return patch; | |
91 | } | |
92 | ||
93 | /** | |
94 | * Check if this {@link Version} is "empty" (i.e., the version was not | |
95 | * parse-able or not given). | |
96 | * <p> | |
97 | * An empty {@link Version} is always <tt>0.0.0</tt>. | |
98 | * | |
99 | * @return TRUS if it is empty | |
100 | */ | |
101 | public boolean isEmpty() { | |
102 | return major == 0 && minor == 0 && patch == 0; | |
103 | } | |
104 | ||
105 | /** | |
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). | |
110 | * <p> | |
111 | * If it does not, return an empty {@link Version} object. | |
112 | * | |
113 | * @return the {@link Version} of the program, or an empty {@link Version} | |
114 | * (does not return NULL) | |
115 | */ | |
3341f018 | 116 | public static Version getCurrentVersion() { |
32ae2079 NR |
117 | String version = null; |
118 | ||
119 | InputStream in = IOUtils.openResource("VERSION"); | |
120 | if (in != null) { | |
121 | try { | |
122 | ByteArrayOutputStream ba = new ByteArrayOutputStream(); | |
123 | IOUtils.write(in, ba); | |
124 | in.close(); | |
125 | ||
126 | version = ba.toString("UTF-8").trim(); | |
127 | } catch (IOException e) { | |
128 | } | |
129 | } | |
130 | ||
3341f018 | 131 | return new Version(version); |
32ae2079 NR |
132 | } |
133 | ||
134 | /** | |
135 | * Return a user-readable form of this {@link Version}. | |
136 | */ | |
137 | @Override | |
138 | public String toString() { | |
139 | return version == null ? "[unknown]" : version; | |
140 | } | |
141 | } |