Skip to content

Commit 6e2c374

Browse files
committed
refactor
1 parent 6d5c22d commit 6e2c374

13 files changed

+55
-100
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ This reference table shows the type mapping between [PostgreSQL][p] and Java dat
343343
| [`bit`][psql-bit-ref] | Not yet supported.|
344344
| [`bit varying`][psql-bit-ref] | Not yet supported.|
345345
| [`boolean or bool`][psql-boolean-ref] | [`Boolean`][java-boolean-ref]|
346-
| [`box`][psql-box-ref] | Not yet supported.|
346+
| [`box`][psql-box-ref] | **`Box`**|
347347
| [`bytea`][psql-bytea-ref] | [**`ByteBuffer`**][java-ByteBuffer-ref], [`byte[]`][java-byte-ref], [`Blob`][r2dbc-blob-ref]|
348348
| [`character`][psql-character-ref] | [`String`][java-string-ref]|
349349
| [`character varying`][psql-character-ref] | [`String`][java-string-ref]|
@@ -358,17 +358,17 @@ This reference table shows the type mapping between [PostgreSQL][p] and Java dat
358358
| [`interval`][psql-interval-ref] | Not yet supported.|
359359
| [`json`][psql-json-ref] | **`Json`**, [`String`][java-string-ref]. Reading: `ByteBuf`[`byte[]`][java-primitive-ref], [`ByteBuffer`][java-ByteBuffer-ref], [`String`][java-string-ref], [`InputStream`][java-inputstream-ref]|
360360
| [`jsonb`][psql-json-ref] | **`Json`**, [`String`][java-string-ref]. Reading: `ByteBuf`[`byte[]`][java-primitive-ref], [`ByteBuffer`][java-ByteBuffer-ref], [`String`][java-string-ref], [`InputStream`][java-inputstream-ref]|
361-
| [`line`][psql-line-ref] | Not yet supported.|
362-
| [`lseg`][psql-lseq-ref] | Not yet supported.|
361+
| [`line`][psql-line-ref] | **`Line`**|
362+
| [`lseg`][psql-lseq-ref] | **`Lseg`**|
363363
| [`macaddr`][psql-macaddr-ref] | Not yet supported.|
364364
| [`macaddr8`][psql-macaddr8-ref] | Not yet supported.|
365365
| [`money`][psql-money-ref] | Not yet supported.|
366366
| [`numeric`][psql-bignumeric-ref] | [`BigDecimal`][java-bigdecimal-ref], [`Boolean`][java-boolean-ref], [`Byte`][java-byte-ref], [`Short`][java-short-ref], [`Integer`][java-integer-ref], [`Long`][java-long-ref], [`BigInteger`][java-biginteger-ref]|
367367
| [`oid`][psql-oid-ref] | [**`Integer`**][java-integer-ref], [`Boolean`][java-boolean-ref], [`Byte`][java-byte-ref], [`Short`][java-short-ref], [`Long`][java-long-ref], [`BigDecimal`][java-bigdecimal-ref], [`BigInteger`][java-biginteger-ref]|
368-
| [`path`][psql-path-ref] | Not yet supported.|
368+
| [`path`][psql-path-ref] | **`Path`**|
369369
| [`pg_lsn`][psql-pg_lsn-ref] | Not yet supported.|
370370
| [`point`][psql-point-ref] | **`Point`**|
371-
| [`polygon`][psql-polygon-ref] | Not yet supported.|
371+
| [`polygon`][psql-polygon-ref] | **`Polygon`**|
372372
| [`real`][psql-real-ref] | [**`Float`**][java-float-ref], [`Double`][java-double-ref], [`Boolean`][java-boolean-ref], [`Byte`][java-byte-ref], [`Short`][java-short-ref], [`Integer`][java-integer-ref], [`Long`][java-long-ref], [`BigDecimal`][java-bigdecimal-ref], [`BigInteger`][java-biginteger-ref]|
373373
| [`smallint`][psql-smallint-ref] | [**`Short`**][java-short-ref], [`Boolean`][java-boolean-ref], [`Byte`][java-byte-ref], [`Integer`][java-integer-ref], [`Long`][java-long-ref], [`BigDecimal`][java-bigdecimal-ref], [`BigInteger`][java-biginteger-ref]|
374374
| [`smallserial`][psql-smallserial-ref] | [**`Integer`**][java-integer-ref], [`Boolean`][java-boolean-ref], [`Byte`][java-byte-ref], [`Short`][java-short-ref], [`Long`][java-long-ref], [`BigDecimal`][java-bigdecimal-ref], [`BigInteger`][java-biginteger-ref]|

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ boolean doCanDecode(PostgresqlObjectId type, Format format) {
3838

3939
@Override
4040
T doDecode(ByteBuf buffer, PostgresqlObjectId dataType, Format format, Class<? extends T> type) {
41+
Assert.requireNonNull(buffer, "byteBuf must not be null");
42+
Assert.requireNonNull(type, "type must not be null");
43+
Assert.requireNonNull(format, "format must not be null");
4144
if (format == Format.FORMAT_BINARY) {
4245
return doDecodeBinary(buffer);
4346
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
/**
88
* Value object that maps to the {@code box} datatype in Postgres.
9+
* <p>
10+
* Uses {@code double} to represent the coordinates.
911
*/
1012
public final class Box {
1113

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

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,70 +18,41 @@
1818

1919
import io.netty.buffer.ByteBuf;
2020
import io.netty.buffer.ByteBufAllocator;
21-
import io.r2dbc.postgresql.client.Parameter;
22-
import io.r2dbc.postgresql.message.Format;
23-
import io.r2dbc.postgresql.type.PostgresqlObjectId;
24-
import io.r2dbc.postgresql.util.Assert;
25-
import io.r2dbc.postgresql.util.ByteBufUtils;
2621

27-
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
28-
import static io.r2dbc.postgresql.type.PostgresqlObjectId.CIRCLE;
22+
import java.util.List;
2923

30-
final class CircleCodec extends AbstractCodec<Circle> {
24+
import static io.r2dbc.postgresql.type.PostgresqlObjectId.CIRCLE;
3125

32-
private final ByteBufAllocator byteBufAllocator;
26+
final class CircleCodec extends AbstractGeometryCodec<Circle> {
3327

3428
CircleCodec(ByteBufAllocator byteBufAllocator) {
35-
super(Circle.class);
36-
this.byteBufAllocator = Assert.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null");
29+
super(Circle.class, CIRCLE, byteBufAllocator);
3730
}
3831

3932
@Override
40-
boolean doCanDecode(PostgresqlObjectId type, Format format) {
41-
Assert.requireNonNull(type, "type must not be null");
42-
43-
return CIRCLE == type;
33+
Circle doDecodeBinary(ByteBuf byteBuffer) {
34+
double x = byteBuffer.readDouble();
35+
double y = byteBuffer.readDouble();
36+
double r = byteBuffer.readDouble();
37+
return new Circle(Point.of(x, y), r);
4438
}
4539

4640
@Override
47-
Circle doDecode(ByteBuf buffer, PostgresqlObjectId dataType, Format format, Class<? extends Circle> type) {
48-
Assert.requireNonNull(buffer, "byteBuf must not be null");
49-
Assert.requireNonNull(type, "type must not be null");
50-
Assert.requireNonNull(format, "format must not be null");
51-
52-
if (format == FORMAT_BINARY) {
53-
double x = buffer.readDouble();
54-
double y = buffer.readDouble();
55-
double r = buffer.readDouble();
56-
return new Circle(Point.of(x, y), r);
57-
}
58-
59-
String decodedAsString = ByteBufUtils.decode(buffer);
60-
String parenRemovedVal = decodedAsString.replaceAll("[()<>]", "");
61-
String[] coordinatesAsString = parenRemovedVal.split(",");
62-
double x = Double.parseDouble(coordinatesAsString[0]);
63-
double y = Double.parseDouble(coordinatesAsString[1]);
64-
double r = Double.parseDouble(coordinatesAsString[2]);
41+
Circle doDecodeText(String text) {
42+
List<String> tokens = tokenizeTextData(text);
43+
double x = Double.parseDouble(tokens.get(0));
44+
double y = Double.parseDouble(tokens.get(1));
45+
double r = Double.parseDouble(tokens.get(2));
6546
return new Circle(Point.of(x, y), r);
6647
}
6748

68-
/**
69-
* @param value the {@code value}.
70-
* @return Circle in string format as understood by Postgresql - &lt(x,y),r&gt
71-
*/
7249
@Override
73-
Parameter doEncode(Circle value) {
74-
Assert.requireNonNull(value, "value must not be null");
50+
ByteBuf doEncodeBinary(Circle value) {
7551
Point center = value.getCenter();
76-
return create(CIRCLE, FORMAT_BINARY, () -> this.byteBufAllocator.buffer(lengthInBytes())
52+
return this.byteBufAllocator.buffer(lengthInBytes())
7753
.writeDouble(center.getX())
7854
.writeDouble(center.getY())
79-
.writeDouble(value.getRadius()));
80-
}
81-
82-
@Override
83-
public Parameter encodeNull() {
84-
return createNull(CIRCLE, FORMAT_BINARY);
55+
.writeDouble(value.getRadius());
8556
}
8657

8758
int lengthInBytes() {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
public final class Line {
1111

1212
private final double a;
13+
1314
private final double b;
15+
1416
private final double c;
1517

1618
private Line(double a, double b, double c) {
@@ -56,7 +58,7 @@ public int hashCode() {
5658

5759
@Override
5860
public String toString() {
59-
return "{" + this.a + ","+ this.b + "," + this.c + '}';
61+
return "{" + this.a + "," + this.b + "," + this.c + '}';
6062
}
6163

6264
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
/**
88
* Value object that maps to the {@code lseg} datatype in Postgres.
9+
* <p>
10+
* Uses {@code double} to represent the coordinates.
911
*/
1012
public final class Lseg {
1113

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
/**
1212
* Value object that maps to the {@code path} datatype in Postgres.
13+
* <p>
14+
* Uses {@code double} to represent the coordinates.
1315
*/
1416
public class Path {
1517

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

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,65 +18,36 @@
1818

1919
import io.netty.buffer.ByteBuf;
2020
import io.netty.buffer.ByteBufAllocator;
21-
import io.r2dbc.postgresql.client.Parameter;
22-
import io.r2dbc.postgresql.message.Format;
23-
import io.r2dbc.postgresql.type.PostgresqlObjectId;
24-
import io.r2dbc.postgresql.util.Assert;
25-
import io.r2dbc.postgresql.util.ByteBufUtils;
26-
import reactor.util.annotation.Nullable;
2721

28-
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
29-
import static io.r2dbc.postgresql.type.PostgresqlObjectId.POINT;
22+
import java.util.List;
3023

31-
final class PointCodec extends AbstractCodec<Point> {
24+
import static io.r2dbc.postgresql.type.PostgresqlObjectId.POINT;
3225

33-
private final ByteBufAllocator byteBufAllocator;
26+
final class PointCodec extends AbstractGeometryCodec<Point> {
3427

3528
PointCodec(ByteBufAllocator byteBufAllocator) {
36-
super(Point.class);
37-
this.byteBufAllocator = Assert.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null");
29+
super(Point.class, POINT, byteBufAllocator);
3830
}
3931

4032
@Override
41-
boolean doCanDecode(PostgresqlObjectId type, @Nullable Format format) {
42-
Assert.requireNonNull(type, "type must not be null");
43-
44-
return POINT == type;
45-
}
46-
47-
@Override
48-
Point doDecode(ByteBuf buffer, PostgresqlObjectId dataType, Format format, Class<? extends Point> type) {
49-
Assert.requireNonNull(buffer, "byteBuf must not be null");
50-
Assert.requireNonNull(type, "type must not be null");
51-
Assert.requireNonNull(format, "format must not be null");
52-
53-
if (format == FORMAT_BINARY) {
54-
double x = buffer.readDouble();
55-
double y = buffer.readDouble();
56-
return Point.of(x, y);
57-
}
58-
59-
String decodedAsString = ByteBufUtils.decode(buffer);
60-
String parenRemovedVal = decodedAsString.replaceAll("[()]", "");
61-
String[] coordinatesAsString = parenRemovedVal.split(",");
62-
double x = Double.parseDouble(coordinatesAsString[0]);
63-
double y = Double.parseDouble(coordinatesAsString[1]);
33+
Point doDecodeBinary(ByteBuf byteBuffer) {
34+
double x = byteBuffer.readDouble();
35+
double y = byteBuffer.readDouble();
6436
return Point.of(x, y);
6537
}
6638

67-
/**
68-
* @param value the {@code value}.
69-
* @return Point in String format as understood by postgresql - (x,y)
70-
*/
7139
@Override
72-
Parameter doEncode(Point value) {
73-
Assert.requireNonNull(value, "value must not be null");
74-
return create(POINT, FORMAT_BINARY, () -> this.byteBufAllocator.buffer(lengthInBytes()).writeDouble(value.getX()).writeDouble(value.getY()));
40+
Point doDecodeText(String text) {
41+
List<String> tokens = tokenizeTextData(text);
42+
double x = Double.parseDouble(tokens.get(0));
43+
double y = Double.parseDouble(tokens.get(1));
44+
return Point.of(x, y);
7545
}
7646

7747
@Override
78-
public Parameter encodeNull() {
79-
return createNull(POINT, FORMAT_BINARY);
48+
ByteBuf doEncodeBinary(Point value) {
49+
return this.byteBufAllocator.buffer(lengthInBytes())
50+
.writeDouble(value.getX()).writeDouble(value.getY());
8051
}
8152

8253
int lengthInBytes() {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
/**
1212
* Value object that maps to the {@code polygon} datatype in Postgres.
13+
* <p>
14+
* Uses {@code double} to represent the coordinates.
1315
*/
1416
public final class Polygon {
1517

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ void path() {
494494
@Test
495495
void polygon() {
496496
testCodec(Polygon.class, Polygon.of(Point.of(1.1, 2.2), Point.of(10.10, 10.10), Point.of(.42, 5.3)), "POLYGON");
497-
testCodec(Polygon.class, Polygon.of(Point.of(1.1, 2.2), Point.of(10.10, 10.10), Point.of(.42, 5.3), Point.of(-3.5, 0.)), "POLYGON");;
497+
testCodec(Polygon.class, Polygon.of(Point.of(1.1, 2.2), Point.of(10.10, 10.10), Point.of(.42, 5.3), Point.of(-3.5, 0.)), "POLYGON");
498498
}
499499

500500
private static <T> Mono<T> close(Connection connection) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,5 @@ void encodeNull() {
9494
ParameterAssert.assertThat(new LsegCodec(TEST).encodeNull())
9595
.isEqualTo(new Parameter(FORMAT_BINARY, dataType, NULL_VALUE));
9696
}
97-
97+
9898
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,5 @@ void encodeNull() {
121121
ParameterAssert.assertThat(new PathCodec(TEST).encodeNull())
122122
.isEqualTo(new Parameter(FORMAT_BINARY, dataType, NULL_VALUE));
123123
}
124-
124+
125125
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,5 @@ void encodeNull() {
8686
ParameterAssert.assertThat(new PolygonCodec(TEST).encodeNull())
8787
.isEqualTo(new Parameter(FORMAT_BINARY, dataType, NULL_VALUE));
8888
}
89-
89+
9090
}

0 commit comments

Comments
 (0)