2 * Copyright 2000-2013 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.util.text;
18 import org.jetbrains.annotations.NotNull;
19 import org.jetbrains.annotations.Nullable;
21 import java.util.regex.Pattern;
24 * See http://semver.org
26 * @author Sergey Simonchik
29 private final int myMajor;
30 private final int myMinor;
31 private final int myPatch;
32 private final String myRawVersion;
34 public SemVer(@NotNull String rawVersion, int major, int minor, int patch) {
35 myRawVersion = rawVersion;
42 public String getRawVersion() {
46 public int getMajor() {
50 public int getMinor() {
54 public int getPatch() {
59 public String getParsedVersion() {
60 return myMajor + "." + myMinor + "." + myPatch;
64 public boolean equals(Object o) {
65 if (this == o) return true;
66 if (o == null || getClass() != o.getClass()) return false;
68 SemVer semVer = (SemVer)o;
70 if (myMajor != semVer.myMajor) return false;
71 if (myMinor != semVer.myMinor) return false;
72 if (myPatch != semVer.myPatch) return false;
78 public int hashCode() {
80 result = 31 * result + myMinor;
81 result = 31 * result + myPatch;
86 public String toString() {
91 public static SemVer parseFromText(@NotNull String text) {
92 String[] comps = text.split(Pattern.quote("."), 3);
93 if (comps.length != 3) {
96 Integer major = toInteger(comps[0]);
97 Integer minor = toInteger(comps[1]);
98 String patchStr = comps[2];
99 int dashInd = patchStr.indexOf('-');
101 patchStr = patchStr.substring(0, dashInd);
103 Integer patch = toInteger(patchStr);
104 if (major != null && minor != null && patch != null) {
105 return new SemVer(text, major, minor, patch);
110 private static Integer toInteger(@NotNull String str) {
112 return Integer.parseInt(str);
114 catch (NumberFormatException e) {