2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2009 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.updatecenter.common;
22 import org.apache.commons.lang.StringUtils;
24 public class Version implements Comparable<Version> {
26 private String normalizedMajor = normalizePart("0");
27 private String normalizedMinor = normalizePart("0");
28 private String normalizedPatch = normalizePart("0");
29 private String normalizedPatch2 = normalizePart("0");
30 private String major = "0";
31 private String minor = "0";
32 private String patch = "0";
33 private String patch2 = "0";
36 private Version(String version) {
37 this.name = StringUtils.trimToEmpty(version);
38 this.name = StringUtils.substringBefore(this.name, "-"); // we don't care about snapshots and RCs for now
39 String[] split = StringUtils.split(name, '.');
40 if (split.length >= 1) {
42 normalizedMajor = normalizePart(major);
44 if (split.length >= 2) {
46 normalizedMinor = normalizePart(minor);
48 if (split.length >= 3) {
50 normalizedPatch = normalizePart(patch);
52 if (split.length >= 4) {
54 normalizedPatch2 = normalizePart(patch2);
58 private static String normalizePart(String part) {
59 return StringUtils.leftPad(part, 4, '0');
62 public String getMajor() {
66 public String getMinor() {
70 public String getPatch() {
74 public String getPatch2() {
78 public String getName() {
83 public boolean equals(Object o) {
87 if (o == null || getClass() != o.getClass()) {
91 Version version = (Version) o;
92 if ( !normalizedMajor.equals(version.normalizedMajor)) {
95 if ( !normalizedMinor.equals(version.normalizedMinor)) {
98 if ( !normalizedPatch.equals(version.normalizedPatch)) {
101 if ( !normalizedPatch2.equals(version.normalizedPatch2)) {
108 public int hashCode() {
109 int result = normalizedMajor.hashCode();
110 result = 31 * result + normalizedMinor.hashCode();
111 result = 31 * result + normalizedPatch.hashCode();
112 result = 31 * result + normalizedPatch2.hashCode();
116 public int compareTo(Version other) {
117 // TODO : manage RC, alpha, ...
118 int c = normalizedMajor.compareTo(other.normalizedMajor);
120 c = normalizedMinor.compareTo(other.normalizedMinor);
122 c = normalizedPatch.compareTo(other.normalizedPatch);
124 c = normalizedPatch2.compareTo(other.normalizedPatch2);
132 public String toString() {
136 public static Version create(String version) {
137 return new Version(version);