44
44
*/
45
45
public final class DefaultCodecs implements Codecs , CodecRegistry {
46
46
47
- private final List <Codec <?>> codecs ;
48
-
49
47
private final CodecLookup codecLookup ;
50
48
49
+ private final List <Codec <?>> codecs ;
50
+
51
51
/**
52
52
* Create a new instance of {@link DefaultCodecs} preferring detached (copied buffers).
53
53
*
@@ -96,97 +96,6 @@ public DefaultCodecs(ByteBufAllocator byteBufAllocator, boolean preferAttachedBu
96
96
this .codecLookup .afterCodecAdded ();
97
97
}
98
98
99
- @ SuppressWarnings ({"unchecked" , "rawtypes" })
100
- private static List <Codec <?>> getDefaultCodecs (ByteBufAllocator byteBufAllocator , boolean preferAttachedBuffers , CodecConfiguration configuration ) {
101
-
102
- List <Codec <?>> codecs = new CopyOnWriteArrayList <>(Arrays .asList (
103
-
104
- // Prioritized Codecs
105
- new StringCodec (byteBufAllocator ),
106
- new InstantCodec (byteBufAllocator , configuration ::getZoneId ),
107
- new ZonedDateTimeCodec (byteBufAllocator ),
108
- new BinaryByteBufferCodec (byteBufAllocator ),
109
- new BinaryByteArrayCodec (byteBufAllocator ),
110
-
111
- new BigDecimalCodec (byteBufAllocator ),
112
- new BigIntegerCodec (byteBufAllocator ),
113
- new BooleanCodec (byteBufAllocator ),
114
- new CharacterCodec (byteBufAllocator ),
115
- new DoubleCodec (byteBufAllocator ),
116
- new FloatCodec (byteBufAllocator ),
117
- new InetAddressCodec (byteBufAllocator ),
118
- new IntegerCodec (byteBufAllocator ),
119
- new IntervalCodec (byteBufAllocator ),
120
- new LocalDateCodec (byteBufAllocator ),
121
- new LocalDateTimeCodec (byteBufAllocator , configuration ::getZoneId ),
122
- new LocalTimeCodec (byteBufAllocator ),
123
- new LongCodec (byteBufAllocator ),
124
- new OffsetDateTimeCodec (byteBufAllocator ),
125
- new OffsetTimeCodec (byteBufAllocator ),
126
- new ShortCodec (byteBufAllocator ),
127
- new UriCodec (byteBufAllocator ),
128
- new UrlCodec (byteBufAllocator ),
129
- new UuidCodec (byteBufAllocator ),
130
- new ZoneIdCodec (byteBufAllocator ),
131
-
132
- // JSON
133
- new JsonCodec (byteBufAllocator , preferAttachedBuffers ),
134
- new JsonByteArrayCodec (byteBufAllocator ),
135
- new JsonByteBufCodec (byteBufAllocator ),
136
- new JsonByteBufferCodec (byteBufAllocator ),
137
- new JsonInputStreamCodec (byteBufAllocator ),
138
- new JsonStringCodec (byteBufAllocator ),
139
-
140
- // Fallback for Object.class
141
- new ByteCodec (byteBufAllocator ),
142
- new DateCodec (byteBufAllocator , configuration ::getZoneId ),
143
-
144
- new BlobCodec (byteBufAllocator ),
145
- new ClobCodec (byteBufAllocator ),
146
- RefCursorCodec .INSTANCE ,
147
- RefCursorNameCodec .INSTANCE ,
148
-
149
- // Array
150
- new StringArrayCodec (byteBufAllocator ),
151
-
152
- // Geometry
153
- new CircleCodec (byteBufAllocator ),
154
- new PointCodec (byteBufAllocator ),
155
- new BoxCodec (byteBufAllocator ),
156
- new LineCodec (byteBufAllocator ),
157
- new LsegCodec (byteBufAllocator ),
158
- new PathCodec (byteBufAllocator ),
159
- new PolygonCodec (byteBufAllocator )
160
- ));
161
-
162
- List <Codec <?>> defaultArrayCodecs = new ArrayList <>();
163
-
164
- for (Codec <?> codec : codecs ) {
165
-
166
- if (codec instanceof ArrayCodecDelegate <?>) {
167
-
168
- Assert .requireType (codec , AbstractCodec .class , "Codec " + codec + " must be a subclass of AbstractCodec to be registered as generic array codec" );
169
- ArrayCodecDelegate <?> delegate = (ArrayCodecDelegate <?>) codec ;
170
- Class <?> componentType = delegate .type ();
171
-
172
- if (codec instanceof BoxCodec ) {
173
- // BOX[] uses a ';' as a delimiter (i.e. "{(3.7,4.6),(1.9,2.8);(5,7),(1.5,3.3)}")
174
- defaultArrayCodecs .add (new ArrayCodec (byteBufAllocator , delegate .getArrayDataType (), delegate , componentType , (byte ) ';' ));
175
- } else if (codec instanceof AbstractNumericCodec ) {
176
- defaultArrayCodecs .add (new ConvertingArrayCodec (byteBufAllocator , delegate , componentType , ConvertingArrayCodec .NUMERIC_ARRAY_TYPES ));
177
- } else if (codec instanceof AbstractTemporalCodec ) {
178
- defaultArrayCodecs .add (new ConvertingArrayCodec (byteBufAllocator , delegate , componentType , ConvertingArrayCodec .DATE_ARRAY_TYPES ));
179
- } else {
180
- defaultArrayCodecs .add (new ArrayCodec (byteBufAllocator , delegate , componentType ));
181
- }
182
- }
183
- }
184
-
185
- codecs .addAll (defaultArrayCodecs );
186
-
187
- return codecs ;
188
- }
189
-
190
99
@ Override
191
100
public void addFirst (Codec <?> codec ) {
192
101
Assert .requireNonNull (codec , "codec must not be null" );
@@ -264,6 +173,35 @@ public EncodedParameter encode(Object value) {
264
173
return encodeParameterValue (value , dataType , parameterValue );
265
174
}
266
175
176
+ @ Override
177
+ public EncodedParameter encodeNull (Class <?> type ) {
178
+ Assert .requireNonNull (type , "type must not be null" );
179
+
180
+ Codec <?> codec = this .codecLookup .findEncodeNullCodec (type );
181
+ if (codec != null ) {
182
+ return codec .encodeNull ();
183
+ }
184
+
185
+ throw new IllegalArgumentException (String .format ("Cannot encode null parameter of type %s" , type .getName ()));
186
+ }
187
+
188
+ @ Override
189
+ public Iterator <Codec <?>> iterator () {
190
+ return Collections .unmodifiableList (new ArrayList <>(this .codecs )).iterator ();
191
+ }
192
+
193
+ @ Override
194
+ public Class <?> preferredType (int dataType , Format format ) {
195
+ Assert .requireNonNull (format , "format must not be null" );
196
+
197
+ Codec <?> codec = this .codecLookup .findDecodeCodec (dataType , format , Object .class );
198
+ if (codec instanceof CodecMetadata ) {
199
+ return ((CodecMetadata ) codec ).type ();
200
+ }
201
+
202
+ return null ;
203
+ }
204
+
267
205
EncodedParameter encodeParameterValue (Object value , @ Nullable PostgresTypeIdentifier dataType , @ Nullable Object parameterValue ) {
268
206
if (dataType == null ) {
269
207
@@ -290,33 +228,101 @@ EncodedParameter encodeParameterValue(Object value, @Nullable PostgresTypeIdenti
290
228
throw new IllegalArgumentException (String .format ("Cannot encode parameter of type %s (%s)" , value .getClass ().getName (), parameterValue ));
291
229
}
292
230
293
- @ Override
294
- public EncodedParameter encodeNull (Class <?> type ) {
295
- Assert .requireNonNull (type , "type must not be null" );
231
+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
232
+ private static List <Codec <?>> getDefaultCodecs (ByteBufAllocator byteBufAllocator , boolean preferAttachedBuffers , CodecConfiguration configuration ) {
296
233
297
- Codec <?> codec = this .codecLookup .findEncodeNullCodec (type );
298
- if (codec != null ) {
299
- return codec .encodeNull ();
300
- }
234
+ List <Codec <?>> codecs = new CopyOnWriteArrayList <>(Arrays .asList (
301
235
302
- throw new IllegalArgumentException (String .format ("Cannot encode null parameter of type %s" , type .getName ()));
303
- }
236
+ // Prioritized Codecs
237
+ new StringCodec (byteBufAllocator ),
238
+ new InstantCodec (byteBufAllocator , configuration ::getZoneId ),
239
+ new ZonedDateTimeCodec (byteBufAllocator ),
240
+ new BinaryByteBufferCodec (byteBufAllocator ),
241
+ new BinaryByteArrayCodec (byteBufAllocator ),
304
242
305
- @ Override
306
- public Class <?> preferredType (int dataType , Format format ) {
307
- Assert .requireNonNull (format , "format must not be null" );
243
+ new BigDecimalCodec (byteBufAllocator ),
244
+ new BigIntegerCodec (byteBufAllocator ),
245
+ new BooleanCodec (byteBufAllocator ),
246
+ new CharacterCodec (byteBufAllocator ),
247
+ new DoubleCodec (byteBufAllocator ),
248
+ new FloatCodec (byteBufAllocator ),
249
+ new InetAddressCodec (byteBufAllocator ),
250
+ new IntegerCodec (byteBufAllocator ),
251
+ new IntervalCodec (byteBufAllocator ),
252
+ new LocalDateCodec (byteBufAllocator ),
253
+ new LocalDateTimeCodec (byteBufAllocator , configuration ::getZoneId ),
254
+ new LocalTimeCodec (byteBufAllocator ),
255
+ new LongCodec (byteBufAllocator ),
256
+ new OffsetDateTimeCodec (byteBufAllocator ),
257
+ new OffsetTimeCodec (byteBufAllocator ),
258
+ new ShortCodec (byteBufAllocator ),
259
+ new UriCodec (byteBufAllocator ),
260
+ new UrlCodec (byteBufAllocator ),
261
+ new UuidCodec (byteBufAllocator ),
262
+ new ZoneIdCodec (byteBufAllocator ),
263
+ new DayOfWeekCodec (byteBufAllocator ),
264
+ new MonthCodec (byteBufAllocator ),
265
+ new MonthDayCodec (byteBufAllocator ),
266
+ new PeriodCodec (byteBufAllocator ),
267
+ new YearCodec (byteBufAllocator ),
268
+ new YearMonthCodec (byteBufAllocator ),
308
269
309
- Codec <?> codec = this .codecLookup .findDecodeCodec (dataType , format , Object .class );
310
- if (codec instanceof CodecMetadata ) {
311
- return ((CodecMetadata ) codec ).type ();
270
+ // JSON
271
+ new JsonCodec (byteBufAllocator , preferAttachedBuffers ),
272
+ new JsonByteArrayCodec (byteBufAllocator ),
273
+ new JsonByteBufCodec (byteBufAllocator ),
274
+ new JsonByteBufferCodec (byteBufAllocator ),
275
+ new JsonInputStreamCodec (byteBufAllocator ),
276
+ new JsonStringCodec (byteBufAllocator ),
277
+
278
+ // Fallback for Object.class
279
+ new ByteCodec (byteBufAllocator ),
280
+ new DateCodec (byteBufAllocator , configuration ::getZoneId ),
281
+
282
+ new BlobCodec (byteBufAllocator ),
283
+ new ClobCodec (byteBufAllocator ),
284
+ RefCursorCodec .INSTANCE ,
285
+ RefCursorNameCodec .INSTANCE ,
286
+
287
+ // Array
288
+ new StringArrayCodec (byteBufAllocator ),
289
+
290
+ // Geometry
291
+ new CircleCodec (byteBufAllocator ),
292
+ new PointCodec (byteBufAllocator ),
293
+ new BoxCodec (byteBufAllocator ),
294
+ new LineCodec (byteBufAllocator ),
295
+ new LsegCodec (byteBufAllocator ),
296
+ new PathCodec (byteBufAllocator ),
297
+ new PolygonCodec (byteBufAllocator )
298
+ ));
299
+
300
+ List <Codec <?>> defaultArrayCodecs = new ArrayList <>();
301
+
302
+ for (Codec <?> codec : codecs ) {
303
+
304
+ if (codec instanceof ArrayCodecDelegate <?>) {
305
+
306
+ Assert .requireType (codec , AbstractCodec .class , "Codec " + codec + " must be a subclass of AbstractCodec to be registered as generic array codec" );
307
+ ArrayCodecDelegate <?> delegate = (ArrayCodecDelegate <?>) codec ;
308
+ Class <?> componentType = delegate .type ();
309
+
310
+ if (codec instanceof BoxCodec ) {
311
+ // BOX[] uses a ';' as a delimiter (i.e. "{(3.7,4.6),(1.9,2.8);(5,7),(1.5,3.3)}")
312
+ defaultArrayCodecs .add (new ArrayCodec (byteBufAllocator , delegate .getArrayDataType (), delegate , componentType , (byte ) ';' ));
313
+ } else if (codec instanceof AbstractNumericCodec ) {
314
+ defaultArrayCodecs .add (new ConvertingArrayCodec (byteBufAllocator , delegate , componentType , ConvertingArrayCodec .NUMERIC_ARRAY_TYPES ));
315
+ } else if (codec instanceof AbstractTemporalCodec ) {
316
+ defaultArrayCodecs .add (new ConvertingArrayCodec (byteBufAllocator , delegate , componentType , ConvertingArrayCodec .DATE_ARRAY_TYPES ));
317
+ } else {
318
+ defaultArrayCodecs .add (new ArrayCodec (byteBufAllocator , delegate , componentType ));
319
+ }
320
+ }
312
321
}
313
322
314
- return null ;
315
- }
323
+ codecs .addAll (defaultArrayCodecs );
316
324
317
- @ Override
318
- public Iterator <Codec <?>> iterator () {
319
- return Collections .unmodifiableList (new ArrayList <>(this .codecs )).iterator ();
325
+ return codecs ;
320
326
}
321
327
322
328
}
0 commit comments