Skip to content

Commit 7fdc6b8

Browse files
committed
Added several sanity checks to VersionRange
1 parent d25d0be commit 7fdc6b8

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/main/java/com/viaversion/vialoader/util/VersionRange.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,34 @@ public class VersionRange {
3535
private VersionRange(final ProtocolVersion min, final ProtocolVersion max) {
3636
this.min = min;
3737
this.max = max;
38-
this.ranges = new ArrayList<>();
38+
this.ranges = new ArrayList<>(3);
3939
}
4040

4141
public static VersionRange andNewer(final ProtocolVersion version) {
42+
Objects.requireNonNull(version, "Version cannot be null");
43+
4244
return new VersionRange(version, null);
4345
}
4446

4547
public static VersionRange single(final ProtocolVersion version) {
48+
Objects.requireNonNull(version, "Version cannot be null");
49+
4650
return new VersionRange(version, version);
4751
}
4852

4953
public static VersionRange andOlder(final ProtocolVersion version) {
54+
Objects.requireNonNull(version, "Version cannot be null");
55+
5056
return new VersionRange(null, version);
5157
}
5258

5359
public static VersionRange of(final ProtocolVersion min, final ProtocolVersion max) {
60+
Objects.requireNonNull(min, "Min version cannot be null");
61+
Objects.requireNonNull(max, "Max version cannot be null");
62+
if (min.newerThan(max)) {
63+
throw new IllegalArgumentException("Min version cannot be newer than max version");
64+
}
65+
5466
return new VersionRange(min, max);
5567
}
5668

@@ -64,6 +76,8 @@ public VersionRange add(final VersionRange range) {
6476
}
6577

6678
public boolean contains(final ProtocolVersion version) {
79+
Objects.requireNonNull(version, "Version cannot be null");
80+
6781
for (VersionRange range : this.ranges) {
6882
if (range.contains(version)) return true;
6983
}
@@ -109,10 +123,10 @@ public int hashCode() {
109123
return Objects.hash(min, max, ranges);
110124
}
111125

112-
public static VersionRange fromString(String str) {
126+
public static VersionRange fromString(final String str) {
113127
if ("*".equals(str)) return all();
114128
else if (str.contains(",")) {
115-
String[] rangeParts = str.split(", ");
129+
final String[] rangeParts = str.split(", ");
116130
VersionRange versionRange = null;
117131

118132
for (String part : rangeParts) {
@@ -125,13 +139,13 @@ else if (str.contains(",")) {
125139
}
126140
}
127141

128-
private static VersionRange parseSinglePart(String part) {
142+
private static VersionRange parseSinglePart(final String part) {
129143
if (part.startsWith("<= ")) return andOlder(ProtocolVersion.getClosest(part.substring(3)));
130144
else if (part.startsWith(">= ")) return andNewer(ProtocolVersion.getClosest(part.substring(3)));
131145
else if (part.contains(" - ")) {
132-
String[] rangeParts = part.split(" - ");
133-
ProtocolVersion min = ProtocolVersion.getClosest(rangeParts[0]);
134-
ProtocolVersion max = ProtocolVersion.getClosest(rangeParts[1]);
146+
final String[] rangeParts = part.split(" - ");
147+
final ProtocolVersion min = ProtocolVersion.getClosest(rangeParts[0]);
148+
final ProtocolVersion max = ProtocolVersion.getClosest(rangeParts[1]);
135149
return of(min, max);
136150
} else return single(ProtocolVersion.getClosest(part));
137151
}

0 commit comments

Comments
 (0)