Commonize into PathUtils; On Linux/BSD try to use xdg-open/x-www-browser if Java...
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / swm / SuiteVersionRange.java
blobf986bfb3ba74465747bf36bd2b4b9d57d059d16d
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 is used to handle version ranges that may be used for dependencies.
18 * Version ranges are inclusive.
20 * @since 2017/02/22
22 public final class SuiteVersionRange
23 implements Comparable<SuiteVersionRange>
25 /** Any version. */
26 public static final SuiteVersionRange ANY_VERSION =
27 new SuiteVersionRange(SuiteVersion.MIN_VERSION,
28 SuiteVersion.MAX_VERSION);
30 /** The starting range, inclusive. */
31 protected final SuiteVersion from;
33 /** Tne ending range, inclusive. */
34 protected final SuiteVersion to;
36 /** String representation. */
37 private Reference<String> _string;
39 /**
40 * Initializes the range inclusively between the two given versions.
42 * @param __from The source version.
43 * @param __to The destination version.
44 * @throws NullPointerException On null arguments.
45 * @since 2017/02/22
47 public SuiteVersionRange(SuiteVersion __from, SuiteVersion __to)
48 throws NullPointerException
50 // Check
51 if (__from == null || __to == null)
52 throw new NullPointerException("NARG");
54 // Make sure from is always first
55 if (__from.compareTo(__to) <= 0)
57 this.from = __from;
58 this.to = __to;
61 // Swapped
62 else
64 this.from = __to;
65 this.to = __from;
69 /**
70 * Parses the version range that is specified in the dependency of JAR
71 * files.
73 * @param __s The string to parse.
74 * @throws InvalidSuiteException If the range is not valid.
75 * @throws NullPointerException On null arguments.
76 * @since 2017/02/22
78 public SuiteVersionRange(String __s)
79 throws InvalidSuiteException, NullPointerException
81 // Check
82 if (__s == null)
83 throw new NullPointerException("NARG");
85 // Trim
86 __s = __s.trim();
88 // {@squirreljme.error DG0l The version range cannot be blank.}
89 int sl = __s.length();
90 if (sl <= 0)
91 throw new IllegalArgumentException("AR0l");
93 // Get the last character
94 char lc = __s.charAt(__s.length() - 1);
96 // All versions following this.
97 if (lc == '+')
99 this.from = new SuiteVersion(__s.substring(0, sl - 1));
100 this.to = new SuiteVersion(99, 99, 99);
103 // All versions in the group
104 else if (lc == '*')
106 // Get the last dot, if any
107 int ld = __s.lastIndexOf('.');
108 if (ld < 0)
110 // Any version, does not matter
111 if (sl == 1)
113 this.from = new SuiteVersion(0);
114 this.to = new SuiteVersion(99, 99, 99);
117 // {@squirreljme.error DG0m Major only wildcard versions must
118 // be a single asterisk. (The input string)}
119 else
120 throw new InvalidSuiteException(String.format("AR0m %s",
121 __s));
124 // Parse otherwise, just count the number of dots to determine
125 // how deep it goes
126 else
128 // {@squirreljme.error DG0n The last dot in a wildcard must be
129 // before the asterisk. (The input string)}
130 if (ld != sl - 1)
131 throw new InvalidSuiteException(String.format("AR0n %s",
132 __s));
134 // Source range is simple
135 SuiteVersion ver = new SuiteVersion(
136 __s.substring(0, sl - 2));
137 this.from = ver;
139 // Count dots, determines major/minor
140 int numdots = 0;
141 for (int i = 0; i < sl; i++)
142 if (__s.charAt(i) == '.')
143 numdots++;
145 // minor and release wildcard
146 if (numdots == 1)
147 this.to = new SuiteVersion(ver.major(), 99, 99);
149 // release ranged wildcard
150 else if (numdots == 2)
151 this.to = new SuiteVersion(ver.major(), ver.minor(), 99);
153 // {@squirreljme.error DG0o There are too many decimal points
154 // in the wildcard version string. (The input string)}
155 else
156 throw new InvalidSuiteException(String.format("AR0o %s",
157 __s));
161 // Only this version
162 else
164 SuiteVersion ver = new SuiteVersion(__s);
165 this.from = ver;
166 this.to = ver;
171 * {@inheritDoc}
172 * @since 2017/11/30
174 @Override
175 public int compareTo(SuiteVersionRange __o)
177 // From version is always first
178 int rv = this.from.compareTo(__o.from);
179 if (rv != 0)
180 return rv;
182 return this.to.compareTo(__o.to);
186 * {@inheritDoc}
187 * @since 2017/02/22
189 @Override
190 public boolean equals(Object __o)
192 // Check
193 if (!(__o instanceof SuiteVersionRange))
194 return false;
196 // Compare
197 SuiteVersionRange o = (SuiteVersionRange)__o;
198 return this.from.equals(o.from) && this.to.equals(o.to);
202 * Returns the start of the range
204 * @return The range start.
205 * @since 2017/02/22
207 public SuiteVersion from()
209 return this.from;
213 * {@inheritDoc}
214 * @since 2017/02/22
216 @Override
217 public int hashCode()
219 return this.to.hashCode() ^ (~this.from.hashCode());
223 * Checks whether the specified version is in range.
225 * @param __v The version to check.
226 * @return {@code true} if it is in the range.
227 * @throws NullPointerException On null arguments.
228 * @since 2017/02/22
230 public boolean inRange(SuiteVersion __v)
231 throws NullPointerException
233 // Check
234 if (__v == null)
235 throw new NullPointerException("NARG");
237 return __v.compareTo(this.from) >= 0 &&
238 __v.compareTo(this.to) <= 0;
242 * Checks whether the given version is within range of the other version.
244 * @param __r The other version range to check.
245 * @return If the other version range shares all or part of its range
246 * with this range.
247 * @throws NullPointerException On null arguments.
248 * @since 2017/11/27
250 public boolean inRange(SuiteVersionRange __r)
251 throws NullPointerException
253 if (__r == null)
254 throw new NullPointerException("NARG");
256 throw new Error("TODO");
260 * Returns the end of the range.
262 * @return The range end.
263 * @since 2017/02/22
265 public SuiteVersion to()
267 return this.to;
271 * {@inheritDoc}
272 * @since 2017/02/22
274 @Override
275 public String toString()
277 // Get
278 Reference<String> ref = this._string;
279 String rv;
281 // Cache?
282 if (ref == null || null == (rv = ref.get()))
284 // Slowly build version
285 StringBuilder sb = new StringBuilder();
286 SuiteVersion from = this.from;
287 SuiteVersion to = this.to;
289 // Get all values
290 int amaj = from.major(),
291 amin = from.minor(),
292 arel = from.release(),
293 bmaj = to.major(),
294 bmin = to.minor(),
295 brel = to.release();
297 // Pure wildcard
298 if (amaj == 0 && amin == 0 && arel == 0 &&
299 bmaj == 99 && bmin == 99 && brel == 99)
300 sb.append('*');
302 // Exact, subwildcard, or any following
303 else
305 // Add major version
306 sb.append(amaj);
307 sb.append('.');
309 // Wild card minor and release
310 if (amin == 0 && arel == 0 && bmin == 99 && brel == 99)
311 sb.append('*');
313 // Not wild
314 else
316 // Add version
317 sb.append(amin);
318 sb.append('.');
320 // Wild card release
321 if (arel == 0 && brel == 99)
322 sb.append('*');
324 // Would be exact (or plus)
325 else
326 sb.append(arel);
329 // Will be all versions following
330 if (bmaj == 99 && bmin == 99 && brel == 99)
331 sb.append('+');
334 // Store
335 this._string = new WeakReference<>((rv = sb.toString()));
338 return rv;
342 * Returns a version which at most implements the given version.
344 * @param __v The version.
345 * @return The resulting version range.
346 * @throws NullPointerException On null arguments.
347 * @since 2017/11/26
349 public static final SuiteVersionRange atMost(SuiteVersion __v)
350 throws NullPointerException
352 if (__v == null)
353 throw new NullPointerException("NARG");
355 return new SuiteVersionRange(SuiteVersion.MIN_VERSION, __v);
359 * Returns a version which exactly implements the given version.
361 * @param __v The version.
362 * @return The resulting version range.
363 * @throws NullPointerException On null arguments.
364 * @since 2017/11/26
366 public static final SuiteVersionRange exactly(SuiteVersion __v)
367 throws NullPointerException
369 if (__v == null)
370 throw new NullPointerException("NARG");
372 return new SuiteVersionRange(__v, __v);