[util] build ranges

BuildNumber.fromString() now returns null for empty strings, too.
This commit is contained in:
Roman Shevchenko
2016-04-05 15:29:24 +02:00
parent 70cecccebe
commit 06fa856169
4 changed files with 118 additions and 5 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -89,7 +89,7 @@ public class BuildNumber implements Comparable<BuildNumber> {
}
public static BuildNumber fromString(String version, @Nullable String name) {
if (version == null) return null;
if (StringUtil.isEmptyOrSpaces(version)) return null;
if (BUILD_NUMBER.equals(version)) {
final String productCode = name != null ? name : "";
@@ -282,4 +282,4 @@ public class BuildNumber implements Comparable<BuildNumber> {
public String asStringWithAllDetails() {
return asString(true, true);
}
}
}
@@ -0,0 +1,47 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.util;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* An open-ended range of build numbers.
*/
public class BuildRange {
private final BuildNumber since;
private final BuildNumber until;
public BuildRange(@NotNull BuildNumber since, @NotNull BuildNumber until) {
this.since = since;
this.until = until;
if (since.compareTo(until) > 0) {
throw new IllegalArgumentException("Invalid range: [" + since + "; " + until + "]");
}
}
public boolean inRange(@NotNull BuildNumber build) {
return since.compareTo(build) <= 0 && build.compareTo(until) <= 0;
}
@Contract("null, _ -> null; _, null -> null")
public static BuildRange fromStrings(@Nullable String sinceVal, @Nullable String untilVal) {
BuildNumber since = BuildNumber.fromString(sinceVal);
BuildNumber until = BuildNumber.fromString(untilVal);
return since != null && until != null ? new BuildRange(since, until) : null;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,12 @@ import static org.junit.Assert.*;
* @author max
*/
public class BuildNumberTest {
@Test
public void empty() {
assertNull(BuildNumber.fromString(null));
assertNull(BuildNumber.fromString(" "));
}
@Test
public void historicBuild() {
assertEquals(new BuildNumber("", 75, 7512), BuildNumber.fromString("7512"));
@@ -44,4 +50,4 @@ public class BuildNumberTest {
assertTrue(BuildNumber.fromString("IU-90.SNAPSHOT").compareTo(BuildNumber.fromString("RM-100.SNAPSHOT")) < 0);
assertTrue(BuildNumber.fromString("IU-90.SNAPSHOT").compareTo(BuildNumber.fromString("RM-90.SNAPSHOT")) == 0);
}
}
}
@@ -0,0 +1,60 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.util;
import org.junit.Test;
import static org.junit.Assert.*;
public class BuildRangeTest {
@Test
public void composition() {
assertNull(BuildRange.fromStrings(null, null));
assertNull(BuildRange.fromStrings("", ""));
assertNull(BuildRange.fromStrings("1", ""));
}
@Test
public void validation() {
assertNotNull(BuildRange.fromStrings("0", "100"));
assertNotNull(BuildRange.fromStrings("123.456", "123.456"));
try {
BuildRange.fromStrings("654.321", "123.456");
fail();
}
catch (IllegalArgumentException ignored) { }
}
@Test
public void membership() {
assertTrue(BuildRange.fromStrings("1", "1").inRange(BuildNumber.fromString("1")));
assertTrue(BuildRange.fromStrings("1.2", "1.2").inRange(BuildNumber.fromString("1.2")));
assertTrue(BuildRange.fromStrings("1", "1.2").inRange(BuildNumber.fromString("1.0")));
assertTrue(BuildRange.fromStrings("1", "1.2").inRange(BuildNumber.fromString("1.2")));
assertTrue(BuildRange.fromStrings("1", "1.*").inRange(BuildNumber.fromString("1")));
assertTrue(BuildRange.fromStrings("1", "1.*").inRange(BuildNumber.fromString("1.999")));
assertTrue(BuildRange.fromStrings("1", "1.*").inRange(BuildNumber.fromString("1.SNAPSHOT")));
assertFalse(BuildRange.fromStrings("1", "1").inRange(BuildNumber.fromString("1.1")));
assertFalse(BuildRange.fromStrings("1.2", "1.2").inRange(BuildNumber.fromString("1.1")));
assertFalse(BuildRange.fromStrings("1.2", "1.2").inRange(BuildNumber.fromString("1.3")));
assertFalse(BuildRange.fromStrings("1.2", "1.2").inRange(BuildNumber.fromString("2")));
assertFalse(BuildRange.fromStrings("1", "1.*").inRange(BuildNumber.fromString("2")));
}
}