blob: 951ad258e3bfb61528044e08fb93139c005b1dea [file] [log] [blame]
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00001package aQute.bnd.osgi;
Stuart McCullochbb014372012-06-07 21:57:32 +00002
3import java.util.regex.*;
4
5public class Version implements Comparable<Version> {
Stuart McCulloch2286f232012-06-15 13:27:53 +00006 final int major;
7 final int minor;
8 final int micro;
9 final String qualifier;
10 public final static String VERSION_STRING = "(\\d+)(\\.(\\d+)(\\.(\\d+)(\\.([-_\\da-zA-Z]+))?)?)?";
11 public final static Pattern VERSION = Pattern.compile(VERSION_STRING);
12 public final static Version LOWEST = new Version();
13 public final static Version HIGHEST = new Version(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE,
14 "\uFFFF");
Stuart McCullochbb014372012-06-07 21:57:32 +000015
Stuart McCulloch2286f232012-06-15 13:27:53 +000016 public static final Version emptyVersion = LOWEST;
17 public static final Version ONE = new Version(1, 0, 0);
Stuart McCullochbb014372012-06-07 21:57:32 +000018
Stuart McCulloch2286f232012-06-15 13:27:53 +000019 public Version() {
20 this(0);
21 }
Stuart McCullochbb014372012-06-07 21:57:32 +000022
Stuart McCulloch2286f232012-06-15 13:27:53 +000023 public Version(int major, int minor, int micro, String qualifier) {
24 this.major = major;
25 this.minor = minor;
26 this.micro = micro;
27 this.qualifier = qualifier;
28 }
Stuart McCullochbb014372012-06-07 21:57:32 +000029
Stuart McCulloch2286f232012-06-15 13:27:53 +000030 public Version(int major, int minor, int micro) {
31 this(major, minor, micro, null);
32 }
Stuart McCullochbb014372012-06-07 21:57:32 +000033
Stuart McCulloch2286f232012-06-15 13:27:53 +000034 public Version(int major, int minor) {
35 this(major, minor, 0, null);
36 }
Stuart McCullochbb014372012-06-07 21:57:32 +000037
Stuart McCulloch2286f232012-06-15 13:27:53 +000038 public Version(int major) {
39 this(major, 0, 0, null);
40 }
Stuart McCullochbb014372012-06-07 21:57:32 +000041
Stuart McCulloch2286f232012-06-15 13:27:53 +000042 public Version(String version) {
43 version = version.trim();
44 Matcher m = VERSION.matcher(version);
45 if (!m.matches())
46 throw new IllegalArgumentException("Invalid syntax for version: " + version);
Stuart McCullochbb014372012-06-07 21:57:32 +000047
Stuart McCulloch2286f232012-06-15 13:27:53 +000048 major = Integer.parseInt(m.group(1));
49 if (m.group(3) != null)
50 minor = Integer.parseInt(m.group(3));
51 else
52 minor = 0;
Stuart McCullochbb014372012-06-07 21:57:32 +000053
Stuart McCulloch2286f232012-06-15 13:27:53 +000054 if (m.group(5) != null)
55 micro = Integer.parseInt(m.group(5));
56 else
57 micro = 0;
Stuart McCullochbb014372012-06-07 21:57:32 +000058
Stuart McCulloch2286f232012-06-15 13:27:53 +000059 qualifier = m.group(7);
60 }
Stuart McCullochbb014372012-06-07 21:57:32 +000061
Stuart McCulloch2286f232012-06-15 13:27:53 +000062 public int getMajor() {
63 return major;
64 }
Stuart McCullochbb014372012-06-07 21:57:32 +000065
Stuart McCulloch2286f232012-06-15 13:27:53 +000066 public int getMinor() {
67 return minor;
68 }
Stuart McCullochbb014372012-06-07 21:57:32 +000069
Stuart McCulloch2286f232012-06-15 13:27:53 +000070 public int getMicro() {
71 return micro;
72 }
Stuart McCullochbb014372012-06-07 21:57:32 +000073
Stuart McCulloch2286f232012-06-15 13:27:53 +000074 public String getQualifier() {
75 return qualifier;
76 }
Stuart McCullochbb014372012-06-07 21:57:32 +000077
Stuart McCulloch2286f232012-06-15 13:27:53 +000078 public int compareTo(Version other) {
79 if (other == this)
80 return 0;
Stuart McCullochbb014372012-06-07 21:57:32 +000081
Stuart McCulloch2286f232012-06-15 13:27:53 +000082 Version o = other;
83 if (major != o.major)
84 return major - o.major;
Stuart McCullochbb014372012-06-07 21:57:32 +000085
Stuart McCulloch2286f232012-06-15 13:27:53 +000086 if (minor != o.minor)
87 return minor - o.minor;
Stuart McCullochbb014372012-06-07 21:57:32 +000088
Stuart McCulloch2286f232012-06-15 13:27:53 +000089 if (micro != o.micro)
90 return micro - o.micro;
Stuart McCullochbb014372012-06-07 21:57:32 +000091
Stuart McCulloch2286f232012-06-15 13:27:53 +000092 int c = 0;
93 if (qualifier != null)
94 c = 1;
95 if (o.qualifier != null)
96 c += 2;
Stuart McCullochbb014372012-06-07 21:57:32 +000097
Stuart McCulloch2286f232012-06-15 13:27:53 +000098 switch (c) {
99 case 0 :
100 return 0;
101 case 1 :
102 return 1;
103 case 2 :
104 return -1;
105 }
106 return qualifier.compareTo(o.qualifier);
107 }
Stuart McCullochbb014372012-06-07 21:57:32 +0000108
Stuart McCulloch2286f232012-06-15 13:27:53 +0000109 public String toString() {
110 StringBuilder sb = new StringBuilder();
111 sb.append(major);
112 sb.append(".");
113 sb.append(minor);
114 sb.append(".");
115 sb.append(micro);
116 if (qualifier != null) {
117 sb.append(".");
118 sb.append(qualifier);
119 }
120 return sb.toString();
121 }
Stuart McCullochbb014372012-06-07 21:57:32 +0000122
Stuart McCulloch2286f232012-06-15 13:27:53 +0000123 public boolean equals(Object ot) {
124 if (!(ot instanceof Version))
125 return false;
Stuart McCullochbb014372012-06-07 21:57:32 +0000126
Stuart McCulloch2286f232012-06-15 13:27:53 +0000127 return compareTo((Version) ot) == 0;
128 }
Stuart McCullochbb014372012-06-07 21:57:32 +0000129
Stuart McCulloch2286f232012-06-15 13:27:53 +0000130 public int hashCode() {
131 return major * 97 ^ minor * 13 ^ micro + (qualifier == null ? 97 : qualifier.hashCode());
132 }
133
134 public int get(int i) {
135 switch (i) {
136 case 0 :
137 return major;
138 case 1 :
139 return minor;
140 case 2 :
141 return micro;
142 default :
143 throw new IllegalArgumentException("Version can only get 0 (major), 1 (minor), or 2 (micro)");
144 }
145 }
146
147 public static Version parseVersion(String version) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000148 if (version == null) {
149 return LOWEST;
150 }
151
152 version = version.trim();
153 if (version.length() == 0) {
154 return LOWEST;
155 }
156
157 return new Version(version);
158
Stuart McCulloch2286f232012-06-15 13:27:53 +0000159 }
160
161 public Version getWithoutQualifier() {
162 return new Version(major, minor, micro);
163 }
Stuart McCullochbb014372012-06-07 21:57:32 +0000164}