Add input and output jars as inputs for VMCompactLibraryTask.
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / swm / SuiteVersion.java
blobb10972191cddb4fa2667dbb4192f69ee0d66d5f2
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.plugin.swm;
12 import java.lang.ref.Reference;
13 import java.lang.ref.WeakReference;
15 /**
16 * This represents a suite version.
18 * @since 2016/10/12
20 public final class SuiteVersion
21 implements Comparable<SuiteVersion>
23 /** The minimum version number. */
24 public static final SuiteVersion MIN_VERSION =
25 new SuiteVersion(0, 0, 0);
27 /** The maximum version number. */
28 public static final SuiteVersion MAX_VERSION =
29 new SuiteVersion(99, 99, 99);
31 /** The major version. */
32 protected final int major;
34 /** The minor version. */
35 protected final int minor;
37 /** The release version. */
38 protected final int release;
40 /** The string representation. */
41 private Reference<String> _string;
43 /**
44 * Initializes the version.
46 * @param __v The value to parse.
47 * @throws IllegalArgumentException If there are too many or too little
48 * version fields, they contain illegal characters, or have an out of range
49 * value.
50 * @throws NullPointerException On null arguments.
51 * @since 2016/10/12
53 public SuiteVersion(String __v)
54 throws IllegalArgumentException, NullPointerException
56 this(SuiteVersion.__decodeVersion(__v));
59 /**
60 * Initializes a Midlet version number from the specified array of
61 * integer values.
63 * @param __v The version triplet, up to the first three elements are
64 * used by the version number.
65 * @throws IllegalArgumentException If the version number has an out of
66 * range value.
67 * @throws NullPointerException On null arguments.
68 * @since 2016/10/12
70 public SuiteVersion(int[] __v)
71 throws IllegalArgumentException, NullPointerException
73 this((__v.length > 0 ? __v[0] : 0),
74 (__v.length > 1 ? __v[1] : 0),
75 (__v.length > 2 ? __v[2] : 0));
78 /**
79 * Decodes the midlet version, optionally allowing it to a reverse
80 * operation of the {@link #hashCode()} method.
82 * @param __hash If {@code true} then the value to decode is treated as
83 * the hash code returned by this class.
84 * @param __maj If {@code __hash} is {@code true} then this is the hash
85 * code of a SuiteVersion, otherwise it is the major version number.
86 * @throws IllegalArgumentException If the version number has an out of
87 * range value.
88 * @since 2016/10/13
90 public SuiteVersion(boolean __hash, int __maj)
91 throws IllegalArgumentException
93 this((__hash ? __maj / 10000 : __maj),
94 (__hash ? (__maj / 100) % 100 : 0),
95 (__hash ? __maj % 100 : 0));
98 /**
99 * Initializes the version.
101 * @param __maj The major version.
102 * @throws IllegalArgumentException If any value is out of range.
103 * @since 2016/10/12
105 public SuiteVersion(int __maj)
107 this(__maj, 0, 0);
111 * Initializes the version.
113 * @param __maj The major version.
114 * @param __min The minor version.
115 * @throws IllegalArgumentException If any value is out of range.
116 * @since 2016/10/12
118 public SuiteVersion(int __maj, int __min)
120 this(__maj, __min, 0);
124 * Initializes the version.
126 * @param __maj The major version.
127 * @param __min The minor version.
128 * @param __rel The release version.
129 * @throws IllegalArgumentException If any value is out of range.
130 * @since 2016/10/12
132 public SuiteVersion(int __maj, int __min, int __rel)
133 throws IllegalArgumentException
135 // Check version ranges
136 if (__maj < 0 || __maj > 99 || __min < 0 || __min > 99 ||
137 __rel < 0 || __rel > 99)
138 throw new IllegalArgumentException(String.format(
139 "Input version number is out of range, only 0 through " +
140 "99 are valid: (%d, %d, %d)", __maj, __min, __rel));
142 // Set
143 this.major = __maj;
144 this.minor = __min;
145 this.release = __rel;
149 * Checks if this version at least the specified verison.
151 * @param __v The version to check against.
152 * @return {@code true} if this version is at least the other.
153 * @throws NullPointerException On nul arguments.
155 public boolean atLeast(SuiteVersion __v)
156 throws NullPointerException
158 // Check
159 if (__v == null)
160 throw new NullPointerException("NARG");
162 // Can compare the hashcodes
163 return this.hashCode() >= __v.hashCode();
167 * {@inheritDoc}
168 * @since 2016/10/12
170 @Override
171 public int compareTo(SuiteVersion __o)
173 // Major first
174 int amaj = this.major, bmaj = __o.major;
175 int rv = amaj - bmaj;
176 if (rv != 0)
177 return rv;
179 // Then minor
180 int amin = this.minor, bmin = __o.minor;
181 rv = amin - bmin;
182 if (rv != 0)
183 return rv;
185 // Then release
186 int arel = this.release, brel = __o.release;
187 rv = arel - brel;
188 if (rv != 0)
189 return rv;
191 // The same
192 return 0;
196 * {@inheritDoc}
197 * @since 2016/10/12
199 @Override
200 public boolean equals(Object __o)
202 // Check
203 if (!(__o instanceof SuiteVersion))
204 return false;
206 // Cast
207 SuiteVersion o = (SuiteVersion)__o;
208 return this.major == o.major &&
209 this.minor == o.minor &&
210 this.release == o.release;
214 * {@inheritDoc}
215 * @since 2016/10/12
217 @Override
218 public int hashCode()
220 return (this.major * 10000) +
221 (this.minor * 100) +
222 this.release;
226 * Returns the major version.
228 * @return The major version.
229 * @since 2017/02/22
231 public int major()
233 return this.major;
237 * Returns the minor version.
239 * @return The minor version.
240 * @since 2017/02/22
242 public int minor()
244 return this.minor;
248 * Returns the release version.
250 * @return The release version.
251 * @since 2017/02/22
253 public int release()
255 return this.release;
259 * {@inheritDoc}
260 * @since 2016/10/12
262 @Override
263 public String toString()
265 // Get
266 Reference<String> ref = this._string;
267 String rv;
269 // Cache?
270 if (ref == null || null == (rv = ref.get()))
271 this._string = new WeakReference<>((rv = this.major + "." +
272 this.minor + "." + this.release));
274 // Return it
275 return rv;
279 * Decodes the string based version number
281 * @param __v The input string.
282 * @return The version tuplet.
283 * @throws IllegalArgumentException If the input is not valid.
284 * @throws NullPointerException On null arguments.
285 * @since 2016/10/12
287 private static int[] __decodeVersion(String __v)
288 throws IllegalArgumentException, NullPointerException
290 // Check
291 if (__v == null)
292 throw new NullPointerException("NARG");
294 // Trim whitespace
295 __v = __v.trim();
297 // Output array
298 int[] rv = new int[3];
300 // Parse the input value
301 int n = __v.length(), at = 0;
302 StringBuilder sb = new StringBuilder();
303 for (int i = 0; i <= n; i++)
305 int c = (i == n ? -1 : __v.charAt(i));
307 // Decimal point? or end
308 if (c == '.' || c == -1)
310 rv[at++] = Integer.parseInt(sb.toString(), 10);
312 // Too many fields
313 if (c != -1 && at >= 4)
314 throw new IllegalArgumentException(String.format(
315 "Too many version fields in the specified string: %s",
316 __v));
318 // Clear
319 sb.setLength(0);
322 // Add to string
323 else if (c >= '0' && c <= '9')
324 sb.append((char)c);
326 // Invalid format
327 else
328 throw new IllegalArgumentException(String.format(
329 "An illegal character is in the version string: %s %c",
330 __v, c));
333 // Return it
334 return rv;