1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package de.smartics.maven.issues.util;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22
23 import org.apache.maven.artifact.versioning.ArtifactVersion;
24 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
25 import org.apache.maven.artifact.versioning.Restriction;
26 import org.apache.maven.artifact.versioning.VersionRange;
27 import org.codehaus.plexus.util.StringUtils;
28
29
30
31
32
33
34
35 public final class Utils
36 {
37
38
39
40
41
42
43
44
45
46
47
48
49
50 private Utils()
51 {
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public static String normalizeKey(final String key)
72 {
73 if (key == null || "".equals(key))
74 {
75 return key;
76 }
77
78 final char[] trimmed = key.trim().toCharArray();
79 boolean uppercase = false;
80 int currentPos = 0;
81 for (int i = 0; i < trimmed.length; i++, currentPos++)
82 {
83 final char c = trimmed[i];
84 if (Character.isWhitespace(c))
85 {
86 uppercase = true;
87 --currentPos;
88 }
89 else if (uppercase)
90 {
91 trimmed[currentPos] = Character.toUpperCase(c);
92 uppercase = false;
93 }
94 else
95 {
96 trimmed[currentPos] = c;
97 }
98 }
99
100 return new String(trimmed).substring(0, currentPos);
101 }
102
103
104
105
106
107
108
109
110
111
112 public static List<String> splitToList(final String commaSeparatedValues)
113 {
114 if (StringUtils.isNotBlank(commaSeparatedValues))
115 {
116 final String[] items = commaSeparatedValues.split(",");
117 final List<String> list = new ArrayList<String>(items.length);
118 for (String item : items)
119 {
120 list.add(item.trim());
121 }
122 return list;
123 }
124 else
125 {
126 return Collections.emptyList();
127 }
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 @SuppressWarnings("unchecked")
148 public static ArtifactVersion findVersionRepresentative(
149 final VersionRange range) throws NullPointerException,
150 IllegalArgumentException
151 {
152 final List<Restriction> restrictions = range.getRestrictions();
153 final ArtifactVersion currentLowerBound;
154 final ArtifactVersion recommendedVersion = range.getRecommendedVersion();
155 if (recommendedVersion != null)
156 {
157 currentLowerBound = recommendedVersion;
158 }
159 else if (!restrictions.isEmpty())
160 {
161 currentLowerBound = handleRestriction(restrictions);
162 }
163 else
164 {
165 currentLowerBound = new DefaultArtifactVersion("0.0.0");
166 }
167
168 return currentLowerBound;
169 }
170
171
172
173
174
175
176
177
178
179
180 private static ArtifactVersion handleRestriction(
181 final List<Restriction> restrictions) throws IllegalArgumentException
182 {
183 final ArtifactVersion currentLowerBound;
184 final Restriction restriction = restrictions.get(0);
185 final ArtifactVersion lowerBound = restriction.getLowerBound();
186 if (lowerBound == null)
187 {
188 currentLowerBound = new DefaultArtifactVersion("0.0.0");
189 }
190 else if (restriction.isLowerBoundInclusive())
191 {
192 currentLowerBound = lowerBound;
193 }
194 else
195 {
196 currentLowerBound = handleUpperBound(restriction);
197 }
198 return currentLowerBound;
199 }
200
201
202
203
204
205
206
207
208
209
210 private static ArtifactVersion handleUpperBound(final Restriction restriction)
211 throws IllegalArgumentException
212 {
213 assert !restriction.isLowerBoundInclusive();
214 final ArtifactVersion upperBound = restriction.getUpperBound();
215 if (restriction.isUpperBoundInclusive() && upperBound != null)
216 {
217 return upperBound;
218 }
219 else
220 {
221 throw new IllegalArgumentException(
222 "At least one bound is required to be inclusive and not 'null'.");
223 }
224 }
225
226
227
228 }