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 public class SemVer implements Comparable<SemVer> {
27 private final int myMajor;
28 private final int myMinor;
29 private final int myPatch;
30 private final String myRawVersion;
32 public SemVer(@NotNull String rawVersion, int major, int minor, int patch) {
33 myRawVersion = rawVersion;
40 public String getRawVersion() {
44 public int getMajor() {
48 public int getMinor() {
52 public int getPatch() {
57 public String getParsedVersion() {
58 return myMajor + "." + myMinor + "." + myPatch;
62 public boolean equals(Object o) {
63 if (this == o) return true;
64 if (o == null || getClass() != o.getClass()) return false;
66 SemVer semVer = (SemVer)o;
68 if (myMajor != semVer.myMajor) return false;
69 if (myMinor != semVer.myMinor) return false;
70 if (myPatch != semVer.myPatch) return false;
76 public int hashCode() {
78 result = 31 * result + myMinor;
79 result = 31 * result + myPatch;
84 public String toString() {
89 public static SemVer parseFromText(@NotNull String text) {
90 String[] comps = text.split(Pattern.quote("."), 3);
91 if (comps.length != 3) {
94 Integer major = toInteger(comps[0]);
95 Integer minor = toInteger(comps[1]);
96 String patchStr = comps[2];
97 int dashInd = patchStr.indexOf('-');
99 patchStr = patchStr.substring(0, dashInd);
101 Integer patch = toInteger(patchStr);
102 if (major != null && minor != null && patch != null) {
103 return new SemVer(text, major, minor, patch);
108 private static Integer toInteger(@NotNull String str) {
110 return Integer.parseInt(str);
112 catch (NumberFormatException e) {
119 public int compareTo(SemVer other) {
120 // null is not permitted
121 if (getMajor() != other.getMajor()) {
122 return getMajor() - other.getMajor();
124 if (getMinor() != other.getMinor()) {
125 return getMinor() - other.getMinor();
127 if (getPatch() != other.getPatch()) {
128 return getPatch() - other.getPatch();