blob: 7ec3ba103ee017dd66fee0435ea5d7d70ba07a23 [file] [log] [blame]
Stuart McCullochcd1ddd72012-07-19 13:11:20 +00001package aQute.bnd.version;
Stuart McCullochf3173222012-06-07 21:57:32 +00002
3import java.util.regex.*;
4
5public class Version implements Comparable<Version> {
Stuart McCulloch4482c702012-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 McCullochf3173222012-06-07 21:57:32 +000015
Stuart McCulloch4482c702012-06-15 13:27:53 +000016 public static final Version emptyVersion = LOWEST;
17 public static final Version ONE = new Version(1, 0, 0);
Stuart McCullochf3173222012-06-07 21:57:32 +000018
Stuart McCulloch4482c702012-06-15 13:27:53 +000019 public Version() {
20 this(0);
21 }
Stuart McCullochf3173222012-06-07 21:57:32 +000022
Stuart McCulloch4482c702012-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 McCullochf3173222012-06-07 21:57:32 +000029
Stuart McCulloch4482c702012-06-15 13:27:53 +000030 public Version(int major, int minor, int micro) {
31 this(major, minor, micro, null);
32 }
Stuart McCullochf3173222012-06-07 21:57:32 +000033
Stuart McCulloch4482c702012-06-15 13:27:53 +000034 public Version(int major, int minor) {
35 this(major, minor, 0, null);
36 }
Stuart McCullochf3173222012-06-07 21:57:32 +000037
Stuart McCulloch4482c702012-06-15 13:27:53 +000038 public Version(int major) {
39 this(major, 0, 0, null);
40 }
Stuart McCullochf3173222012-06-07 21:57:32 +000041
Stuart McCulloch4482c702012-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 McCullochf3173222012-06-07 21:57:32 +000047
Stuart McCulloch4482c702012-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 McCullochf3173222012-06-07 21:57:32 +000053
Stuart McCulloch4482c702012-06-15 13:27:53 +000054 if (m.group(5) != null)
55 micro = Integer.parseInt(m.group(5));
56 else
57 micro = 0;
Stuart McCullochf3173222012-06-07 21:57:32 +000058
Stuart McCulloch4482c702012-06-15 13:27:53 +000059 qualifier = m.group(7);
60 }
Stuart McCullochf3173222012-06-07 21:57:32 +000061
Stuart McCulloch4482c702012-06-15 13:27:53 +000062 public int getMajor() {
63 return major;
64 }
Stuart McCullochf3173222012-06-07 21:57:32 +000065
Stuart McCulloch4482c702012-06-15 13:27:53 +000066 public int getMinor() {
67 return minor;
68 }
Stuart McCullochf3173222012-06-07 21:57:32 +000069
Stuart McCulloch4482c702012-06-15 13:27:53 +000070 public int getMicro() {
71 return micro;
72 }
Stuart McCullochf3173222012-06-07 21:57:32 +000073
Stuart McCulloch4482c702012-06-15 13:27:53 +000074 public String getQualifier() {
75 return qualifier;
76 }
Stuart McCullochf3173222012-06-07 21:57:32 +000077
Stuart McCulloch4482c702012-06-15 13:27:53 +000078 public int compareTo(Version other) {
79 if (other == this)
80 return 0;
Stuart McCullochf3173222012-06-07 21:57:32 +000081
Stuart McCulloch4482c702012-06-15 13:27:53 +000082 Version o = other;
83 if (major != o.major)
84 return major - o.major;
Stuart McCullochf3173222012-06-07 21:57:32 +000085
Stuart McCulloch4482c702012-06-15 13:27:53 +000086 if (minor != o.minor)
87 return minor - o.minor;
Stuart McCullochf3173222012-06-07 21:57:32 +000088
Stuart McCulloch4482c702012-06-15 13:27:53 +000089 if (micro != o.micro)
90 return micro - o.micro;
Stuart McCullochf3173222012-06-07 21:57:32 +000091
Stuart McCulloch4482c702012-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 McCullochf3173222012-06-07 21:57:32 +000097
Stuart McCulloch4482c702012-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 McCullochf3173222012-06-07 21:57:32 +0000108
Stuart McCulloch2929e2d2012-08-07 10:57:21 +0000109 @Override
Stuart McCulloch4482c702012-06-15 13:27:53 +0000110 public String toString() {
111 StringBuilder sb = new StringBuilder();
112 sb.append(major);
113 sb.append(".");
114 sb.append(minor);
115 sb.append(".");
116 sb.append(micro);
117 if (qualifier != null) {
118 sb.append(".");
119 sb.append(qualifier);
120 }
121 return sb.toString();
122 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000123
Stuart McCulloch2929e2d2012-08-07 10:57:21 +0000124 @Override
Stuart McCulloch4482c702012-06-15 13:27:53 +0000125 public boolean equals(Object ot) {
126 if (!(ot instanceof Version))
127 return false;
Stuart McCullochf3173222012-06-07 21:57:32 +0000128
Stuart McCulloch4482c702012-06-15 13:27:53 +0000129 return compareTo((Version) ot) == 0;
130 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000131
Stuart McCulloch2929e2d2012-08-07 10:57:21 +0000132 @Override
Stuart McCulloch4482c702012-06-15 13:27:53 +0000133 public int hashCode() {
134 return major * 97 ^ minor * 13 ^ micro + (qualifier == null ? 97 : qualifier.hashCode());
135 }
136
137 public int get(int i) {
138 switch (i) {
139 case 0 :
140 return major;
141 case 1 :
142 return minor;
143 case 2 :
144 return micro;
145 default :
146 throw new IllegalArgumentException("Version can only get 0 (major), 1 (minor), or 2 (micro)");
147 }
148 }
149
150 public static Version parseVersion(String version) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000151 if (version == null) {
152 return LOWEST;
153 }
154
155 version = version.trim();
156 if (version.length() == 0) {
157 return LOWEST;
158 }
159
160 return new Version(version);
161
Stuart McCulloch4482c702012-06-15 13:27:53 +0000162 }
163
164 public Version getWithoutQualifier() {
165 return new Version(major, minor, micro);
166 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000167}