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