Skip to content

Commit d0ae71e

Browse files
committed
Polishing
Reformat code. Convert tab indents to space indents. Add missing Override annotations. Fix canEncodeNull(…) for arrays as UUID[] is assignable to Object[] so now AbstractArrayCodec checks for canEncodeNull(…) comparing the component type. [#336][resolves #335]
1 parent bc19461 commit d0ae71e

File tree

7 files changed

+88
-52
lines changed

7 files changed

+88
-52
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,9 @@ Support for the following single-dimensional arrays (read and write):
365365

366366
| PostgreSQL Type | Supported Data Type |
367367
|:-----------------------------------------------|:-------------------------------------|
368-
|[`text[]`][psql-text-ref] |[`String[]`][java-string-ref] |
368+
|[`text[]`][psql-text-ref] |[`String[]`][java-string-ref] |
369369
|[`integer[] or int[]`][psql-integer-ref] |[`Integer[]`][java-integer-ref], [`Long[]`][java-long-ref], [`Short[]`][java-short-ref]|
370-
|[`uuid[]`][psql-uuid-ref] |[`UUID[]`][java-uuid-ref]|
370+
|[`uuid[]`][psql-uuid-ref] |[`UUID[]`][java-uuid-ref]|
371371

372372
[psql-bigint-ref]: https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-INT
373373
[psql-bit-ref]: https://www.postgresql.org/docs/current/datatype-numeric.html

src/main/java/io/r2dbc/postgresql/codec/AbstractArrayCodec.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ public boolean canEncode(Object value) {
7272
return isTypeAssignable(value.getClass());
7373
}
7474

75+
@Override
76+
public boolean canEncodeNull(Class<?> type) {
77+
Assert.requireNonNull(type, "type must not be null");
78+
79+
return isTypeAssignable(type);
80+
}
81+
7582
static String escapeArrayElement(String s) {
7683
StringBuilder b = new StringBuilder();
7784
b.append('"');

src/main/java/io/r2dbc/postgresql/codec/AbstractCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public boolean canEncode(Object value) {
6666
}
6767

6868
@Override
69-
public final boolean canEncodeNull(Class<?> type) {
69+
public boolean canEncodeNull(Class<?> type) {
7070
Assert.requireNonNull(type, "type must not be null");
7171

7272
return this.type.isAssignableFrom(type);
Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 the original author or authors.
2+
* Copyright 2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,43 +18,52 @@
1818

1919
import io.netty.buffer.ByteBuf;
2020
import io.netty.buffer.ByteBufAllocator;
21+
import io.r2dbc.postgresql.client.Parameter;
2122
import io.r2dbc.postgresql.message.Format;
2223
import io.r2dbc.postgresql.type.PostgresqlObjectId;
23-
import io.r2dbc.postgresql.client.Parameter;
2424
import io.r2dbc.postgresql.util.Assert;
25-
import reactor.util.annotation.Nullable;
2625

2726
import java.util.UUID;
2827
import java.util.function.Supplier;
2928

3029
final class UuidArrayCodec extends AbstractArrayCodec<UUID> {
31-
UuidArrayCodec(ByteBufAllocator byteBufAllocator) {
32-
super(byteBufAllocator, UUID.class);
33-
}
34-
35-
public Parameter encodeNull() {
36-
return createNull(PostgresqlObjectId.UUID_ARRAY, Format.FORMAT_TEXT);
37-
}
38-
39-
UUID decodeItem(ByteBuf byteBuf) {
40-
return new UUID(byteBuf.readLong(), byteBuf.readLong());
41-
}
42-
43-
UUID decodeItem(String str) {
44-
return UUID.fromString(str);
45-
}
46-
47-
boolean doCanDecode(PostgresqlObjectId type, @Nullable Format format) {
48-
Assert.requireNonNull(type, "type must not be null");
49-
return PostgresqlObjectId.UUID_ARRAY==type;
50-
}
51-
52-
Parameter encodeArray(Supplier<ByteBuf> encodedSupplier) {
53-
return create(PostgresqlObjectId.UUID_ARRAY, Format.FORMAT_TEXT, encodedSupplier);
54-
}
55-
56-
String encodeItem(UUID value) {
57-
Assert.requireNonNull(value, "value must not be null");
58-
return value.toString();
59-
}
30+
31+
UuidArrayCodec(ByteBufAllocator byteBufAllocator) {
32+
super(byteBufAllocator, UUID.class);
33+
}
34+
35+
@Override
36+
public Parameter encodeNull() {
37+
return createNull(PostgresqlObjectId.UUID_ARRAY, Format.FORMAT_TEXT);
38+
}
39+
40+
@Override
41+
UUID decodeItem(ByteBuf byteBuf) {
42+
return new UUID(byteBuf.readLong(), byteBuf.readLong());
43+
}
44+
45+
@Override
46+
UUID decodeItem(String str) {
47+
return UUID.fromString(str);
48+
}
49+
50+
@Override
51+
boolean doCanDecode(PostgresqlObjectId type, Format format) {
52+
Assert.requireNonNull(type, "type must not be null");
53+
54+
return PostgresqlObjectId.UUID_ARRAY == type;
55+
}
56+
57+
@Override
58+
Parameter encodeArray(Supplier<ByteBuf> encodedSupplier) {
59+
return create(PostgresqlObjectId.UUID_ARRAY, Format.FORMAT_TEXT, encodedSupplier);
60+
}
61+
62+
@Override
63+
String encodeItem(UUID value) {
64+
Assert.requireNonNull(value, "value must not be null");
65+
66+
return value.toString();
67+
}
68+
6069
}

src/test/java/io/r2dbc/postgresql/AbstractCodecIntegrationTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,11 @@ void uuid() {
446446
testCodec(UUID.class, UUID.randomUUID(), "UUID");
447447
}
448448

449+
@Test
450+
void uuidArray() {
451+
testCodec(UUID[].class, new UUID[]{UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID()}, "UUID[]");
452+
}
453+
449454
@Test
450455
void zoneId() {
451456
testCodec(ZoneId.class, ZoneId.systemDefault(), "BPCHAR(32)");

src/test/java/io/r2dbc/postgresql/codec/ShortArrayCodecUnitTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import io.r2dbc.postgresql.client.Parameter;
2121
import org.junit.jupiter.api.Test;
2222

23+
import java.util.UUID;
24+
2325
import static io.r2dbc.postgresql.client.Parameter.NULL_VALUE;
2426
import static io.r2dbc.postgresql.client.ParameterAssert.assertThat;
2527
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
@@ -70,6 +72,18 @@ void doCanDecode() {
7072
assertThat(new ShortArrayCodec(TEST).doCanDecode(INT2_ARRAY, FORMAT_BINARY)).isTrue();
7173
}
7274

75+
@Test
76+
void canEncode() {
77+
assertThat(new ShortArrayCodec(TEST).canEncode(new Short[0])).isTrue();
78+
assertThat(new ShortArrayCodec(TEST).canEncode(new UUID[0])).isFalse();
79+
}
80+
81+
@Test
82+
void canEncodeNull() {
83+
assertThat(new ShortArrayCodec(TEST).canEncodeNull(Short[].class)).isTrue();
84+
assertThat(new ShortArrayCodec(TEST).canEncodeNull(UUID[].class)).isFalse();
85+
}
86+
7387
@Test
7488
void doCanDecodeNoType() {
7589
assertThatIllegalArgumentException().isThrownBy(() -> new ShortArrayCodec(TEST).doCanDecode(null, null))

src/test/java/io/r2dbc/postgresql/codec/UuidArrayCodecUnitTests.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 the original author or authors.
2+
* Copyright 2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,8 @@
2121
import io.r2dbc.postgresql.type.PostgresqlObjectId;
2222
import org.junit.jupiter.api.Test;
2323

24+
import java.util.UUID;
25+
2426
import static io.r2dbc.postgresql.client.Parameter.NULL_VALUE;
2527
import static io.r2dbc.postgresql.client.ParameterAssert.assertThat;
2628
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
@@ -31,8 +33,6 @@
3133
import static org.assertj.core.api.Assertions.assertThat;
3234
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3335

34-
import java.util.UUID;
35-
3636
/**
3737
* Unit tests for {@link UuidArrayCodec}.
3838
*/
@@ -41,23 +41,24 @@ final class UuidArrayCodecUnitTests {
4141
private static final int dataType = UUID_ARRAY.getObjectId();
4242

4343
private static final UUID u1 = UUID.randomUUID();
44+
4445
private static final UUID u2 = UUID.randomUUID();
45-
private static final String parms = "{"+u1+","+u2+"}";
4646

47-
private final ByteBuf BINARY_ARRAY = TEST
48-
.buffer()
49-
.writeInt(1)
50-
.writeInt(0)
51-
.writeInt(2951)
52-
.writeInt(2)
53-
.writeInt(2)
54-
.writeInt(16)
55-
.writeLong(u1.getMostSignificantBits())
56-
.writeLong(u1.getLeastSignificantBits())
57-
.writeInt(16)
58-
.writeLong(u2.getMostSignificantBits())
59-
.writeLong(u2.getLeastSignificantBits());
47+
private static final String parms = "{" + u1 + "," + u2 + "}";
6048

49+
private final ByteBuf BINARY_ARRAY = TEST
50+
.buffer()
51+
.writeInt(1)
52+
.writeInt(0)
53+
.writeInt(2951)
54+
.writeInt(2)
55+
.writeInt(2)
56+
.writeInt(16)
57+
.writeLong(u1.getMostSignificantBits())
58+
.writeLong(u1.getLeastSignificantBits())
59+
.writeInt(16)
60+
.writeLong(u2.getMostSignificantBits())
61+
.writeLong(u2.getLeastSignificantBits());
6162

6263
@Test
6364
void decodeItem() {

0 commit comments

Comments
 (0)