1 package com.fasterxml.jackson.databind.cfg;
2
3 import java.text.DateFormat;
4 import java.util.Locale;
5 import java.util.TimeZone;
6
7 import com.fasterxml.jackson.core.Base64Variant;
8 import com.fasterxml.jackson.databind.*;
9 import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
10 import com.fasterxml.jackson.databind.introspect.ClassIntrospector;
11 import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
12 import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
13 import com.fasterxml.jackson.databind.type.TypeFactory;
14 import com.fasterxml.jackson.databind.util.StdDateFormat;
15
16
21 public final class BaseSettings
22 implements java.io.Serializable
23 {
24
25 private static final long serialVersionUID = 1L;
26
27
30 private static final TimeZone DEFAULT_TIMEZONE =
31
32
35 TimeZone.getTimeZone("UTC");
36
37
42
43
48 protected final ClassIntrospector _classIntrospector;
49
50
53 protected final AnnotationIntrospector _annotationIntrospector;
54
55
58 protected final PropertyNamingStrategy _propertyNamingStrategy;
59
60
65 protected final TypeFactory _typeFactory;
66
67
72
73
77 protected final TypeResolverBuilder<?> _typeResolverBuilder;
78
79
85 protected final PolymorphicTypeValidator _typeValidator;
86
87
92
93
100 protected final DateFormat _dateFormat;
101
102
108 protected final HandlerInstantiator _handlerInstantiator;
109
110
114 protected final Locale _locale;
115
116
125 protected final TimeZone _timeZone;
126
127
134 protected final Base64Variant _defaultBase64;
135
136
141
142
145 public BaseSettings(ClassIntrospector ci, AnnotationIntrospector ai,
146 PropertyNamingStrategy pns, TypeFactory tf,
147 TypeResolverBuilder<?> typer, DateFormat dateFormat, HandlerInstantiator hi,
148 Locale locale, TimeZone tz, Base64Variant defaultBase64,
149 PolymorphicTypeValidator ptv)
150 {
151 _classIntrospector = ci;
152 _annotationIntrospector = ai;
153 _propertyNamingStrategy = pns;
154 _typeFactory = tf;
155 _typeResolverBuilder = typer;
156 _dateFormat = dateFormat;
157 _handlerInstantiator = hi;
158 _locale = locale;
159 _timeZone = tz;
160 _defaultBase64 = defaultBase64;
161 _typeValidator = ptv;
162 }
163
164 @Deprecated
165 public BaseSettings(ClassIntrospector ci, AnnotationIntrospector ai,
166 PropertyNamingStrategy pns, TypeFactory tf,
167 TypeResolverBuilder<?> typer, DateFormat dateFormat, HandlerInstantiator hi,
168 Locale locale, TimeZone tz, Base64Variant defaultBase64)
169 {
170 this(ci, ai, pns, tf, typer, dateFormat, hi, locale, tz, defaultBase64, null);
171 }
172
173
179 public BaseSettings copy() {
180 return new BaseSettings(_classIntrospector.copy(),
181 _annotationIntrospector,
182 _propertyNamingStrategy,
183 _typeFactory,
184 _typeResolverBuilder,
185 _dateFormat,
186 _handlerInstantiator,
187 _locale,
188 _timeZone,
189 _defaultBase64,
190 _typeValidator);
191
192 }
193
194
199
200 public BaseSettings withClassIntrospector(ClassIntrospector ci) {
201 if (_classIntrospector == ci) {
202 return this;
203 }
204 return new BaseSettings(ci, _annotationIntrospector, _propertyNamingStrategy, _typeFactory,
205 _typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
206 _timeZone, _defaultBase64, _typeValidator);
207 }
208
209 public BaseSettings withAnnotationIntrospector(AnnotationIntrospector ai) {
210 if (_annotationIntrospector == ai) {
211 return this;
212 }
213 return new BaseSettings(_classIntrospector, ai, _propertyNamingStrategy, _typeFactory,
214 _typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
215 _timeZone, _defaultBase64, _typeValidator);
216 }
217
218 public BaseSettings withInsertedAnnotationIntrospector(AnnotationIntrospector ai) {
219 return withAnnotationIntrospector(AnnotationIntrospectorPair.create(ai, _annotationIntrospector));
220 }
221
222 public BaseSettings withAppendedAnnotationIntrospector(AnnotationIntrospector ai) {
223 return withAnnotationIntrospector(AnnotationIntrospectorPair.create(_annotationIntrospector, ai));
224 }
225
226
235
236 public BaseSettings withPropertyNamingStrategy(PropertyNamingStrategy pns) {
237 if (_propertyNamingStrategy == pns) {
238 return this;
239 }
240 return new BaseSettings(_classIntrospector, _annotationIntrospector, pns, _typeFactory,
241 _typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
242 _timeZone, _defaultBase64, _typeValidator);
243 }
244
245 public BaseSettings withTypeFactory(TypeFactory tf) {
246 if (_typeFactory == tf) {
247 return this;
248 }
249 return new BaseSettings(_classIntrospector, _annotationIntrospector, _propertyNamingStrategy, tf,
250 _typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
251 _timeZone, _defaultBase64, _typeValidator);
252 }
253
254 public BaseSettings withTypeResolverBuilder(TypeResolverBuilder<?> typer) {
255 if (_typeResolverBuilder == typer) {
256 return this;
257 }
258 return new BaseSettings(_classIntrospector, _annotationIntrospector, _propertyNamingStrategy, _typeFactory,
259 typer, _dateFormat, _handlerInstantiator, _locale,
260 _timeZone, _defaultBase64, _typeValidator);
261 }
262
263 public BaseSettings withDateFormat(DateFormat df) {
264 if (_dateFormat == df) {
265 return this;
266 }
267
268
269 if ((df != null) && hasExplicitTimeZone()) {
270 df = _force(df, _timeZone);
271 }
272 return new BaseSettings(_classIntrospector, _annotationIntrospector, _propertyNamingStrategy, _typeFactory,
273 _typeResolverBuilder, df, _handlerInstantiator, _locale,
274 _timeZone, _defaultBase64, _typeValidator);
275 }
276
277 public BaseSettings withHandlerInstantiator(HandlerInstantiator hi) {
278 if (_handlerInstantiator == hi) {
279 return this;
280 }
281 return new BaseSettings(_classIntrospector, _annotationIntrospector, _propertyNamingStrategy, _typeFactory,
282 _typeResolverBuilder, _dateFormat, hi, _locale,
283 _timeZone, _defaultBase64, _typeValidator);
284 }
285
286 public BaseSettings with(Locale l) {
287 if (_locale == l) {
288 return this;
289 }
290 return new BaseSettings(_classIntrospector, _annotationIntrospector, _propertyNamingStrategy, _typeFactory,
291 _typeResolverBuilder, _dateFormat, _handlerInstantiator, l,
292 _timeZone, _defaultBase64, _typeValidator);
293 }
294
295
300 public BaseSettings with(TimeZone tz)
301 {
302 if (tz == null) {
303 throw new IllegalArgumentException();
304 }
305 if (tz == _timeZone) {
306 return this;
307 }
308
309 DateFormat df = _force(_dateFormat, tz);
310 return new BaseSettings(_classIntrospector, _annotationIntrospector,
311 _propertyNamingStrategy, _typeFactory,
312 _typeResolverBuilder, df, _handlerInstantiator, _locale,
313 tz, _defaultBase64, _typeValidator);
314 }
315
316
319 public BaseSettings with(Base64Variant base64) {
320 if (base64 == _defaultBase64) {
321 return this;
322 }
323 return new BaseSettings(_classIntrospector, _annotationIntrospector,
324 _propertyNamingStrategy, _typeFactory,
325 _typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
326 _timeZone, base64, _typeValidator);
327 }
328
329
332 public BaseSettings with(PolymorphicTypeValidator v) {
333 if (v == _typeValidator) {
334 return this;
335 }
336 return new BaseSettings(_classIntrospector, _annotationIntrospector,
337 _propertyNamingStrategy, _typeFactory,
338 _typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
339 _timeZone, _defaultBase64, v);
340 }
341
342
347
348 public ClassIntrospector getClassIntrospector() {
349 return _classIntrospector;
350 }
351
352 public AnnotationIntrospector getAnnotationIntrospector() {
353 return _annotationIntrospector;
354 }
355
356 public PropertyNamingStrategy getPropertyNamingStrategy() {
357 return _propertyNamingStrategy;
358 }
359
360 public TypeFactory getTypeFactory() {
361 return _typeFactory;
362 }
363
364 public TypeResolverBuilder<?> getTypeResolverBuilder() {
365 return _typeResolverBuilder;
366 }
367
368
371 public PolymorphicTypeValidator getPolymorphicTypeValidator() {
372 return _typeValidator;
373 }
374
375 public DateFormat getDateFormat() {
376 return _dateFormat;
377 }
378
379 public HandlerInstantiator getHandlerInstantiator() {
380 return _handlerInstantiator;
381 }
382
383 public Locale getLocale() {
384 return _locale;
385 }
386
387 public TimeZone getTimeZone() {
388 TimeZone tz = _timeZone;
389 return (tz == null) ? DEFAULT_TIMEZONE : tz;
390 }
391
392
399 public boolean hasExplicitTimeZone() {
400 return (_timeZone != null);
401 }
402
403 public Base64Variant getBase64Variant() {
404 return _defaultBase64;
405 }
406
407
412
413 private DateFormat _force(DateFormat df, TimeZone tz)
414 {
415 if (df instanceof StdDateFormat) {
416 return ((StdDateFormat) df).withTimeZone(tz);
417 }
418
419 df = (DateFormat) df.clone();
420 df.setTimeZone(tz);
421 return df;
422 }
423 }
424