-
Notifications
You must be signed in to change notification settings - Fork 45
/
Utilities.java
262 lines (235 loc) · 7.79 KB
/
Utilities.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2016 the original authors or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sarl.eclipse.util;
import com.google.common.base.Strings;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeParameter;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.osgi.framework.Bundle;
import org.osgi.framework.Version;
import io.sarl.lang.SARLConfig;
/** Utilities.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
public final class Utilities {
/** Empty string.
*/
public static final String EMPTY_STRING = ""; //$NON-NLS-1$
private Utilities() {
//
}
/** Null-safe version parser.
*
* @param version - the version string.
* @return the version.
*/
public static Version parseVersion(String version) {
if (!Strings.isNullOrEmpty(version)) {
try {
return Version.parseVersion(version);
} catch (Throwable exception) {
//
}
}
return null;
}
/** Null-safe compare a version number to a range of version numbers.
*
* <p>The minVersion must be strictly lower to the maxVersion. Otherwise
* the behavior is not predictible.
*
* @param version - the version to compare to the range; must not be <code>null</code>.
* @param minVersion - the minimal version in the range (inclusive); could be <code>null</code>.
* @param maxVersion - the maximal version in the range (exclusive); could be <code>null</code>.
* @return a negative number if the version in lower than the minVersion.
* A positive number if the version is greater than or equal to the maxVersion.
* <code>0</code> if the version is between minVersion and maxVersion.
*/
public static int compareVersionToRange(Version version, Version minVersion, Version maxVersion) {
assert minVersion == null || maxVersion == null || minVersion.compareTo(maxVersion) < 0;
if (version == null) {
return Integer.MIN_VALUE;
}
if (minVersion != null && compareVersionsNoQualifier(version, minVersion) < 0) {
return -1;
}
if (maxVersion != null && compareVersionsNoQualifier(version, maxVersion) >= 0) {
return 1;
}
return 0;
}
private static int compareVersionsNoQualifier(Version firstVersion, Version secondVersion) {
if (firstVersion == secondVersion) {
return 0;
}
int result = firstVersion.getMajor() - secondVersion.getMajor();
if (result != 0) {
return result;
}
result = firstVersion.getMinor() - secondVersion.getMinor();
if (result != 0) {
return result;
}
return firstVersion.getMicro() - secondVersion.getMicro();
}
/** Null-safe comparison.
*
* @param <T> - type of the comparable element.
* @param object1 - the first object.
* @param object2 - the second object.
* @return Negative number if a lower than b.
* Positive number if a greater than b.
* <code>0</code> if a is equal to b.
*/
public static <T> int compareTo(Comparable<T> object1, T object2) {
if (object1 == object2) {
return 0;
}
if (object1 == null) {
return Integer.MIN_VALUE;
}
if (object2 == null) {
return Integer.MAX_VALUE;
}
assert object1 != null && object2 != null;
return object1.compareTo(object2);
}
/** Replies the fully qualified name with generic parameters.
*
* @param type the type. Never <code>null</code>.
* @return the qualified name.
*/
public static String getNameWithTypeParameters(IType type) {
assert type != null;
final String superName = type.getFullyQualifiedName('.');
if (!JavaModelUtil.is50OrHigher(type.getJavaProject())) {
return superName;
}
try {
final ITypeParameter[] typeParameters = type.getTypeParameters();
if (typeParameters != null && typeParameters.length > 0) {
final StringBuffer buf = new StringBuffer(superName);
buf.append('<');
for (int k = 0; k < typeParameters.length; ++k) {
if (k != 0) {
buf.append(',').append(' ');
}
buf.append(typeParameters[k].getElementName());
}
buf.append('>');
return buf.toString();
}
} catch (JavaModelException e) {
// ignore
}
return superName;
}
/** Create the classpath library linked to the bundle with the given name.
*
* @param bundle the bundle to point to. Never <code>null</code>.
* @param precomputedBundlePath the path to the bundle that is already available. If <code>null</code>,
* the path is computed from the bundle with {@link BundleUtil}.
* @param javadocURLs the mappings from the bundle to the javadoc URL. It is used for linking the javadoc to the bundle if
* the bundle platform does not know the Javadoc file. If <code>null</code>, no mapping is defined.
* @return the classpath entry.
*/
public static IClasspathEntry newLibraryEntry(Bundle bundle, IPath precomputedBundlePath, BundleURLMappings javadocURLs) {
assert bundle != null;
final IPath bundlePath;
if (precomputedBundlePath == null) {
bundlePath = BundleUtil.getBundlePath(bundle);
} else {
bundlePath = precomputedBundlePath;
}
final IPath sourceBundlePath = BundleUtil.getSourceBundlePath(bundle, bundlePath);
final IPath javadocPath = BundleUtil.getJavadocBundlePath(bundle, bundlePath);
final IClasspathAttribute[] extraAttributes;
if (javadocPath == null) {
if (javadocURLs != null) {
final String url = javadocURLs.getURLForBundle(bundle);
if (!Strings.isNullOrEmpty(url)) {
final IClasspathAttribute attr = JavaCore.newClasspathAttribute(
IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME,
url);
extraAttributes = new IClasspathAttribute[] {attr};
} else {
extraAttributes = new IClasspathAttribute[0];
}
} else {
extraAttributes = new IClasspathAttribute[0];
}
} else {
final IClasspathAttribute attr = JavaCore.newClasspathAttribute(
IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME,
javadocPath.makeAbsolute().toOSString());
extraAttributes = new IClasspathAttribute[] {attr};
}
return JavaCore.newLibraryEntry(
bundlePath,
sourceBundlePath,
null,
null,
extraAttributes,
false);
}
/** Define a mapping from bundles to URLs.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
@FunctionalInterface
public interface BundleURLMappings {
/** Replies the URL for the given bundle.
*
* @param bundle the bundle, never <code>null</code>.
* @return the URL, or <code>null</code> if no URL is defined.
*/
String getURLForBundle(Bundle bundle);
}
/** Define a mapping from bundles to URLs.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
public static class SARLBundleJavadocURLMappings implements BundleURLMappings {
private static final String SARL_PREFIX = "io.sarl."; //$NON-NLS-1$
@Override
public String getURLForBundle(Bundle bundle) {
if (bundle.getSymbolicName().startsWith(SARL_PREFIX)) {
return SARLConfig.JAVADOC_URL;
}
return null;
}
}
}