Skip to content

Commit

Permalink
undoing accidental reformatting of Default codecs - pgjdbcgh-591
Browse files Browse the repository at this point in the history
  • Loading branch information
steverigney committed Sep 2, 2023
1 parent 939da47 commit 4b698d9
Showing 1 changed file with 119 additions and 119 deletions.
238 changes: 119 additions & 119 deletions src/main/java/io/r2dbc/postgresql/codec/DefaultCodecs.java
Expand Up @@ -44,10 +44,10 @@
*/
public final class DefaultCodecs implements Codecs, CodecRegistry {

private final CodecLookup codecLookup;

private final List<Codec<?>> codecs;

private final CodecLookup codecLookup;

/**
* Create a new instance of {@link DefaultCodecs} preferring detached (copied buffers).
*
Expand Down Expand Up @@ -96,6 +96,103 @@ public DefaultCodecs(ByteBufAllocator byteBufAllocator, boolean preferAttachedBu
this.codecLookup.afterCodecAdded();
}

@SuppressWarnings({"unchecked", "rawtypes"})
private static List<Codec<?>> getDefaultCodecs(ByteBufAllocator byteBufAllocator, boolean preferAttachedBuffers, CodecConfiguration configuration) {

List<Codec<?>> 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<Codec<?>> 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");
Expand Down Expand Up @@ -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<Codec<?>> 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) {

Expand All @@ -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<Codec<?>> getDefaultCodecs(ByteBufAllocator byteBufAllocator, boolean preferAttachedBuffers, CodecConfiguration configuration) {

List<Codec<?>> 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<Codec<?>> 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<Codec<?>> iterator() {
return Collections.unmodifiableList(new ArrayList<>(this.codecs)).iterator();
}

}

0 comments on commit 4b698d9

Please sign in to comment.