From 4b698d93cd44b681b3815b4d69afd8c53e19c701 Mon Sep 17 00:00:00 2001 From: steverigney Date: Sat, 2 Sep 2023 12:33:20 +0100 Subject: [PATCH] undoing accidental reformatting of Default codecs - gh-591 --- .../r2dbc/postgresql/codec/DefaultCodecs.java | 238 +++++++++--------- 1 file changed, 119 insertions(+), 119 deletions(-) diff --git a/src/main/java/io/r2dbc/postgresql/codec/DefaultCodecs.java b/src/main/java/io/r2dbc/postgresql/codec/DefaultCodecs.java index 16e24074..1001ba57 100644 --- a/src/main/java/io/r2dbc/postgresql/codec/DefaultCodecs.java +++ b/src/main/java/io/r2dbc/postgresql/codec/DefaultCodecs.java @@ -44,10 +44,10 @@ */ public final class DefaultCodecs implements Codecs, CodecRegistry { - private final CodecLookup codecLookup; - private final List> codecs; + private final CodecLookup codecLookup; + /** * Create a new instance of {@link DefaultCodecs} preferring detached (copied buffers). * @@ -96,6 +96,103 @@ public DefaultCodecs(ByteBufAllocator byteBufAllocator, boolean preferAttachedBu this.codecLookup.afterCodecAdded(); } + @SuppressWarnings({"unchecked", "rawtypes"}) + private static List> getDefaultCodecs(ByteBufAllocator byteBufAllocator, boolean preferAttachedBuffers, CodecConfiguration configuration) { + + List> codecs = new CopyOnWriteArrayList<>(Arrays.asList( + + // Prioritized Codecs + new StringCodec(byteBufAllocator), + new InstantCodec(byteBufAllocator, configuration::getZoneId), + new ZonedDateTimeCodec(byteBufAllocator), + new BinaryByteBufferCodec(byteBufAllocator), + new BinaryByteArrayCodec(byteBufAllocator), + + new BigDecimalCodec(byteBufAllocator), + new BigIntegerCodec(byteBufAllocator), + new BooleanCodec(byteBufAllocator), + new CharacterCodec(byteBufAllocator), + new DoubleCodec(byteBufAllocator), + new FloatCodec(byteBufAllocator), + new InetAddressCodec(byteBufAllocator), + new IntegerCodec(byteBufAllocator), + new IntervalCodec(byteBufAllocator), + new LocalDateCodec(byteBufAllocator), + new LocalDateTimeCodec(byteBufAllocator, configuration::getZoneId), + new LocalTimeCodec(byteBufAllocator), + new LongCodec(byteBufAllocator), + new OffsetDateTimeCodec(byteBufAllocator), + new OffsetTimeCodec(byteBufAllocator), + new ShortCodec(byteBufAllocator), + new UriCodec(byteBufAllocator), + new UrlCodec(byteBufAllocator), + new UuidCodec(byteBufAllocator), + new ZoneIdCodec(byteBufAllocator), + new DayOfWeekCodec(byteBufAllocator), + new MonthCodec(byteBufAllocator), + new MonthDayCodec(byteBufAllocator), + new PeriodCodec(byteBufAllocator), + new YearCodec(byteBufAllocator), + new YearMonthCodec(byteBufAllocator), + + // JSON + new JsonCodec(byteBufAllocator, preferAttachedBuffers), + new JsonByteArrayCodec(byteBufAllocator), + new JsonByteBufCodec(byteBufAllocator), + new JsonByteBufferCodec(byteBufAllocator), + new JsonInputStreamCodec(byteBufAllocator), + new JsonStringCodec(byteBufAllocator), + + // Fallback for Object.class + new ByteCodec(byteBufAllocator), + new DateCodec(byteBufAllocator, configuration::getZoneId), + + new BlobCodec(byteBufAllocator), + new ClobCodec(byteBufAllocator), + RefCursorCodec.INSTANCE, + RefCursorNameCodec.INSTANCE, + + // Array + new StringArrayCodec(byteBufAllocator), + + // Geometry + new CircleCodec(byteBufAllocator), + new PointCodec(byteBufAllocator), + new BoxCodec(byteBufAllocator), + new LineCodec(byteBufAllocator), + new LsegCodec(byteBufAllocator), + new PathCodec(byteBufAllocator), + new PolygonCodec(byteBufAllocator) + )); + + List> defaultArrayCodecs = new ArrayList<>(); + + for (Codec codec : codecs) { + + if (codec instanceof ArrayCodecDelegate) { + + Assert.requireType(codec, AbstractCodec.class, "Codec " + codec + " must be a subclass of AbstractCodec to be registered as generic array codec"); + ArrayCodecDelegate delegate = (ArrayCodecDelegate) codec; + Class componentType = delegate.type(); + + if (codec instanceof BoxCodec) { + // BOX[] uses a ';' as a delimiter (i.e. "{(3.7,4.6),(1.9,2.8);(5,7),(1.5,3.3)}") + defaultArrayCodecs.add(new ArrayCodec(byteBufAllocator, delegate.getArrayDataType(), delegate, componentType, (byte) ';')); + } else if (codec instanceof AbstractNumericCodec) { + defaultArrayCodecs.add(new ConvertingArrayCodec(byteBufAllocator, delegate, componentType, ConvertingArrayCodec.NUMERIC_ARRAY_TYPES)); + } else if (codec instanceof AbstractTemporalCodec) { + defaultArrayCodecs.add(new ConvertingArrayCodec(byteBufAllocator, delegate, componentType, ConvertingArrayCodec.DATE_ARRAY_TYPES)); + } else { + defaultArrayCodecs.add(new ArrayCodec(byteBufAllocator, delegate, componentType)); + } + } + } + + codecs.addAll(defaultArrayCodecs); + + return codecs; + } + @Override public void addFirst(Codec codec) { Assert.requireNonNull(codec, "codec must not be null"); @@ -173,35 +270,6 @@ public EncodedParameter encode(Object value) { return encodeParameterValue(value, dataType, parameterValue); } - @Override - public EncodedParameter encodeNull(Class type) { - Assert.requireNonNull(type, "type must not be null"); - - Codec codec = this.codecLookup.findEncodeNullCodec(type); - if (codec != null) { - return codec.encodeNull(); - } - - throw new IllegalArgumentException(String.format("Cannot encode null parameter of type %s", type.getName())); - } - - @Override - public Iterator> iterator() { - return Collections.unmodifiableList(new ArrayList<>(this.codecs)).iterator(); - } - - @Override - public Class preferredType(int dataType, Format format) { - Assert.requireNonNull(format, "format must not be null"); - - Codec codec = this.codecLookup.findDecodeCodec(dataType, format, Object.class); - if (codec instanceof CodecMetadata) { - return ((CodecMetadata) codec).type(); - } - - return null; - } - EncodedParameter encodeParameterValue(Object value, @Nullable PostgresTypeIdentifier dataType, @Nullable Object parameterValue) { if (dataType == null) { @@ -228,101 +296,33 @@ EncodedParameter encodeParameterValue(Object value, @Nullable PostgresTypeIdenti throw new IllegalArgumentException(String.format("Cannot encode parameter of type %s (%s)", value.getClass().getName(), parameterValue)); } - @SuppressWarnings({"unchecked", "rawtypes"}) - private static List> getDefaultCodecs(ByteBufAllocator byteBufAllocator, boolean preferAttachedBuffers, CodecConfiguration configuration) { - - List> codecs = new CopyOnWriteArrayList<>(Arrays.asList( - - // Prioritized Codecs - new StringCodec(byteBufAllocator), - new InstantCodec(byteBufAllocator, configuration::getZoneId), - new ZonedDateTimeCodec(byteBufAllocator), - new BinaryByteBufferCodec(byteBufAllocator), - new BinaryByteArrayCodec(byteBufAllocator), - - new BigDecimalCodec(byteBufAllocator), - new BigIntegerCodec(byteBufAllocator), - new BooleanCodec(byteBufAllocator), - new CharacterCodec(byteBufAllocator), - new DoubleCodec(byteBufAllocator), - new FloatCodec(byteBufAllocator), - new InetAddressCodec(byteBufAllocator), - new IntegerCodec(byteBufAllocator), - new IntervalCodec(byteBufAllocator), - new LocalDateCodec(byteBufAllocator), - new LocalDateTimeCodec(byteBufAllocator, configuration::getZoneId), - new LocalTimeCodec(byteBufAllocator), - new LongCodec(byteBufAllocator), - new OffsetDateTimeCodec(byteBufAllocator), - new OffsetTimeCodec(byteBufAllocator), - new ShortCodec(byteBufAllocator), - new UriCodec(byteBufAllocator), - new UrlCodec(byteBufAllocator), - new UuidCodec(byteBufAllocator), - new ZoneIdCodec(byteBufAllocator), - new DayOfWeekCodec(byteBufAllocator), - new MonthCodec(byteBufAllocator), - new MonthDayCodec(byteBufAllocator), - new PeriodCodec(byteBufAllocator), - new YearCodec(byteBufAllocator), - new YearMonthCodec(byteBufAllocator), - - // JSON - new JsonCodec(byteBufAllocator, preferAttachedBuffers), - new JsonByteArrayCodec(byteBufAllocator), - new JsonByteBufCodec(byteBufAllocator), - new JsonByteBufferCodec(byteBufAllocator), - new JsonInputStreamCodec(byteBufAllocator), - new JsonStringCodec(byteBufAllocator), - - // Fallback for Object.class - new ByteCodec(byteBufAllocator), - new DateCodec(byteBufAllocator, configuration::getZoneId), - - new BlobCodec(byteBufAllocator), - new ClobCodec(byteBufAllocator), - RefCursorCodec.INSTANCE, - RefCursorNameCodec.INSTANCE, - - // Array - new StringArrayCodec(byteBufAllocator), - - // Geometry - new CircleCodec(byteBufAllocator), - new PointCodec(byteBufAllocator), - new BoxCodec(byteBufAllocator), - new LineCodec(byteBufAllocator), - new LsegCodec(byteBufAllocator), - new PathCodec(byteBufAllocator), - new PolygonCodec(byteBufAllocator) - )); - - List> defaultArrayCodecs = new ArrayList<>(); + @Override + public EncodedParameter encodeNull(Class type) { + Assert.requireNonNull(type, "type must not be null"); - for (Codec codec : codecs) { + Codec codec = this.codecLookup.findEncodeNullCodec(type); + if (codec != null) { + return codec.encodeNull(); + } - if (codec instanceof ArrayCodecDelegate) { + throw new IllegalArgumentException(String.format("Cannot encode null parameter of type %s", type.getName())); + } - Assert.requireType(codec, AbstractCodec.class, "Codec " + codec + " must be a subclass of AbstractCodec to be registered as generic array codec"); - ArrayCodecDelegate delegate = (ArrayCodecDelegate) codec; - Class componentType = delegate.type(); + @Override + public Class preferredType(int dataType, Format format) { + Assert.requireNonNull(format, "format must not be null"); - if (codec instanceof BoxCodec) { - // BOX[] uses a ';' as a delimiter (i.e. "{(3.7,4.6),(1.9,2.8);(5,7),(1.5,3.3)}") - defaultArrayCodecs.add(new ArrayCodec(byteBufAllocator, delegate.getArrayDataType(), delegate, componentType, (byte) ';')); - } else if (codec instanceof AbstractNumericCodec) { - defaultArrayCodecs.add(new ConvertingArrayCodec(byteBufAllocator, delegate, componentType, ConvertingArrayCodec.NUMERIC_ARRAY_TYPES)); - } else if (codec instanceof AbstractTemporalCodec) { - defaultArrayCodecs.add(new ConvertingArrayCodec(byteBufAllocator, delegate, componentType, ConvertingArrayCodec.DATE_ARRAY_TYPES)); - } else { - defaultArrayCodecs.add(new ArrayCodec(byteBufAllocator, delegate, componentType)); - } - } + Codec codec = this.codecLookup.findDecodeCodec(dataType, format, Object.class); + if (codec instanceof CodecMetadata) { + return ((CodecMetadata) codec).type(); } - codecs.addAll(defaultArrayCodecs); + return null; + } - return codecs; + @Override + public Iterator> iterator() { + return Collections.unmodifiableList(new ArrayList<>(this.codecs)).iterator(); } }