Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.libg.version; |
| 2 | |
| 3 | import java.util.regex.*; |
| 4 | |
| 5 | public class Version implements Comparable<Version> { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 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.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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 15 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 16 | public static final Version emptyVersion = LOWEST; |
| 17 | public static final Version ONE = new Version(1, 0, 0); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 18 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 19 | public Version() { |
| 20 | this(0); |
| 21 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 22 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 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 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 29 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 30 | public Version(int major, int minor, int micro) { |
| 31 | this(major, minor, micro, null); |
| 32 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 33 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 34 | public Version(int major, int minor) { |
| 35 | this(major, minor, 0, null); |
| 36 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 37 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 38 | public Version(int major) { |
| 39 | this(major, 0, 0, null); |
| 40 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 41 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 42 | 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 47 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 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; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 53 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 54 | if (m.group(5) != null) |
| 55 | micro = Integer.parseInt(m.group(5)); |
| 56 | else |
| 57 | micro = 0; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 58 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 59 | qualifier = m.group(7); |
| 60 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 61 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 62 | public int getMajor() { |
| 63 | return major; |
| 64 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 65 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 66 | public int getMinor() { |
| 67 | return minor; |
| 68 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 69 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 70 | public int getMicro() { |
| 71 | return micro; |
| 72 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 73 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 74 | public String getQualifier() { |
| 75 | return qualifier; |
| 76 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 77 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 78 | public int compareTo(Version other) { |
| 79 | if (other == this) |
| 80 | return 0; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 81 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 82 | Version o = other; |
| 83 | if (major != o.major) |
| 84 | return major - o.major; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 85 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 86 | if (minor != o.minor) |
| 87 | return minor - o.minor; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 88 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 89 | if (micro != o.micro) |
| 90 | return micro - o.micro; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 91 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 92 | int c = 0; |
| 93 | if (qualifier != null) |
| 94 | c = 1; |
| 95 | if (o.qualifier != null) |
| 96 | c += 2; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 97 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 98 | 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 108 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 109 | 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 122 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 123 | public boolean equals(Object ot) { |
| 124 | if (!(ot instanceof Version)) |
| 125 | return false; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 126 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 127 | return compareTo((Version) ot) == 0; |
| 128 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 129 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 130 | 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 148 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | public Version getWithoutQualifier() { |
| 162 | return new Version(major, minor, micro); |
| 163 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 164 | } |