1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util;
17
18 import org.apache.commons.lang3.StringUtils;
19
20 public class VersionUtils {
21
22 public static final String MAVEN_SNAPSHOT_TOKEN = "SNAPSHOT";
23 private static final String[] DELIMITERS = new String[] { ".", "-" };
24 private static final String SEPARATOR_CHARS = Str.toString(DELIMITERS);
25
26
27
28
29 public static final boolean isSnapshot(String version) {
30 for (String delimiter : DELIMITERS) {
31 String suffix = delimiter + MAVEN_SNAPSHOT_TOKEN;
32 if (StringUtils.endsWithIgnoreCase(version, suffix)) {
33 return true;
34 }
35 }
36 return false;
37 }
38
39
40
41
42
43
44
45
46
47
48
49 public static final String trimSnapshot(String version) {
50 if (isSnapshot(version)) {
51 int length = MAVEN_SNAPSHOT_TOKEN.length() + 1;
52 return StringUtils.left(version, version.length() - length);
53 } else {
54 return version;
55 }
56 }
57
58
59
60
61
62
63
64 public static final Version getVersion(String version) {
65 boolean snapshot = isSnapshot(version);
66 String trimmed = trimSnapshot(version);
67 Version v = new Version();
68 v.setTrimmed(trimmed);
69 v.setSnapshot(snapshot);
70 String[] tokens = StringUtils.split(trimmed, SEPARATOR_CHARS);
71 if (tokens.length > 0) {
72 v.setMajor(tokens[0]);
73 }
74 if (tokens.length > 1) {
75 v.setMinor(tokens[1]);
76 }
77 if (tokens.length > 2) {
78 v.setIncremental(tokens[2]);
79 }
80 String qualifier = getQualifier(trimmed, tokens);
81 v.setQualifier(qualifier);
82 return v;
83 }
84
85 protected static final String getQualifier(String trimmed, String[] tokens) {
86 if (tokens.length < 4) {
87 return null;
88 }
89 int pos = tokens[0].length() + 1 + tokens[1].length() + 1 + tokens[2].length() + 1;
90 return trimmed.substring(pos);
91 }
92
93 }