Skip to content

Commit 0e9402e

Browse files
committed
LineCodec
1 parent 6c07d56 commit 0e9402e

File tree

5 files changed

+210
-1
lines changed

5 files changed

+210
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ public DefaultCodecs(ByteBufAllocator byteBufAllocator) {
9898
//Geometry
9999
new CircleCodec(byteBufAllocator),
100100
new PointCodec(byteBufAllocator),
101-
new BoxCodec(byteBufAllocator)
101+
new BoxCodec(byteBufAllocator),
102+
new LineCodec(byteBufAllocator)
102103
));
103104
}
104105

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package io.r2dbc.postgresql.codec;
2+
3+
import java.util.Objects;
4+
5+
/**
6+
* Value object that maps to the {@code line} datatype in Postgres.
7+
* <p>
8+
* Uses {@code double} to represent the coordinates.
9+
*/
10+
public final class Line {
11+
12+
private final double a;
13+
private final double b;
14+
private final double c;
15+
16+
private Line(double a, double b, double c) {
17+
this.a = a;
18+
this.b = b;
19+
this.c = c;
20+
}
21+
22+
public static Line of(double a, double b, double c) {
23+
return new Line(a, b, c);
24+
}
25+
26+
public double getA() {
27+
return this.a;
28+
}
29+
30+
public double getB() {
31+
return this.b;
32+
}
33+
34+
public double getC() {
35+
return this.c;
36+
}
37+
38+
@Override
39+
public boolean equals(Object o) {
40+
if (this == o) {
41+
return true;
42+
}
43+
if (o == null || getClass() != o.getClass()) {
44+
return false;
45+
}
46+
Line line = (Line) o;
47+
return Double.compare(line.a, this.a) == 0 &&
48+
Double.compare(line.b, this.b) == 0 &&
49+
Double.compare(line.c, this.c) == 0;
50+
}
51+
52+
@Override
53+
public int hashCode() {
54+
return Objects.hash(this.a, this.b, this.c);
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return "{" + this.a + ","+ this.b + "," + this.c + '}';
60+
}
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.r2dbc.postgresql.codec;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.buffer.ByteBufAllocator;
5+
import io.r2dbc.postgresql.type.PostgresqlObjectId;
6+
7+
import java.util.List;
8+
9+
final class LineCodec extends AbstractGeometryCodec<Line> {
10+
11+
LineCodec(ByteBufAllocator byteBufAllocator) {
12+
super(Line.class, PostgresqlObjectId.LINE, byteBufAllocator);
13+
}
14+
15+
@Override
16+
Line doDecodeBinary(ByteBuf byteBuffer) {
17+
double a = byteBuffer.readDouble();
18+
double b = byteBuffer.readDouble();
19+
double c = byteBuffer.readDouble();
20+
return Line.of(a, b, c);
21+
}
22+
23+
@Override
24+
Line doDecodeText(String text) {
25+
List<String> tokens = tokenizeTextData(text);
26+
double a = Double.parseDouble(tokens.get(0));
27+
double b = Double.parseDouble(tokens.get(1));
28+
double c = Double.parseDouble(tokens.get(2));
29+
return Line.of(a, b, c);
30+
}
31+
32+
@Override
33+
ByteBuf doEncodeBinary(Line value) {
34+
return this.byteBufAllocator
35+
.buffer(24)
36+
.writeDouble(value.getA())
37+
.writeDouble(value.getB())
38+
.writeDouble(value.getC());
39+
}
40+
41+
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.r2dbc.postgresql.codec.Circle;
2626
import io.r2dbc.postgresql.codec.EnumCodec;
2727
import io.r2dbc.postgresql.codec.Json;
28+
import io.r2dbc.postgresql.codec.Line;
2829
import io.r2dbc.postgresql.codec.Point;
2930
import io.r2dbc.spi.Blob;
3031
import io.r2dbc.spi.Clob;
@@ -468,6 +469,12 @@ void box() {
468469
testCodec(Box.class, Box.of(Point.of(1.5, 3.3), Point.of(5., 7.)), "BOX");
469470
}
470471

472+
@Test
473+
void line() {
474+
testCodec(Line.class, Line.of(1., 2., 4.), "LINE");
475+
testCodec(Line.class, Line.of(-10.42, 3.14, 5.24), "LINE");
476+
}
477+
471478
private static <T> Mono<T> close(Connection connection) {
472479
return Mono.from(connection
473480
.close())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package io.r2dbc.postgresql.codec;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.r2dbc.postgresql.client.Parameter;
5+
import io.r2dbc.postgresql.client.ParameterAssert;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static io.r2dbc.postgresql.client.Parameter.NULL_VALUE;
9+
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
10+
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
11+
import static io.r2dbc.postgresql.type.PostgresqlObjectId.LINE;
12+
import static io.r2dbc.postgresql.type.PostgresqlObjectId.POINT;
13+
import static io.r2dbc.postgresql.type.PostgresqlObjectId.VARCHAR;
14+
import static io.r2dbc.postgresql.util.ByteBufUtils.encode;
15+
import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST;
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
18+
19+
/**
20+
* Unit tests for {@link LineCodec}.
21+
*/
22+
public class LineCodecUnitTest {
23+
24+
private static final int dataType = LINE.getObjectId();
25+
26+
@Test
27+
void constructorNoByteBufAllocator() {
28+
assertThatIllegalArgumentException().isThrownBy(() -> new LineCodec(null))
29+
.withMessage("byteBufAllocator must not be null");
30+
}
31+
32+
@Test
33+
void decode() {
34+
Line line = Line.of(-10.42, 3.14, 5.24);
35+
36+
ByteBuf lineTextFormat = encode(TEST, "{-10.42,3.14,5.24}");
37+
assertThat(new LineCodec(TEST).decode(lineTextFormat, dataType, FORMAT_TEXT, Line.class))
38+
.isEqualTo(line);
39+
40+
ByteBuf lineByteFormat = TEST.buffer(24)
41+
.writeDouble(-10.42).writeDouble(3.14).writeDouble(5.24);
42+
assertThat(new LineCodec(TEST).decode(lineByteFormat, dataType, FORMAT_BINARY, Line.class))
43+
.isEqualTo(line);
44+
}
45+
46+
@Test
47+
void decodeNoByteBuf() {
48+
assertThat(new LineCodec(TEST).decode(null, dataType, FORMAT_TEXT, Line.class)).isNull();
49+
}
50+
51+
@Test
52+
void doCanDecode() {
53+
LineCodec codec = new LineCodec(TEST);
54+
55+
assertThat(codec.doCanDecode(LINE, FORMAT_BINARY)).isTrue();
56+
assertThat(codec.doCanDecode(VARCHAR, FORMAT_TEXT)).isFalse();
57+
assertThat(codec.doCanDecode(POINT, FORMAT_TEXT)).isFalse();
58+
}
59+
60+
@Test
61+
void doCanDecodeNoFormat() {
62+
assertThatIllegalArgumentException().isThrownBy(() -> new LineCodec(TEST).doCanDecode(LINE, null))
63+
.withMessage("format must not be null");
64+
}
65+
66+
@Test
67+
void doCanDecodeNoType() {
68+
assertThatIllegalArgumentException().isThrownBy(() -> new LineCodec(TEST).doCanDecode(null, FORMAT_TEXT))
69+
.withMessage("type must not be null");
70+
}
71+
72+
@Test
73+
void doEncode() {
74+
Line line = Line.of(-10.42, 3.14, 5.24);
75+
76+
ParameterAssert.assertThat(new LineCodec(TEST).doEncode(line))
77+
.hasFormat(FORMAT_BINARY)
78+
.hasType(dataType)
79+
.hasValue(TEST.buffer(24)
80+
.writeDouble(-10.42)
81+
.writeDouble(3.14)
82+
.writeDouble(5.24)
83+
);
84+
}
85+
86+
@Test
87+
void doEncodeNoValue() {
88+
assertThatIllegalArgumentException().isThrownBy(() -> new LineCodec(TEST).doEncode(null))
89+
.withMessage("value must not be null");
90+
}
91+
92+
@Test
93+
void encodeNull() {
94+
ParameterAssert.assertThat(new LineCodec(TEST).encodeNull())
95+
.isEqualTo(new Parameter(FORMAT_BINARY, dataType, NULL_VALUE));
96+
}
97+
98+
}

0 commit comments

Comments
 (0)