Skip to content

Commit

Permalink
Apply JDT's 'Always use parentheses' clean-up to P2's VersionRange
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Oct 18, 2024
1 parent 29e2a6d commit 3388118
Showing 1 changed file with 75 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,25 @@ public class VersionRange implements Serializable {
private static int copyEscaped(String vr, int pos, String breakChars, StringBuilder sb) {
int top = vr.length();
pos = VersionParser.skipWhite(vr, pos);
if (pos >= top)
if (pos >= top) {
throw new IllegalArgumentException();

}
char c = vr.charAt(pos);
for (;;) {
if (c == '\\' && ++pos < top)
if (c == '\\' && ++pos < top) {
c = vr.charAt(pos);
else {
if (c <= ' ')
} else {
if (c <= ' ') {
return VersionParser.skipWhite(vr, pos);
if (breakChars != null && breakChars.indexOf(c) >= 0)
}
if (breakChars != null && breakChars.indexOf(c) >= 0) {
break;
}
}
sb.append(c);
if (++pos >= top)
if (++pos >= top) {
break;
}
c = vr.charAt(pos);
}
return pos;
Expand All @@ -83,15 +86,16 @@ public VersionRange(Version minVersion, boolean includeMin, Version maxVersion,
maxVersion = Version.MAX_VERSION;
}
} else {
if (maxVersion == null)
if (maxVersion == null) {
maxVersion = Version.MAX_VERSION;
else {
if (minVersion != maxVersion && minVersion.equals(maxVersion))
} else {
if (minVersion != maxVersion && minVersion.equals(maxVersion)) {
maxVersion = minVersion;
else if (!(minVersion.getFormat() == null ? maxVersion.getFormat() == null : minVersion.getFormat().equals(maxVersion.getFormat()))) {
} else if (!(minVersion.getFormat() == null ? maxVersion.getFormat() == null : minVersion.getFormat().equals(maxVersion.getFormat()))) {
// We always allow the MIN and MAX boundaries
if (!(minVersion.equals(Version.emptyVersion) || maxVersion.equals(Version.MAX_VERSION)))
if (!(minVersion.equals(Version.emptyVersion) || maxVersion.equals(Version.MAX_VERSION))) {
throw new IllegalArgumentException(NLS.bind(Messages.range_boundaries_0_and_1_cannot_have_different_formats, minVersion, maxVersion));
}
}
}
}
Expand Down Expand Up @@ -136,38 +140,42 @@ public VersionRange(String versionRange) {
position[0] = pos;
fmt = parseFormat(versionRange, position);
pos = position[0];
if (pos >= versionRange.length())
if (pos >= versionRange.length()) {
throw new IllegalArgumentException(NLS.bind(Messages.format_must_be_delimited_by_colon_0, versionRange));

}
c = versionRange.charAt(pos);
if (c != ':')
if (c != ':') {
throw new IllegalArgumentException(NLS.bind(Messages.format_must_be_delimited_by_colon_0, versionRange));
}
++pos;
}
pos = VersionParser.skipWhite(versionRange, pos);
if (pos >= top)
if (pos >= top) {
throw new IllegalArgumentException(NLS.bind(Messages.premature_EOS_0, versionRange));
}
c = versionRange.charAt(pos);
} else
} else {
fmt = VersionFormat.OSGI_FORMAT;

}
String minStr;
String maxStr;
StringBuilder sb = new StringBuilder();
if (c == '[' || c == '(') {
includeMin = (c == '[');
pos = copyEscaped(versionRange, ++pos, ",)]", sb); //$NON-NLS-1$
if (pos >= top)
if (pos >= top) {
throw new IllegalArgumentException(NLS.bind(Messages.premature_EOS_0, versionRange));
}
c = versionRange.charAt(pos++);
if (c != ',')
if (c != ',') {
throw new IllegalArgumentException(NLS.bind(Messages.missing_comma_in_range_0, versionRange));

}
minStr = sb.toString();
sb.setLength(0);
pos = copyEscaped(versionRange, pos, ")]", sb); //$NON-NLS-1$
if (pos >= top)
if (pos >= top) {
throw new IllegalArgumentException();
}
maxStr = sb.toString();

c = versionRange.charAt(pos++);
Expand All @@ -185,25 +193,27 @@ public VersionRange(String versionRange) {
String origMax = null;
pos = VersionParser.skipWhite(versionRange, pos);
if (pos < top && versionRange.charAt(pos) == '/') {
if (++pos == top)
if (++pos == top) {
throw new IllegalArgumentException(NLS.bind(Messages.original_stated_but_missing_0, versionRange));
}
position[0] = pos;
fmt = parseFormat(versionRange, position);
pos = VersionParser.skipWhite(versionRange, position[0]);
if (pos < top) {
boolean origUseIncDelims = false;
c = versionRange.charAt(pos);
if (c != ':')
if (c != ':') {
throw new IllegalArgumentException(NLS.bind(Messages.original_must_start_with_colon_0, versionRange));

}
pos = VersionParser.skipWhite(versionRange, ++pos);
if (pos == top)
if (pos == top) {
throw new IllegalArgumentException(NLS.bind(Messages.original_stated_but_missing_0, versionRange));

}
c = versionRange.charAt(pos);
if (c == '[' || c == '(') {
if (includeMin != (c == '[') || maxStr == null)
if (includeMin != (c == '[') || maxStr == null) {
throw new IllegalArgumentException(NLS.bind(Messages.raw_and_original_must_use_same_range_inclusion_0, versionRange));
}
pos = VersionParser.skipWhite(versionRange, ++pos);
origUseIncDelims = true;
}
Expand All @@ -214,43 +224,51 @@ public VersionRange(String versionRange) {
origMin = sb.toString();
} else {
pos = copyEscaped(versionRange, pos, ",])", sb); //$NON-NLS-1$
if (pos >= top)
if (pos >= top) {
throw new IllegalArgumentException(NLS.bind(Messages.premature_EOS_0, versionRange));
}
c = versionRange.charAt(pos++);
if (c != ',')
if (c != ',') {
throw new IllegalArgumentException(NLS.bind(Messages.missing_comma_in_range_0, versionRange));
}
origMin = sb.toString();

sb.setLength(0);
pos = copyEscaped(versionRange, pos, "])", sb); //$NON-NLS-1$
if (origUseIncDelims) {
if (pos >= top)
if (pos >= top) {
throw new IllegalArgumentException(NLS.bind(Messages.premature_EOS_0, versionRange));
}
c = versionRange.charAt(pos++);
if (includeMax != (c == ']'))
if (includeMax != (c == ']')) {
throw new IllegalArgumentException(NLS.bind(Messages.raw_and_original_must_use_same_range_inclusion_0, versionRange));
}
}
origMax = sb.toString();
}
}
}
minVersion = VersionFormat.parseRaw(minStr, fmt, origMin);
if (maxStr != null) {
if (maxStr.equals(minStr))
if (maxStr.equals(minStr)) {
maxVersion = minVersion;
else
} else {
maxVersion = VersionFormat.parseRaw(maxStr, fmt, origMax);
} else
}
} else {
maxVersion = Version.MAX_VERSION;
}
} else {
if (fmt == null)
if (fmt == null) {
fmt = VersionFormat.OSGI_FORMAT;
}
minVersion = fmt.parse(minStr);
if (maxStr != null) {
if (maxStr.equals(minStr))
if (maxStr.equals(minStr)) {
maxVersion = minVersion;
else
} else {
maxVersion = fmt.parse(maxStr);
}
} else {
maxVersion = Version.MAX_VERSION;
}
Expand Down Expand Up @@ -293,9 +311,9 @@ public static VersionRange create(String versionRange) {

private static IVersionFormat parseFormat(String versionRange, int[] position) {
int pos = VersionParser.skipWhite(versionRange, position[0]);
if (!versionRange.startsWith("format(", pos)) //$NON-NLS-1$
if (!versionRange.startsWith("format(", pos)) { //$NON-NLS-1$
return null;

}
pos += 7;
int end = VersionParser.findEndOfFormat(versionRange, pos, versionRange.length());
try {
Expand Down Expand Up @@ -354,8 +372,9 @@ public VersionRange intersect(VersionRange r2) {
boolean resultMinIncluded;
Version resultMin;
if (minCompare == 0) {
if (maxCompare == 0 && includeMin == r2.getIncludeMinimum() && includeMax == r2.getIncludeMaximum())
if (maxCompare == 0 && includeMin == r2.getIncludeMinimum() && includeMax == r2.getIncludeMaximum()) {
return this;
}
resultMin = minVersion;
resultMinIncluded = includeMin && r2.getIncludeMinimum();
} else if (minCompare < 0) {
Expand All @@ -380,9 +399,9 @@ public VersionRange intersect(VersionRange r2) {
}

int minMaxCmp = resultMin.compareTo(resultMax);
if (minMaxCmp < 0 || (minMaxCmp == 0 && resultMinIncluded && resultMaxIncluded))
if (minMaxCmp < 0 || (minMaxCmp == 0 && resultMinIncluded && resultMaxIncluded)) {
return new VersionRange(resultMin, resultMinIncluded, resultMax, resultMaxIncluded);

}
return null;
}

Expand All @@ -397,13 +416,13 @@ public VersionRange intersect(VersionRange r2) {
* <code>false</code> otherwise
*/
public boolean isIncluded(Version version) {
if (version == null)
if (version == null) {
return false;

if (minVersion == maxVersion)
}
if (minVersion == maxVersion) {
// Can only happen when both includeMin and includeMax are true
return minVersion.equals(version);

}
int minCheck = includeMin ? 0 : -1;
int maxCheck = includeMax ? 0 : 1;
return minVersion.compareTo(version) <= minCheck && maxVersion.compareTo(version) >= maxCheck;
Expand Down Expand Up @@ -483,17 +502,19 @@ public void toString(StringBuilder result) {
boolean hasOriginal = (minVersion.getOriginal() != null || maxVersion.getOriginal() != null);
if (fmt != null || hasOriginal) {
result.append('/');
if (fmt != null)
if (fmt != null) {
fmt.toString(result);
}
if (hasOriginal) {
result.append(':');
if (gtEqual) {
((BasicVersion) minVersion).originalToString(result, true);
} else {
if (Version.emptyVersion.equals(minVersion))
if (Version.emptyVersion.equals(minVersion)) {
((BasicVersion) minVersion).rawToString(result, true);
else
} else {
((BasicVersion) minVersion).originalToString(result, true);
}
result.append(',');
((BasicVersion) maxVersion).originalToString(result, true);
}
Expand All @@ -504,15 +525,17 @@ public void toString(StringBuilder result) {
// Preserve singletons during deserialization
private Object readResolve() {
VersionRange vr = this;
if (equals(emptyRange))
if (equals(emptyRange)) {
vr = emptyRange;
}
return vr;
}

private void validateRange() {
int cmp = minVersion.compareTo(maxVersion);
if (!(cmp < 0 || (cmp == 0 && includeMin && includeMax)))
if (!(cmp < 0 || (cmp == 0 && includeMin && includeMax))) {
throw new IllegalArgumentException(NLS.bind(Messages.range_min_0_is_not_less_then_range_max_1, minVersion, maxVersion));
}
}

}

0 comments on commit 3388118

Please sign in to comment.