Skip to content

Commit 6af1108

Browse files
committed
LsegCodec
1 parent 0e9402e commit 6af1108

File tree

6 files changed

+205
-2
lines changed

6 files changed

+205
-2
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ public DefaultCodecs(ByteBufAllocator byteBufAllocator) {
9999
new CircleCodec(byteBufAllocator),
100100
new PointCodec(byteBufAllocator),
101101
new BoxCodec(byteBufAllocator),
102-
new LineCodec(byteBufAllocator)
102+
new LineCodec(byteBufAllocator),
103+
new LsegCodec(byteBufAllocator)
103104
));
104105
}
105106

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.r2dbc.postgresql.codec;
2+
3+
import io.r2dbc.postgresql.util.Assert;
4+
5+
import java.util.Objects;
6+
7+
/**
8+
* Value object that maps to the {@code lseg} datatype in Postgres.
9+
*/
10+
public final class Lseg {
11+
12+
private final Point p1;
13+
14+
private final Point p2;
15+
16+
private Lseg(Point p1, Point p2) {
17+
this.p1 = Assert.requireNonNull(p1, "p1 must not be null");
18+
this.p2 = Assert.requireNonNull(p2, "p2 must not be null");
19+
}
20+
21+
public static Lseg of(Point p1, Point p2) {
22+
return new Lseg(p1, p2);
23+
}
24+
25+
public Point getP1() {
26+
return this.p1;
27+
}
28+
29+
public Point getP2() {
30+
return this.p2;
31+
}
32+
33+
@Override
34+
public boolean equals(Object o) {
35+
if (this == o) {
36+
return true;
37+
}
38+
if (o == null || getClass() != o.getClass()) {
39+
return false;
40+
}
41+
Lseg lseg = (Lseg) o;
42+
return this.p1.equals(lseg.p1) &&
43+
this.p2.equals(lseg.p2);
44+
}
45+
46+
@Override
47+
public int hashCode() {
48+
return Objects.hash(this.p1, this.p2);
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return "[" + this.p1 + "," + this.p2 + ']';
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 LsegCodec extends AbstractGeometryCodec<Lseg> {
10+
11+
LsegCodec(ByteBufAllocator byteBufAllocator) {
12+
super(Lseg.class, PostgresqlObjectId.LSEG, byteBufAllocator);
13+
}
14+
15+
@Override
16+
Lseg doDecodeBinary(ByteBuf byteBuffer) {
17+
return Lseg.of(
18+
Point.of(byteBuffer.readDouble(), byteBuffer.readDouble()),
19+
Point.of(byteBuffer.readDouble(), byteBuffer.readDouble())
20+
);
21+
}
22+
23+
@Override
24+
Lseg doDecodeText(String text) {
25+
List<String> tokens = tokenizeTextData(text);
26+
return Lseg.of(
27+
Point.of(Double.parseDouble(tokens.get(0)), Double.parseDouble(tokens.get(1))),
28+
Point.of(Double.parseDouble(tokens.get(2)), Double.parseDouble(tokens.get(3)))
29+
);
30+
}
31+
32+
@Override
33+
ByteBuf doEncodeBinary(Lseg value) {
34+
return this.byteBufAllocator
35+
.buffer(32)
36+
.writeDouble(value.getP1().getX())
37+
.writeDouble(value.getP1().getY())
38+
.writeDouble(value.getP2().getX())
39+
.writeDouble(value.getP2().getY());
40+
}
41+
42+
}

src/main/java/io/r2dbc/postgresql/type/PostgresqlObjectId.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public enum PostgresqlObjectId {
190190
/**
191191
* The line segment object id
192192
*/
193-
LINE_SEGMENT(601),
193+
LSEG(601),
194194

195195
/**
196196
* The money object id.

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

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.r2dbc.postgresql.codec.EnumCodec;
2727
import io.r2dbc.postgresql.codec.Json;
2828
import io.r2dbc.postgresql.codec.Line;
29+
import io.r2dbc.postgresql.codec.Lseg;
2930
import io.r2dbc.postgresql.codec.Point;
3031
import io.r2dbc.spi.Blob;
3132
import io.r2dbc.spi.Clob;
@@ -475,6 +476,11 @@ void line() {
475476
testCodec(Line.class, Line.of(-10.42, 3.14, 5.24), "LINE");
476477
}
477478

479+
@Test
480+
void lseg() {
481+
testCodec(Lseg.class, Lseg.of(Point.of(1.11, 2.22), Point.of(3.33, 4.44)), "LSEG");
482+
}
483+
478484
private static <T> Mono<T> close(Connection connection) {
479485
return Mono.from(connection
480486
.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.BOX;
12+
import static io.r2dbc.postgresql.type.PostgresqlObjectId.LSEG;
13+
import static io.r2dbc.postgresql.type.PostgresqlObjectId.POINT;
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 LsegCodec}.
21+
*/
22+
public class LsegCodecUnitTest {
23+
24+
private static final int dataType = LSEG.getObjectId();
25+
26+
@Test
27+
void constructorNoByteBufAllocator() {
28+
assertThatIllegalArgumentException().isThrownBy(() -> new LsegCodec(null))
29+
.withMessage("byteBufAllocator must not be null");
30+
}
31+
32+
@Test
33+
void decode() {
34+
Lseg lseg = Lseg.of(Point.of(1.11, 2.22), Point.of(3.33, 4.44));
35+
36+
ByteBuf boxTextFormat = encode(TEST, "[(1.11,2.22),(3.33,4.44)]");
37+
assertThat(new LsegCodec(TEST).decode(boxTextFormat, dataType, FORMAT_TEXT, Lseg.class))
38+
.isEqualTo(lseg);
39+
40+
ByteBuf boxByteFormat = TEST.buffer(32)
41+
.writeDouble(1.11).writeDouble(2.22)
42+
.writeDouble(3.33).writeDouble(4.44);
43+
assertThat(new LsegCodec(TEST).decode(boxByteFormat, dataType, FORMAT_BINARY, Lseg.class))
44+
.isEqualTo(lseg);
45+
}
46+
47+
@Test
48+
void decodeNoByteBuf() {
49+
assertThat(new LsegCodec(TEST).decode(null, dataType, FORMAT_TEXT, Lseg.class)).isNull();
50+
}
51+
52+
@Test
53+
void doCanDecode() {
54+
LsegCodec codec = new LsegCodec(TEST);
55+
56+
assertThat(codec.doCanDecode(LSEG, FORMAT_BINARY)).isTrue();
57+
assertThat(codec.doCanDecode(BOX, FORMAT_TEXT)).isFalse();
58+
assertThat(codec.doCanDecode(POINT, FORMAT_TEXT)).isFalse();
59+
}
60+
61+
@Test
62+
void doCanDecodeNoFormat() {
63+
assertThatIllegalArgumentException().isThrownBy(() -> new LsegCodec(TEST).doCanDecode(LSEG, null))
64+
.withMessage("format must not be null");
65+
}
66+
67+
@Test
68+
void doCanDecodeNoType() {
69+
assertThatIllegalArgumentException().isThrownBy(() -> new LsegCodec(TEST).doCanDecode(null, FORMAT_TEXT))
70+
.withMessage("type must not be null");
71+
}
72+
73+
@Test
74+
void doEncode() {
75+
Lseg lseg = Lseg.of(Point.of(1.11, 2.22), Point.of(3.33, 4.44));
76+
77+
ParameterAssert.assertThat(new LsegCodec(TEST).doEncode(lseg))
78+
.hasFormat(FORMAT_BINARY)
79+
.hasType(dataType)
80+
.hasValue(TEST.buffer(32)
81+
.writeDouble(1.11).writeDouble(2.22)
82+
.writeDouble(3.33).writeDouble(4.44)
83+
);
84+
}
85+
86+
@Test
87+
void doEncodeNoValue() {
88+
assertThatIllegalArgumentException().isThrownBy(() -> new LsegCodec(TEST).doEncode(null))
89+
.withMessage("value must not be null");
90+
}
91+
92+
@Test
93+
void encodeNull() {
94+
ParameterAssert.assertThat(new LsegCodec(TEST).encodeNull())
95+
.isEqualTo(new Parameter(FORMAT_BINARY, dataType, NULL_VALUE));
96+
}
97+
98+
}

0 commit comments

Comments
 (0)