From 06fa8561691639e02498e081176b03e44687b52b Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 5 Apr 2016 15:29:10 +0200 Subject: [PATCH] [util] build ranges BuildNumber.fromString() now returns null for empty strings, too. --- .../intellij/openapi/util/BuildNumber.java | 6 +- .../com/intellij/openapi/util/BuildRange.java | 47 +++++++++++++++ .../openapi/util/BuildNumberTest.java | 10 +++- .../intellij/openapi/util/BuildRangeTest.java | 60 +++++++++++++++++++ 4 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 platform/util/src/com/intellij/openapi/util/BuildRange.java create mode 100644 platform/util/testSrc/com/intellij/openapi/util/BuildRangeTest.java diff --git a/platform/util/src/com/intellij/openapi/util/BuildNumber.java b/platform/util/src/com/intellij/openapi/util/BuildNumber.java index c2b68b44c3c2..36e0f2c5583c 100644 --- a/platform/util/src/com/intellij/openapi/util/BuildNumber.java +++ b/platform/util/src/com/intellij/openapi/util/BuildNumber.java @@ -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 { } 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 { public String asStringWithAllDetails() { return asString(true, true); } -} +} \ No newline at end of file diff --git a/platform/util/src/com/intellij/openapi/util/BuildRange.java b/platform/util/src/com/intellij/openapi/util/BuildRange.java new file mode 100644 index 000000000000..c43fbc95e0e9 --- /dev/null +++ b/platform/util/src/com/intellij/openapi/util/BuildRange.java @@ -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; + } +} \ No newline at end of file diff --git a/platform/util/testSrc/com/intellij/openapi/util/BuildNumberTest.java b/platform/util/testSrc/com/intellij/openapi/util/BuildNumberTest.java index c5690842a29d..c2283c4fa02a 100644 --- a/platform/util/testSrc/com/intellij/openapi/util/BuildNumberTest.java +++ b/platform/util/testSrc/com/intellij/openapi/util/BuildNumberTest.java @@ -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); } -} +} \ No newline at end of file diff --git a/platform/util/testSrc/com/intellij/openapi/util/BuildRangeTest.java b/platform/util/testSrc/com/intellij/openapi/util/BuildRangeTest.java new file mode 100644 index 000000000000..4c63e89deeea --- /dev/null +++ b/platform/util/testSrc/com/intellij/openapi/util/BuildRangeTest.java @@ -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"))); + } +} \ No newline at end of file