7
7
import java .util .Collections ;
8
8
import java .util .HashMap ;
9
9
import java .util .Map ;
10
+ import java .util .Set ;
10
11
import java .util .TreeMap ;
12
+ import java .util .concurrent .ConcurrentHashMap ;
11
13
12
14
/**
13
15
* The <code>PrimitiveType</code> enumeration defines a mapping of limited set
@@ -175,6 +177,24 @@ public ObjectProperty createProperty() {
175
177
* Joda lib.
176
178
*/
177
179
private static final Map <String , PrimitiveType > EXTERNAL_CLASSES ;
180
+
181
+ /**
182
+ * Allows to exclude specific classes from KEY_CLASSES mappings to primitive
183
+ *
184
+ */
185
+ private static Set <String > customExcludedClasses = ConcurrentHashMap .newKeySet ();
186
+
187
+ /**
188
+ * Allows to exclude specific classes from EXTERNAL_CLASSES mappings to primitive
189
+ *
190
+ */
191
+ private static Set <String > customExcludedExternalClasses = ConcurrentHashMap .newKeySet ();
192
+
193
+ /**
194
+ * Adds support for custom mapping of classes to primitive types
195
+ */
196
+ private static Map <String , PrimitiveType > customClasses = new ConcurrentHashMap <>();
197
+
178
198
/**
179
199
* Alternative names for primitive types that have to be supported for
180
200
* backward compatibility.
@@ -212,7 +232,7 @@ public ObjectProperty createProperty() {
212
232
addKeys (externalClasses , DATE_TIME , "org.joda.time.DateTime" , "org.joda.time.ReadableDateTime" ,
213
233
"javax.xml.datatype.XMLGregorianCalendar" , "java.time.LocalDateTime" , "java.time.ZonedDateTime" ,
214
234
"java.time.OffsetDateTime" );
215
- addKeys (externalClasses , LONG , "java.time.Instant" );
235
+ addKeys (externalClasses , LONG , "java.time.Instant" );
216
236
EXTERNAL_CLASSES = Collections .unmodifiableMap (externalClasses );
217
237
218
238
final Map <String , PrimitiveType > names = new TreeMap <String , PrimitiveType >(String .CASE_INSENSITIVE_ORDER );
@@ -236,15 +256,50 @@ private PrimitiveType(Class<?> keyClass, String commonName) {
236
256
this .commonName = commonName ;
237
257
}
238
258
259
+ /**
260
+ * Adds support for custom mapping of classes to primitive types
261
+ *
262
+ * @return Set of custom classes to primitive type
263
+ */
264
+ public static Set <String > customExcludedClasses () {
265
+ return customExcludedClasses ;
266
+ }
267
+
268
+ /**
269
+ * Adds support for custom mapping of classes to primitive types
270
+ *
271
+ * @return Set of custom classes to primitive type
272
+ */
273
+ public static Set <String > customExcludedExternalClasses () {
274
+ return customExcludedExternalClasses ;
275
+ }
276
+
277
+ /**
278
+ * Adds support for custom mapping of classes to primitive types
279
+ *
280
+ * @return Map of custom classes to primitive type
281
+ */
282
+ public static Map <String , PrimitiveType > customClasses () {
283
+ return customClasses ;
284
+ }
285
+
239
286
public static PrimitiveType fromType (Type type ) {
240
287
final Class <?> raw = TypeFactory .defaultInstance ().constructType (type ).getRawClass ();
241
288
final PrimitiveType key = KEY_CLASSES .get (raw );
242
289
if (key != null ) {
243
- return key ;
290
+ if (!customExcludedClasses .contains (raw .getName ())) {
291
+ return key ;
292
+ }
293
+ }
294
+ final PrimitiveType custom = customClasses .get (raw .getName ());
295
+ if (custom != null ) {
296
+ return custom ;
244
297
}
245
298
final PrimitiveType external = EXTERNAL_CLASSES .get (raw .getName ());
246
299
if (external != null ) {
247
- return external ;
300
+ if (!customExcludedExternalClasses ().contains (raw .getName ())) {
301
+ return external ;
302
+ }
248
303
}
249
304
for (Map .Entry <Class <?>, PrimitiveType > entry : BASE_CLASSES .entrySet ()) {
250
305
if (entry .getKey ().isAssignableFrom (raw )) {
0 commit comments