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
.jvm
.suite
;
12 import cc
.squirreljme
.runtime
.cldc
.debug
.Debugging
;
13 import java
.lang
.ref
.Reference
;
14 import java
.lang
.ref
.WeakReference
;
17 * This is used to handle version ranges that may be used for dependencies.
19 * Version ranges are inclusive.
23 public final class SuiteVersionRange
24 implements Comparable
<SuiteVersionRange
>
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
;
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.
48 public SuiteVersionRange(SuiteVersion __from
, SuiteVersion __to
)
49 throws NullPointerException
52 if (__from
== null || __to
== null)
53 throw new NullPointerException("NARG");
55 // Make sure from is always first
56 if (__from
.compareTo(__to
) <= 0)
71 * Parses the version range that is specified in the dependency of JAR
74 * @param __s The string to parse.
75 * @throws InvalidSuiteException If the range is not valid.
76 * @throws NullPointerException On null arguments.
79 public SuiteVersionRange(String __s
)
80 throws InvalidSuiteException
, NullPointerException
84 throw new NullPointerException("NARG");
89 // {@squirreljme.error DG0l The version range cannot be blank.}
90 int sl
= __s
.length();
92 throw new IllegalArgumentException("AR0l");
94 // Get the last character
95 char lc
= __s
.charAt(__s
.length() - 1);
97 // All versions following this.
100 this.from
= new SuiteVersion(__s
.substring(0, sl
- 1));
101 this.to
= new SuiteVersion(99, 99, 99);
104 // All versions in the group
107 // Get the last dot, if any
108 int ld
= __s
.lastIndexOf('.');
111 // Any version, does not matter
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)}
121 throw new InvalidSuiteException(String
.format("AR0m %s",
125 // Parse otherwise, just count the number of dots to determine
129 // {@squirreljme.error DG0n The last dot in a wildcard must be
130 // before the asterisk. (The input string)}
132 throw new InvalidSuiteException(String
.format("AR0n %s",
135 // Source range is simple
136 SuiteVersion ver
= new SuiteVersion(
137 __s
.substring(0, sl
- 2));
140 // Count dots, determines major/minor
142 for (int i
= 0; i
< sl
; i
++)
143 if (__s
.charAt(i
) == '.')
146 // minor and release wildcard
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)}
157 throw new InvalidSuiteException(String
.format("AR0o %s",
165 SuiteVersion ver
= new SuiteVersion(__s
);
176 public int compareTo(SuiteVersionRange __o
)
178 // From version is always first
179 int rv
= this.from
.compareTo(__o
.from
);
183 return this.to
.compareTo(__o
.to
);
191 public boolean equals(Object __o
)
194 if (!(__o
instanceof SuiteVersionRange
))
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.
208 public SuiteVersion
from()
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.
231 public boolean inRange(SuiteVersion __v
)
232 throws NullPointerException
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
248 * @throws NullPointerException On null arguments.
251 public boolean inRange(SuiteVersionRange __r
)
252 throws NullPointerException
255 throw new NullPointerException("NARG");
257 throw Debugging
.todo();
261 * Returns the end of the range.
263 * @return The range end.
266 public SuiteVersion
to()
276 public String
toString()
279 Reference
<String
> ref
= this._string
;
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
;
291 int amaj
= from
.major(),
293 arel
= from
.release(),
299 if (amaj
== 0 && amin
== 0 && arel
== 0 &&
300 bmaj
== 99 && bmin
== 99 && brel
== 99)
303 // Exact, subwildcard, or any following
310 // Wild card minor and release
311 if (amin
== 0 && arel
== 0 && bmin
== 99 && brel
== 99)
322 if (arel
== 0 && brel
== 99)
325 // Would be exact (or plus)
330 // Will be all versions following
331 if (bmaj
== 99 && bmin
== 99 && brel
== 99)
336 this._string
= new WeakReference
<>((rv
= sb
.toString()));
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.
350 public static final SuiteVersionRange
atMost(SuiteVersion __v
)
351 throws NullPointerException
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.
367 public static final SuiteVersionRange
exactly(SuiteVersion __v
)
368 throws NullPointerException
371 throw new NullPointerException("NARG");
373 return new SuiteVersionRange(__v
, __v
);