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 is used to handle version ranges that may be used for dependencies.
18 * Version ranges are inclusive.
22 public final class SuiteVersionRange
23 implements Comparable
<SuiteVersionRange
>
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
;
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.
47 public SuiteVersionRange(SuiteVersion __from
, SuiteVersion __to
)
48 throws NullPointerException
51 if (__from
== null || __to
== null)
52 throw new NullPointerException("NARG");
54 // Make sure from is always first
55 if (__from
.compareTo(__to
) <= 0)
70 * Parses the version range that is specified in the dependency of JAR
73 * @param __s The string to parse.
74 * @throws InvalidSuiteException If the range is not valid.
75 * @throws NullPointerException On null arguments.
78 public SuiteVersionRange(String __s
)
79 throws InvalidSuiteException
, NullPointerException
83 throw new NullPointerException("NARG");
88 // {@squirreljme.error DG0l The version range cannot be blank.}
89 int sl
= __s
.length();
91 throw new IllegalArgumentException("AR0l");
93 // Get the last character
94 char lc
= __s
.charAt(__s
.length() - 1);
96 // All versions following this.
99 this.from
= new SuiteVersion(__s
.substring(0, sl
- 1));
100 this.to
= new SuiteVersion(99, 99, 99);
103 // All versions in the group
106 // Get the last dot, if any
107 int ld
= __s
.lastIndexOf('.');
110 // Any version, does not matter
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)}
120 throw new InvalidSuiteException(String
.format("AR0m %s",
124 // Parse otherwise, just count the number of dots to determine
128 // {@squirreljme.error DG0n The last dot in a wildcard must be
129 // before the asterisk. (The input string)}
131 throw new InvalidSuiteException(String
.format("AR0n %s",
134 // Source range is simple
135 SuiteVersion ver
= new SuiteVersion(
136 __s
.substring(0, sl
- 2));
139 // Count dots, determines major/minor
141 for (int i
= 0; i
< sl
; i
++)
142 if (__s
.charAt(i
) == '.')
145 // minor and release wildcard
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)}
156 throw new InvalidSuiteException(String
.format("AR0o %s",
164 SuiteVersion ver
= new SuiteVersion(__s
);
175 public int compareTo(SuiteVersionRange __o
)
177 // From version is always first
178 int rv
= this.from
.compareTo(__o
.from
);
182 return this.to
.compareTo(__o
.to
);
190 public boolean equals(Object __o
)
193 if (!(__o
instanceof SuiteVersionRange
))
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.
207 public SuiteVersion
from()
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.
230 public boolean inRange(SuiteVersion __v
)
231 throws NullPointerException
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
247 * @throws NullPointerException On null arguments.
250 public boolean inRange(SuiteVersionRange __r
)
251 throws NullPointerException
254 throw new NullPointerException("NARG");
256 throw new Error("TODO");
260 * Returns the end of the range.
262 * @return The range end.
265 public SuiteVersion
to()
275 public String
toString()
278 Reference
<String
> ref
= this._string
;
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
;
290 int amaj
= from
.major(),
292 arel
= from
.release(),
298 if (amaj
== 0 && amin
== 0 && arel
== 0 &&
299 bmaj
== 99 && bmin
== 99 && brel
== 99)
302 // Exact, subwildcard, or any following
309 // Wild card minor and release
310 if (amin
== 0 && arel
== 0 && bmin
== 99 && brel
== 99)
321 if (arel
== 0 && brel
== 99)
324 // Would be exact (or plus)
329 // Will be all versions following
330 if (bmaj
== 99 && bmin
== 99 && brel
== 99)
335 this._string
= new WeakReference
<>((rv
= sb
.toString()));
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.
349 public static final SuiteVersionRange
atMost(SuiteVersion __v
)
350 throws NullPointerException
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.
366 public static final SuiteVersionRange
exactly(SuiteVersion __v
)
367 throws NullPointerException
370 throw new NullPointerException("NARG");
372 return new SuiteVersionRange(__v
, __v
);