1 package com.atlassian.plugins.rest.module;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.apache.commons.lang.builder.EqualsBuilder;
5 import org.apache.commons.lang.builder.HashCodeBuilder;
6
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
9
10 public class ApiVersion implements Comparable
11 {
12 private final static String DOT = ".";
13
14 private final static Pattern VERSION_PATTERN = Pattern.compile("(\\d+)(?:\\.(\\d+))?(?:\\.(\\d+))?(?:\\.([\\w-]*))?");
15
16 private final Integer major;
17
18 private final Integer minor;
19
20 private final Integer micro;
21
22 private final String classifier;
23
24 public ApiVersion(String version)
25 {
26 if (version == null)
27 {
28 throw new InvalidVersionException(version);
29 }
30
31 final Matcher matcher = VERSION_PATTERN.matcher(version);
32 if (!matcher.matches())
33 {
34 throw new InvalidVersionException(version);
35 }
36
37 major = Integer.valueOf(matcher.group(1));
38 minor = matcher.group(2) != null ? Integer.valueOf(matcher.group(2)) : null;
39 micro = matcher.group(3) != null ? Integer.valueOf(matcher.group(3)) : null;
40 classifier = matcher.group(4);
41 }
42
43 public Integer getMajor()
44 {
45 return major;
46 }
47
48 public Integer getMinor()
49 {
50 return minor;
51 }
52
53 public Integer getMicro()
54 {
55 return micro;
56 }
57
58 public String getClassifier()
59 {
60 return classifier;
61 }
62
63 @Override
64 public boolean equals(Object o)
65 {
66 if (o == null)
67 {
68 return false;
69 }
70 if (o == this)
71 {
72 return true;
73 }
74 if (o.getClass() != this.getClass())
75 {
76 return false;
77 }
78
79 final ApiVersion version = (ApiVersion) o;
80 return new EqualsBuilder()
81 .append(this.major, version.major)
82 .append(this.minor, version.minor)
83 .append(this.micro, version.micro)
84 .append(this.classifier, version.classifier).isEquals();
85 }
86
87 @Override
88 public int hashCode()
89 {
90 return new HashCodeBuilder(3, 41).append(major).append(minor).append(micro).append(classifier).toHashCode();
91 }
92
93 public int compareTo(Object o)
94 {
95 if (o == null)
96 {
97 return 1;
98 }
99 if (o == this)
100 {
101 return 0;
102 }
103 if (o.getClass() != this.getClass())
104 {
105 return 1;
106 }
107
108 final ApiVersion that = (ApiVersion) o;
109
110 final int majorDifference = compare(this.major, that.major);
111 if (majorDifference != 0)
112 {
113 return majorDifference;
114 }
115 final int minorDifference = compare(this.minor, that.minor);
116 if (minorDifference != 0)
117 {
118 return minorDifference;
119 }
120 final int microDifference = compare(this.micro, that.micro);
121 if (microDifference != 0)
122 {
123 return microDifference;
124 }
125 return compare(this.classifier, that.classifier);
126 }
127
128 private <T extends Comparable<T>> int compare(T n, T m)
129 {
130 if (n == null && m == null)
131 {
132 return 0;
133 }
134 if (n == null)
135 {
136 return -1;
137 }
138 if (m == null)
139 {
140 return +1;
141 }
142 return n.compareTo(m);
143 }
144
145 @Override
146 public String toString()
147 {
148 return major
149 + (minor != null ? DOT + minor : "")
150 + (micro != null ? DOT + micro : "")
151 + (StringUtils.isNotBlank(classifier) ? DOT + classifier : "");
152 }
153 }