1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
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
;
16 * This represents a suite version.
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
;
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
50 * @throws NullPointerException On null arguments.
53 public SuiteVersion(String __v
)
54 throws IllegalArgumentException
, NullPointerException
56 this(SuiteVersion
.__decodeVersion(__v
));
60 * Initializes a Midlet version number from the specified array of
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
67 * @throws NullPointerException On null arguments.
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));
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
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));
99 * Initializes the version.
101 * @param __maj The major version.
102 * @throws IllegalArgumentException If any value is out of range.
105 public SuiteVersion(int __maj
)
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.
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.
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
));
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
160 throw new NullPointerException("NARG");
162 // Can compare the hashcodes
163 return this.hashCode() >= __v
.hashCode();
171 public int compareTo(SuiteVersion __o
)
174 int amaj
= this.major
, bmaj
= __o
.major
;
175 int rv
= amaj
- bmaj
;
180 int amin
= this.minor
, bmin
= __o
.minor
;
186 int arel
= this.release
, brel
= __o
.release
;
200 public boolean equals(Object __o
)
203 if (!(__o
instanceof SuiteVersion
))
207 SuiteVersion o
= (SuiteVersion
)__o
;
208 return this.major
== o
.major
&&
209 this.minor
== o
.minor
&&
210 this.release
== o
.release
;
218 public int hashCode()
220 return (this.major
* 10000) +
226 * Returns the major version.
228 * @return The major version.
237 * Returns the minor version.
239 * @return The minor version.
248 * Returns the release version.
250 * @return The release version.
263 public String
toString()
266 Reference
<String
> ref
= this._string
;
270 if (ref
== null || null == (rv
= ref
.get()))
271 this._string
= new WeakReference
<>((rv
= this.major
+ "." +
272 this.minor
+ "." + this.release
));
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.
287 private static int[] __decodeVersion(String __v
)
288 throws IllegalArgumentException
, NullPointerException
292 throw new NullPointerException("NARG");
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);
313 if (c
!= -1 && at
>= 4)
314 throw new IllegalArgumentException(String
.format(
315 "Too many version fields in the specified string: %s",
323 else if (c
>= '0' && c
<= '9')
328 throw new IllegalArgumentException(String
.format(
329 "An illegal character is in the version string: %s %c",