Skip to content

Commit 945ba2f

Browse files
committed
review comments addressed
1 parent 1a46126 commit 945ba2f

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ public DefaultCodecs(ByteBufAllocator byteBufAllocator) {
9292
new ShortArrayCodec(byteBufAllocator),
9393
new StringArrayCodec(byteBufAllocator),
9494
new IntegerArrayCodec(byteBufAllocator),
95-
new LongArrayCodec(byteBufAllocator)
95+
new LongArrayCodec(byteBufAllocator),
96+
97+
new PointCodec(byteBufAllocator)
9698
));
9799
}
98100

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* <p>This implements a version of java.awt.Point, except it uses double to represent the coordinates.</p>
77
*/
88
public class Point {
9-
public double x;
10-
public double y;
9+
private double x;
10+
private double y;
1111

1212
/**
1313
* @param x coordinate
@@ -18,6 +18,22 @@ public Point(double x, double y) {
1818
this.y = y;
1919
}
2020

21+
public double getX() {
22+
return x;
23+
}
24+
25+
public void setX(double x) {
26+
this.x = x;
27+
}
28+
29+
public double getY() {
30+
return y;
31+
}
32+
33+
public void setY(double y) {
34+
this.y = y;
35+
}
36+
2137
/**
2238
* Translate the point by the supplied amount.
2339
*

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

+8-12
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ boolean doCanDecode(PostgresqlObjectId type, Format format) {
2525
Assert.requireNonNull(type, "type must not be null");
2626
Assert.requireNonNull(format, "format must not be null");
2727

28-
return POINT == type && format == FORMAT_BINARY;
28+
return POINT == type;
2929
}
3030

3131
@Override
@@ -34,16 +34,12 @@ Point doDecode(ByteBuf buffer, PostgresqlObjectId dataType, Format format, Class
3434
Assert.requireNonNull(type, "type must not be null");
3535
Assert.requireNonNull(format, "format must not be null");
3636

37-
try {
38-
String decodedAsString = ByteBufUtils.decode(buffer);
39-
String parenRemovedVal = decodedAsString.replaceAll("[()]", "");
40-
String[] coordinatesAsString = parenRemovedVal.split(",");
41-
double x = Double.parseDouble(coordinatesAsString[0]);
42-
double y = Double.parseDouble(coordinatesAsString[1]);
43-
return new Point(x, y);
44-
} catch (NumberFormatException | NullPointerException | ArrayIndexOutOfBoundsException e) {
45-
throw new IllegalArgumentException(e);
46-
}
37+
String decodedAsString = ByteBufUtils.decode(buffer);
38+
String parenRemovedVal = decodedAsString.replaceAll("[()]", "");
39+
String[] coordinatesAsString = parenRemovedVal.split(",");
40+
double x = Double.parseDouble(coordinatesAsString[0]);
41+
double y = Double.parseDouble(coordinatesAsString[1]);
42+
return new Point(x, y);
4743
}
4844

4945
/**
@@ -53,7 +49,7 @@ Point doDecode(ByteBuf buffer, PostgresqlObjectId dataType, Format format, Class
5349
@Override
5450
Parameter doEncode(Point value) {
5551
Assert.requireNonNull(value, "value must not be null");
56-
String encodedValue = "(" + value.x + "," + value.y + ")";
52+
String encodedValue = "(" + value.getX() + "," + value.getY() + ")";
5753
return create(POINT, FORMAT_BINARY, () -> ByteBufUtils.encode(this.byteBufAllocator, encodedValue));
5854
}
5955

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

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.r2dbc.postgresql.api.PostgresqlStatement;
2424
import io.r2dbc.postgresql.codec.EnumCodec;
2525
import io.r2dbc.postgresql.codec.Json;
26+
import io.r2dbc.postgresql.codec.Point;
2627
import io.r2dbc.spi.Blob;
2728
import io.r2dbc.spi.Clob;
2829
import io.r2dbc.spi.Connection;
@@ -360,6 +361,11 @@ void offsetDateTime() {
360361
testCodec(OffsetDateTime.class, OffsetDateTime.now(), (actual, expected) -> assertThat(actual.isEqual(expected)).isTrue(), "TIMESTAMP WITH TIME ZONE");
361362
}
362363

364+
@Test
365+
void point() {
366+
testCodec(Point.class, new Point(1.12, 2.12), "POINT");
367+
}
368+
363369
@Test
364370
void shortArray() {
365371
testCodec(Short[].class, new Short[]{100, 200, 300}, "INT2[]");

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void doCanDecode() {
4040
PointCodec codec = new PointCodec(TEST);
4141

4242
assertThat(codec.doCanDecode(VARCHAR, FORMAT_BINARY)).isFalse();
43-
assertThat(codec.doCanDecode(POINT, FORMAT_TEXT)).isFalse();
43+
assertThat(codec.doCanDecode(POINT, FORMAT_TEXT)).isTrue();
4444
assertThat(codec.doCanDecode(POINT, FORMAT_BINARY)).isTrue();
4545
}
4646

0 commit comments

Comments
 (0)