|
18 | 18 |
|
19 | 19 | import io.netty.buffer.ByteBuf;
|
20 | 20 | 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 | 21 |
|
27 |
| -import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY; |
28 |
| -import static io.r2dbc.postgresql.type.PostgresqlObjectId.CIRCLE; |
| 22 | +import java.util.List; |
29 | 23 |
|
30 |
| -final class CircleCodec extends AbstractCodec<Circle> { |
| 24 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.CIRCLE; |
31 | 25 |
|
32 |
| - private final ByteBufAllocator byteBufAllocator; |
| 26 | +final class CircleCodec extends AbstractGeometryCodec<Circle> { |
33 | 27 |
|
34 | 28 | CircleCodec(ByteBufAllocator byteBufAllocator) {
|
35 |
| - super(Circle.class); |
36 |
| - this.byteBufAllocator = Assert.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null"); |
| 29 | + super(Circle.class, CIRCLE, byteBufAllocator); |
37 | 30 | }
|
38 | 31 |
|
39 | 32 | @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); |
44 | 38 | }
|
45 | 39 |
|
46 | 40 | @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)); |
65 | 46 | return new Circle(Point.of(x, y), r);
|
66 | 47 | }
|
67 | 48 |
|
68 |
| - /** |
69 |
| - * @param value the {@code value}. |
70 |
| - * @return Circle in string format as understood by Postgresql - <(x,y),r> |
71 |
| - */ |
72 | 49 | @Override
|
73 |
| - Parameter doEncode(Circle value) { |
74 |
| - Assert.requireNonNull(value, "value must not be null"); |
| 50 | + ByteBuf doEncodeBinary(Circle value) { |
75 | 51 | Point center = value.getCenter();
|
76 |
| - return create(CIRCLE, FORMAT_BINARY, () -> this.byteBufAllocator.buffer(lengthInBytes()) |
| 52 | + return this.byteBufAllocator.buffer(lengthInBytes()) |
77 | 53 | .writeDouble(center.getX())
|
78 | 54 | .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()); |
85 | 56 | }
|
86 | 57 |
|
87 | 58 | int lengthInBytes() {
|
|
0 commit comments