Make it so mapping files are used and then reapplied.
[SquirrelJME.git] / modules / cldc-compact / src / main / java / cc / squirreljme / jvm / suite / SuiteVersionRange.java
blobfe9111033afc7648c667cac7ee2b7692a1d19817
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.jvm.suite;
12 import cc.squirreljme.runtime.cldc.debug.Debugging;
13 import java.lang.ref.Reference;
14 import java.lang.ref.WeakReference;
16 /**
17 * This is used to handle version ranges that may be used for dependencies.
19 * Version ranges are inclusive.
21 * @since 2017/02/22
23 public final class SuiteVersionRange
24 implements Comparable<SuiteVersionRange>
26 /** Any version. */
27 public static final SuiteVersionRange ANY_VERSION =
28 new SuiteVersionRange(SuiteVersion.MIN_VERSION,
29 SuiteVersion.MAX_VERSION);
31 /** The starting range, inclusive. */
32 protected final SuiteVersion from;
34 /** Tne ending range, inclusive. */
35 protected final SuiteVersion to;
37 /** String representation. */
38 private Reference<String> _string;
40 /**
41 * Initializes the range inclusively between the two given versions.
43 * @param __from The source version.
44 * @param __to The destination version.
45 * @throws NullPointerException On null arguments.
46 * @since 2017/02/22
48 public SuiteVersionRange(SuiteVersion __from, SuiteVersion __to)
49 throws NullPointerException
51 // Check
52 if (__from == null || __to == null)
53 throw new NullPointerException("NARG");
55 // Make sure from is always first
56 if (__from.compareTo(__to) <= 0)
58 this.from = __from;
59 this.to = __to;
62 // Swapped
63 else
65 this.from = __to;
66 this.to = __from;
70 /**
71 * Parses the version range that is specified in the dependency of JAR
72 * files.
74 * @param __s The string to parse.
75 * @throws InvalidSuiteException If the range is not valid.
76 * @throws NullPointerException On null arguments.
77 * @since 2017/02/22
79 public SuiteVersionRange(String __s)
80 throws InvalidSuiteException, NullPointerException
82 // Check
83 if (__s == null)
84 throw new NullPointerException("NARG");
86 // Trim
87 __s = __s.trim();
89 // {@squirreljme.error DG0l The version range cannot be blank.}
90 int sl = __s.length();
91 if (sl <= 0)
92 throw new IllegalArgumentException("AR0l");
94 // Get the last character
95 char lc = __s.charAt(__s.length() - 1);
97 // All versions following this.
98 if (lc == '+')
100 this.from = new SuiteVersion(__s.substring(0, sl - 1));
101 this.to = new SuiteVersion(99, 99, 99);
104 // All versions in the group
105 else if (lc == '*')
107 // Get the last dot, if any
108 int ld = __s.lastIndexOf('.');
109 if (ld < 0)
111 // Any version, does not matter
112 if (sl == 1)
114 this.from = new SuiteVersion(0);
115 this.to = new SuiteVersion(99, 99, 99);
118 // {@squirreljme.error DG0m Major only wildcard versions must
119 // be a single asterisk. (The input string)}
120 else
121 throw new InvalidSuiteException(String.format("AR0m %s",
122 __s));
125 // Parse otherwise, just count the number of dots to determine
126 // how deep it goes
127 else
129 // {@squirreljme.error DG0n The last dot in a wildcard must be
130 // before the asterisk. (The input string)}
131 if (ld != sl - 1)
132 throw new InvalidSuiteException(String.format("AR0n %s",
133 __s));
135 // Source range is simple
136 SuiteVersion ver = new SuiteVersion(
137 __s.substring(0, sl - 2));
138 this.from = ver;
140 // Count dots, determines major/minor
141 int numdots = 0;
142 for (int i = 0; i < sl; i++)
143 if (__s.charAt(i) == '.')
144 numdots++;
146 // minor and release wildcard
147 if (numdots == 1)
148 this.to = new SuiteVersion(ver.major(), 99, 99);
150 // release ranged wildcard
151 else if (numdots == 2)
152 this.to = new SuiteVersion(ver.major(), ver.minor(), 99);
154 // {@squirreljme.error DG0o There are too many decimal points
155 // in the wildcard version string. (The input string)}
156 else
157 throw new InvalidSuiteException(String.format("AR0o %s",
158 __s));
162 // Only this version
163 else
165 SuiteVersion ver = new SuiteVersion(__s);
166 this.from = ver;
167 this.to = ver;
172 * {@inheritDoc}
173 * @since 2017/11/30
175 @Override
176 public int compareTo(SuiteVersionRange __o)
178 // From version is always first
179 int rv = this.from.compareTo(__o.from);
180 if (rv != 0)
181 return rv;
183 return this.to.compareTo(__o.to);
187 * {@inheritDoc}
188 * @since 2017/02/22
190 @Override
191 public boolean equals(Object __o)
193 // Check
194 if (!(__o instanceof SuiteVersionRange))
195 return false;
197 // Compare
198 SuiteVersionRange o = (SuiteVersionRange)__o;
199 return this.from.equals(o.from) && this.to.equals(o.to);
203 * Returns the start of the range
205 * @return The range start.
206 * @since 2017/02/22
208 public SuiteVersion from()
210 return this.from;
214 * {@inheritDoc}
215 * @since 2017/02/22
217 @Override
218 public int hashCode()
220 return this.to.hashCode() ^ (~this.from.hashCode());
224 * Checks whether the specified version is in range.
226 * @param __v The version to check.
227 * @return {@code true} if it is in the range.
228 * @throws NullPointerException On null arguments.
229 * @since 2017/02/22
231 public boolean inRange(SuiteVersion __v)
232 throws NullPointerException
234 // Check
235 if (__v == null)
236 throw new NullPointerException("NARG");
238 return __v.compareTo(this.from) >= 0 &&
239 __v.compareTo(this.to) <= 0;
243 * Checks whether the given version is within range of the other version.
245 * @param __r The other version range to check.
246 * @return If the other version range shares all or part of its range
247 * with this range.
248 * @throws NullPointerException On null arguments.
249 * @since 2017/11/27
251 public boolean inRange(SuiteVersionRange __r)
252 throws NullPointerException
254 if (__r == null)
255 throw new NullPointerException("NARG");
257 throw Debugging.todo();
261 * Returns the end of the range.
263 * @return The range end.
264 * @since 2017/02/22
266 public SuiteVersion to()
268 return this.to;
272 * {@inheritDoc}
273 * @since 2017/02/22
275 @Override
276 public String toString()
278 // Get
279 Reference<String> ref = this._string;
280 String rv;
282 // Cache?
283 if (ref == null || null == (rv = ref.get()))
285 // Slowly build version
286 StringBuilder sb = new StringBuilder();
287 SuiteVersion from = this.from;
288 SuiteVersion to = this.to;
290 // Get all values
291 int amaj = from.major(),
292 amin = from.minor(),
293 arel = from.release(),
294 bmaj = to.major(),
295 bmin = to.minor(),
296 brel = to.release();
298 // Pure wildcard
299 if (amaj == 0 && amin == 0 && arel == 0 &&
300 bmaj == 99 && bmin == 99 && brel == 99)
301 sb.append('*');
303 // Exact, subwildcard, or any following
304 else
306 // Add major version
307 sb.append(amaj);
308 sb.append('.');
310 // Wild card minor and release
311 if (amin == 0 && arel == 0 && bmin == 99 && brel == 99)
312 sb.append('*');
314 // Not wild
315 else
317 // Add version
318 sb.append(amin);
319 sb.append('.');
321 // Wild card release
322 if (arel == 0 && brel == 99)
323 sb.append('*');
325 // Would be exact (or plus)
326 else
327 sb.append(arel);
330 // Will be all versions following
331 if (bmaj == 99 && bmin == 99 && brel == 99)
332 sb.append('+');
335 // Store
336 this._string = new WeakReference<>((rv = sb.toString()));
339 return rv;
343 * Returns a version which at most implements the given version.
345 * @param __v The version.
346 * @return The resulting version range.
347 * @throws NullPointerException On null arguments.
348 * @since 2017/11/26
350 public static final SuiteVersionRange atMost(SuiteVersion __v)
351 throws NullPointerException
353 if (__v == null)
354 throw new NullPointerException("NARG");
356 return new SuiteVersionRange(SuiteVersion.MIN_VERSION, __v);
360 * Returns a version which exactly implements the given version.
362 * @param __v The version.
363 * @return The resulting version range.
364 * @throws NullPointerException On null arguments.
365 * @since 2017/11/26
367 public static final SuiteVersionRange exactly(SuiteVersion __v)
368 throws NullPointerException
370 if (__v == null)
371 throw new NullPointerException("NARG");
373 return new SuiteVersionRange(__v, __v);