Skip to content

Commit a466a9e

Browse files
committed
Migrate methods to construct various types of versions into the version class.
1 parent 2cc4b08 commit a466a9e

4 files changed

Lines changed: 258 additions & 703 deletions

File tree

src/main/java/org/cthing/versionparser/pypa/PypaSpecifier.java

Lines changed: 8 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static String joinVersion(final List<String> components) {
182182
*/
183183
@AccessForTesting
184184
static String canonicalizeVersion(final PypaVersion version, final boolean stripTrailingZeros) {
185-
return stripTrailingZeros ? makeTrimmed(version).toCanonicalString() : version.toCanonicalString();
185+
return stripTrailingZeros ? version.toTrimmedVersion().toCanonicalString() : version.toCanonicalString();
186186
}
187187

188188
/**
@@ -236,66 +236,6 @@ static List<String> padRelease(final List<String> split, final int targetNumeric
236236

237237
return List.copyOf(result);
238238
}
239-
240-
/**
241-
* Creates a public version based on the specified version by removing the local portion.
242-
*
243-
* @param version Version on which to base the public version
244-
* @return Newly created public version. May return the passed in version if it is already public.
245-
*/
246-
@AccessForTesting
247-
static PypaVersion makePublic(final PypaVersion version) {
248-
return version.replace(PypaVersion.Modifier::withoutLocal);
249-
}
250-
251-
/**
252-
* Creates a version that is the earliest prerelease version based on the specified version by
253-
* setting the dev portion to 0 and removing the local portion.
254-
*
255-
* @param version Version on which to base the earliest prerelease version
256-
* @return Newly created version. May return the passed in version if it is already the earliest
257-
* public version.
258-
*/
259-
@AccessForTesting
260-
static PypaVersion makeEarliestPrerelease(final PypaVersion version) {
261-
return version.replace(modifier -> modifier.withDev(0).withoutLocal());
262-
}
263-
264-
/**
265-
* Creates a post release base version based on the specified version by removing the post, dev and
266-
* local portions.
267-
*
268-
* @param version Version on which to base the post base version
269-
* @return Newly created post base version. May return the passed in version if it is already post base.
270-
*/
271-
@AccessForTesting
272-
static PypaVersion makePostBase(final PypaVersion version) {
273-
return version.replace(modifier -> modifier.withoutPost().withoutDev().withoutLocal());
274-
}
275-
276-
/**
277-
* Creates a trimmed version based on the specified version by removing trailing zeroes.
278-
*
279-
* @param version Version on which to base the trimmed version
280-
* @return Newly created trimmed version. May return the passed in version if it is already trimmed.
281-
* If the version only consists of zeroes, one zero is left.
282-
*/
283-
@AccessForTesting
284-
static PypaVersion makeTrimmed(final PypaVersion version) {
285-
final List<Integer> release = version.getRelease();
286-
287-
int i = release.size() - 1;
288-
while (i > 0 && release.get(i) == 0) {
289-
i--;
290-
}
291-
292-
if (i == release.size() - 1) {
293-
return version;
294-
}
295-
296-
final List<Integer> trimmedRelease = release.subList(0, i + 1);
297-
return version.replace(modifier -> modifier.withRelease(trimmedRelease));
298-
}
299239
}
300240

301241

@@ -429,7 +369,7 @@ private boolean allowsWildcard(final PypaVersion prospect) {
429369
final List<String> versionPartComps = splitVersion(canonicalVersionPart);
430370

431371
// Split the prospect into components
432-
final String canonicalProspect = canonicalizeVersion(makePublic(prospect), false);
372+
final String canonicalProspect = canonicalizeVersion(prospect.toPublicVersion(), false);
433373
final List<String> prospectComps = splitVersion(canonicalProspect);
434374

435375
// Ensure the prospect version is of the appropriate length for comparison with the version identifier.
@@ -451,7 +391,7 @@ private boolean allowsPlain(final PypaVersion prospect) {
451391
// If the version identifier does not contain a local portion, any local portion on the prospect
452392
// must be removed before comparison.
453393
if (this.version.getLocal().isEmpty()) {
454-
return makePublic(prospect).compareTo(this.version) == 0;
394+
return prospect.toPublicVersion().compareTo(this.version) == 0;
455395
}
456396

457397
return prospect.compareTo(this.version) == 0;
@@ -552,7 +492,7 @@ private LessThanEqualSpec(final String versionId) throws VersionParsingException
552492

553493
@Override
554494
public boolean allows(final PypaVersion prospect) {
555-
return makePublic(prospect).compareTo(this.version) <= 0;
495+
return prospect.toPublicVersion().compareTo(this.version) <= 0;
556496
}
557497
}
558498

@@ -571,7 +511,7 @@ private static final class LessThanSpec extends AbstractOrderingSpec {
571511
private LessThanSpec(final String versionId) throws VersionParsingException {
572512
super(OPERATOR, versionId);
573513

574-
this.earliestVersion = makeEarliestPrerelease(this.version);
514+
this.earliestVersion = this.version.toEarliestPrereleaseVersion();
575515
}
576516

577517
@Override
@@ -604,7 +544,7 @@ private GreaterThanEqualSpec(final String versionId) throws VersionParsingExcept
604544

605545
@Override
606546
public boolean allows(final PypaVersion prospect) {
607-
return makePublic(prospect).compareTo(this.version) >= 0;
547+
return prospect.toPublicVersion().compareTo(this.version) >= 0;
608548
}
609549
}
610550

@@ -629,11 +569,11 @@ public boolean allows(final PypaVersion prospect) {
629569
}
630570

631571
if (!this.version.isPostRelease() && prospect.isPostRelease()
632-
&& makePostBase(prospect).compareTo(this.version) == 0) {
572+
&& prospect.toPostBaseVersion().compareTo(this.version) == 0) {
633573
return false;
634574
}
635575

636-
return prospect.getLocal().isEmpty() || makePublic(prospect).compareTo(this.version) != 0;
576+
return prospect.getLocal().isEmpty() || prospect.toPublicVersion().compareTo(this.version) != 0;
637577
}
638578
}
639579

0 commit comments

Comments
 (0)