mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Version.parseVersion now understands various suffixes (1.0-beta, 1.0beta, 1.beta)
This commit is contained in:
@@ -4,9 +4,6 @@ import com.intellij.openapi.util.text.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class Version {
|
||||
public final int major;
|
||||
public final int minor;
|
||||
@@ -22,23 +19,27 @@ public class Version {
|
||||
public static Version parseVersion(@NotNull String versionString) {
|
||||
String[] versions = versionString.split("\\.");
|
||||
String version = versions[0];
|
||||
int major = StringUtil.parseInt(version, -1);
|
||||
int major = parseNumber(version, -1);
|
||||
if (major < 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int minor = (versions.length > 1) ? StringUtil.parseInt(versions[1], -1) : 0;
|
||||
int minor = (versions.length > 1) ? parseNumber(versions[1], -1) : 0;
|
||||
if (minor < 0) {
|
||||
return null;
|
||||
return new Version(major, 0, 0);
|
||||
}
|
||||
|
||||
int patch = (versions.length > 2) ? StringUtil.parseInt(versions[2], -1) : 0;
|
||||
int patch = (versions.length > 2) ? parseNumber(versions[2], -1) : 0;
|
||||
if (patch < 0) {
|
||||
return null;
|
||||
return new Version(major, minor, 0);
|
||||
}
|
||||
|
||||
return new Version(major, minor, patch);
|
||||
}
|
||||
|
||||
private static int parseNumber(String num, int def) {
|
||||
return StringUtil.parseInt(num.replaceFirst("(\\d+).*", "$1"), def);
|
||||
}
|
||||
|
||||
public boolean is(@Nullable Integer major) {
|
||||
return is(major, null);
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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.util;
|
||||
|
||||
import com.intellij.openapi.util.Version;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class VersionTest extends TestCase {
|
||||
public void testParseVersion() throws Exception {
|
||||
assertEquals(new Version(1, 0, 0), Version.parseVersion("1"));
|
||||
assertEquals(new Version(1, 2, 0), Version.parseVersion("1.2"));
|
||||
assertEquals(new Version(1, 2, 3), Version.parseVersion("1.2.3"));
|
||||
assertEquals(new Version(1, 2, 3), Version.parseVersion("1.2.3.4"));
|
||||
|
||||
assertEquals(new Version(1, 0, 0), Version.parseVersion("1beta"));
|
||||
assertEquals(new Version(1, 2, 0), Version.parseVersion("1.2beta"));
|
||||
assertEquals(new Version(1, 2, 3), Version.parseVersion("1.2.3beta"));
|
||||
assertEquals(new Version(1, 2, 3), Version.parseVersion("1.2.3.4beta"));
|
||||
|
||||
assertEquals(new Version(1, 0, 0), Version.parseVersion("1-beta"));
|
||||
assertEquals(new Version(1, 2, 0), Version.parseVersion("1.2-beta"));
|
||||
assertEquals(new Version(1, 2, 3), Version.parseVersion("1.2.3-beta"));
|
||||
assertEquals(new Version(1, 2, 3), Version.parseVersion("1.2.3.4-beta"));
|
||||
|
||||
assertEquals(new Version(1, 0, 0), Version.parseVersion("1.beta"));
|
||||
assertEquals(new Version(1, 2, 0), Version.parseVersion("1.2.beta"));
|
||||
assertEquals(new Version(1, 2, 3), Version.parseVersion("1.2.3.beta"));
|
||||
|
||||
assertEquals(null, Version.parseVersion(""));
|
||||
assertEquals(null, Version.parseVersion("beta1"));
|
||||
assertEquals(null, Version.parseVersion("beta.beta.beta"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user