1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.gpg;
20
21 import java.util.Arrays;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25
26
27
28
29
30
31
32
33 public class GpgVersion implements Comparable<GpgVersion> {
34
35 private static final Pattern VERSION_PATTERN = Pattern.compile("(\\d+\\.)+(\\d+)");
36
37 private final int[] versionSegments;
38
39 private GpgVersion(int... versionSegments) {
40 this.versionSegments = versionSegments;
41 }
42
43 public static GpgVersion parse(String rawVersion) {
44 final Matcher versionMatcher = VERSION_PATTERN.matcher(rawVersion);
45 if (!versionMatcher.find()) {
46 throw new IllegalArgumentException("Can't parse version of " + rawVersion);
47 }
48
49 final String[] rawVersionSegments = versionMatcher.group(0).split("\\.");
50
51 final int[] versionSegments = new int[rawVersionSegments.length];
52 for (int index = 0; index < rawVersionSegments.length; index++) {
53 versionSegments[index] = Integer.parseInt(rawVersionSegments[index]);
54 }
55
56 return new GpgVersion(versionSegments);
57 }
58
59 @Override
60 public int compareTo(GpgVersion other) {
61 final int[] thisSegments = versionSegments;
62 final int[] otherSegments = other.versionSegments;
63
64 int minSegments = Math.min(thisSegments.length, otherSegments.length);
65
66 for (int index = 0; index < minSegments; index++) {
67 int compareValue = Integer.compare(thisSegments[index], otherSegments[index]);
68
69 if (compareValue != 0) {
70 return compareValue;
71 }
72 }
73
74 return (thisSegments.length - otherSegments.length);
75 }
76
77
78
79
80
81
82
83 public boolean isBefore(GpgVersion other) {
84 return this.compareTo(other) < 0;
85 }
86
87
88
89
90
91
92
93 public boolean isAtLeast(GpgVersion other) {
94 return this.compareTo(other) >= 0;
95 }
96
97 @Override
98 public String toString() {
99 if (versionSegments.length == 0) {
100 return "";
101 }
102
103 final StringBuilder versionStringBuilder = new StringBuilder();
104 versionStringBuilder.append(versionSegments[0]);
105
106 for (int index = 1; index < versionSegments.length; index++) {
107 versionStringBuilder.append('.').append(versionSegments[index]);
108 }
109
110 return versionStringBuilder.toString();
111 }
112
113 @Override
114 public boolean equals(final Object other) {
115 if (this == other) {
116 return true;
117 }
118
119 if (!(other instanceof GpgVersion)) {
120 return false;
121 }
122
123 final GpgVersion that = (GpgVersion) other;
124
125 return compareTo(that) == 0;
126 }
127
128 @Override
129 public int hashCode() {
130 return Arrays.hashCode(versionSegments);
131 }
132 }