blob: 4f087a09bebd21f2b00b459247bbdc6d36413893 [file] [log] [blame]
Stuart McCulloch26e7a5a2011-10-17 10:31:43 +00001package aQute.libg.version;
2
3import java.util.regex.*;
4
5public class Version implements Comparable<Version> {
6 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
12 .compile(VERSION_STRING);
13 public final static Version LOWEST = new Version();
14 public final static Version HIGHEST = new Version(Integer.MAX_VALUE,
15 Integer.MAX_VALUE,
16 Integer.MAX_VALUE,
17 "\uFFFF");
18
19 public Version() {
20 this(0);
21 }
22
23 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 }
29
30 public Version(int major, int minor, int micro) {
31 this(major, minor, micro, null);
32 }
33
34 public Version(int major, int minor) {
35 this(major, minor, 0, null);
36 }
37
38 public Version(int major) {
39 this(major, 0, 0, null);
40 }
41
42 public Version(String version) {
43 Matcher m = VERSION.matcher(version);
44 if (!m.matches())
45 throw new IllegalArgumentException("Invalid syntax for version: "
46 + version);
47
48 major = Integer.parseInt(m.group(1));
49 if (m.group(3) != null)
50 minor = Integer.parseInt(m.group(3));
51 else
52 minor = 0;
53
54 if (m.group(5) != null)
55 micro = Integer.parseInt(m.group(5));
56 else
57 micro = 0;
58
59 qualifier = m.group(7);
60 }
61
62 public int getMajor() {
63 return major;
64 }
65
66 public int getMinor() {
67 return minor;
68 }
69
70 public int getMicro() {
71 return micro;
72 }
73
74 public String getQualifier() {
75 return qualifier;
76 }
77
78 public int compareTo(Version other) {
79 if (other == this)
80 return 0;
81
82 if (!(other instanceof Version))
83 throw new IllegalArgumentException(
84 "Can only compare versions to versions");
85
86 Version o = (Version) other;
87 if (major != o.major)
88 return major - o.major;
89
90 if (minor != o.minor)
91 return minor - o.minor;
92
93 if (micro != o.micro)
94 return micro - o.micro;
95
96 int c = 0;
97 if (qualifier != null)
98 c = 1;
99 if (o.qualifier != null)
100 c += 2;
101
102 switch (c) {
103 case 0:
104 return 0;
105 case 1:
106 return 1;
107 case 2:
108 return -1;
109 }
110 return qualifier.compareTo(o.qualifier);
111 }
112
113 public String toString() {
114 StringBuffer sb = new StringBuffer();
115 sb.append(major);
116 sb.append(".");
117 sb.append(minor);
118 sb.append(".");
119 sb.append(micro);
120 if (qualifier != null) {
121 sb.append(".");
122 sb.append(qualifier);
123 }
124 return sb.toString();
125 }
126
127 public boolean equals(Object ot) {
128 if ( ! (ot instanceof Version))
129 return false;
130
131 return compareTo((Version)ot) == 0;
132 }
133
134 public int hashCode() {
135 return major * 97 ^ minor * 13 ^ micro
136 + (qualifier == null ? 97 : qualifier.hashCode());
137 }
138
139 public int get(int i) {
140 switch(i) {
141 case 0 : return major;
142 case 1 : return minor;
143 case 2 : return micro;
144 default:
145 throw new IllegalArgumentException("Version can only get 0 (major), 1 (minor), or 2 (micro)");
146 }
147 }
148}