|
1 | 1 | package org.labkey.test.util; |
2 | 2 |
|
| 3 | +/** |
| 4 | + * Represents a range of LabKey versions. |
| 5 | + * Used for checking if a specific version falls within an inclusive range. |
| 6 | + */ |
3 | 7 | public class VersionRange |
4 | 8 | { |
5 | | - private final Version eariestVersion; |
| 9 | + private final Version earliestVersion; |
6 | 10 | private final Version latestVersion; |
7 | 11 |
|
8 | | - public VersionRange(Version eariestVersion, Version latestVersion) |
| 12 | + /** |
| 13 | + * Constructs a version range. |
| 14 | + * |
| 15 | + * @param earliestVersion The earliest version in the range (inclusive). If null, there is no lower bound. |
| 16 | + * @param latestVersion The latest version in the range (inclusive). If null, there is no upper bound. |
| 17 | + * @throws IllegalArgumentException if both versions are null, or if earliestVersion is after latestVersion. |
| 18 | + */ |
| 19 | + public VersionRange(Version earliestVersion, Version latestVersion) |
9 | 20 | { |
10 | | - this.eariestVersion = eariestVersion; |
| 21 | + this.earliestVersion = earliestVersion; |
11 | 22 | this.latestVersion = latestVersion; |
| 23 | + |
| 24 | + if (earliestVersion == null && latestVersion == null) |
| 25 | + throw new IllegalArgumentException("Version range requires at least one version"); |
| 26 | + if (earliestVersion != null && latestVersion != null && earliestVersion.compareTo(latestVersion) > 0) |
| 27 | + throw new IllegalArgumentException("%s is after %s".formatted(earliestVersion, latestVersion)); |
| 28 | + |
12 | 29 | } |
13 | 30 |
|
| 31 | + /** |
| 32 | + * Creates a version range starting from the specified version with no upper bound. |
| 33 | + * |
| 34 | + * @param version The earliest version (inclusive). |
| 35 | + * @return A new {@link VersionRange}. |
| 36 | + */ |
14 | 37 | public static VersionRange from(String version) |
15 | 38 | { |
16 | 39 | return new VersionRange(new Version(version), null); |
17 | 40 | } |
18 | 41 |
|
| 42 | + /** |
| 43 | + * Creates a version range ending at the specified version with no lower bound. |
| 44 | + * |
| 45 | + * @param version The latest version (inclusive). |
| 46 | + * @return A new {@link VersionRange}. |
| 47 | + */ |
19 | 48 | public static VersionRange until(String version) |
20 | 49 | { |
21 | 50 | return new VersionRange(null, new Version(version)); |
22 | 51 | } |
23 | 52 |
|
| 53 | + /** |
| 54 | + * Creates a version range with specified bounds. |
| 55 | + * |
| 56 | + * @param earliestVersion The earliest version (inclusive). If null, there is no lower bound. |
| 57 | + * @param latestVersion The latest version (inclusive). If null, there is no upper bound. |
| 58 | + * @return A new {@link VersionRange}. |
| 59 | + */ |
24 | 60 | public static VersionRange versionRange(String earliestVersion, String latestVersion) |
25 | 61 | { |
26 | 62 | Version earliest = earliestVersion == null ? null : new Version(earliestVersion); |
27 | 63 | Version latest = latestVersion == null ? null : new Version(latestVersion); |
28 | 64 | return new VersionRange(earliest, latest); |
29 | 65 | } |
30 | 66 |
|
| 67 | + /** |
| 68 | + * Checks if the specified version is within this range (inclusive). |
| 69 | + * |
| 70 | + * @param version The version to check. |
| 71 | + * @return {@code true} if the version is within the range. |
| 72 | + */ |
31 | 73 | public boolean contains(Version version) |
32 | 74 | { |
33 | | - return (eariestVersion == null || eariestVersion.compareTo(version) <= 0) && |
| 75 | + return (earliestVersion == null || earliestVersion.compareTo(version) <= 0) && |
34 | 76 | (latestVersion == null || latestVersion.compareTo(version) >= 0); |
35 | 77 | } |
36 | 78 | } |
0 commit comments