Skip to content

Commit 786da55

Browse files
committed
review comments addressed
1 parent 945ba2f commit 786da55

File tree

3 files changed

+34
-56
lines changed

3 files changed

+34
-56
lines changed

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

+20-35
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
/**
44
* <p>It maps to the point datatype in org.postgresql.</p>
55
*
6-
* <p>This implements a version of java.awt.Point, except it uses double to represent the coordinates.</p>
6+
* <p> It uses double to represent the coordinates.</p>
77
*/
88
public class Point {
9-
private double x;
10-
private double y;
9+
10+
private final double x;
11+
12+
private final double y;
1113

1214
/**
1315
* @param x coordinate
@@ -22,37 +24,30 @@ public double getX() {
2224
return x;
2325
}
2426

25-
public void setX(double x) {
26-
this.x = x;
27-
}
28-
2927
public double getY() {
3028
return y;
3129
}
3230

33-
public void setY(double y) {
34-
this.y = y;
35-
}
36-
3731
/**
3832
* Translate the point by the supplied amount.
3933
*
4034
* @param x integer amount to add on the x axis
4135
* @param y integer amount to add on the y axis
36+
* @return - new point with translated values
4237
*/
43-
public void translate(int x, int y) {
44-
translate((double) x, (double) y);
38+
public Point translate(int x, int y) {
39+
return translate((double) x, (double) y);
4540
}
4641

4742
/**
4843
* Translate the point by the supplied amount.
4944
*
5045
* @param x double amount to add on the x axis
5146
* @param y double amount to add on the y axis
47+
* @return - new point with translated values
5248
*/
53-
public void translate(double x, double y) {
54-
this.x += x;
55-
this.y += y;
49+
public Point translate(double x, double y) {
50+
return new Point(this.x + x, this.y + y);
5651
}
5752

5853
/**
@@ -61,40 +56,30 @@ public void translate(double x, double y) {
6156
* @param x integer coordinate
6257
* @param y integer coordinate
6358
*/
64-
public void move(int x, int y) {
65-
setLocation(x, y);
59+
public Point move(int x, int y) {
60+
return setLocation(x, y);
6661
}
6762

6863
/**
6964
* Moves the point to the supplied coordinates.
7065
*
7166
* @param x double coordinate
7267
* @param y double coordinate
68+
* @return - new point with provided coordinates
7369
*/
74-
public void move(double x, double y) {
75-
this.x = x;
76-
this.y = y;
70+
public Point move(double x, double y) {
71+
return new Point(x, y);
7772
}
7873

7974
/**
80-
* Moves the point to the supplied coordinates. refer to java.awt.Point for description of this.
75+
* Moves the point to the supplied coordinates.
8176
*
8277
* @param x integer coordinate
8378
* @param y integer coordinate
84-
* @see java.awt.Point
85-
*/
86-
public void setLocation(int x, int y) {
87-
move((double) x, (double) y);
88-
}
89-
90-
/**
91-
* Moves the point to the supplied java.awt.Point refer to java.awt.Point for description of this.
92-
*
93-
* @param p Point to move to
94-
* @see java.awt.Point
79+
* @return - return new Point with these coordinates
9580
*/
96-
public void setLocation(java.awt.Point p) {
97-
setLocation(p.x, p.y);
81+
public Point setLocation(int x, int y) {
82+
return move((double) x, (double) y);
9883
}
9984

10085
public boolean equals(Object obj) {

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.r2dbc.postgresql.type.PostgresqlObjectId;
88
import io.r2dbc.postgresql.util.Assert;
99
import io.r2dbc.postgresql.util.ByteBufUtils;
10+
import reactor.util.annotation.Nullable;
1011

1112
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
1213
import static io.r2dbc.postgresql.type.PostgresqlObjectId.POINT;
@@ -21,9 +22,8 @@ public class PointCodec extends AbstractCodec<Point> {
2122
}
2223

2324
@Override
24-
boolean doCanDecode(PostgresqlObjectId type, Format format) {
25+
boolean doCanDecode(PostgresqlObjectId type, @Nullable Format format) {
2526
Assert.requireNonNull(type, "type must not be null");
26-
Assert.requireNonNull(format, "format must not be null");
2727

2828
return POINT == type;
2929
}
@@ -49,8 +49,7 @@ Point doDecode(ByteBuf buffer, PostgresqlObjectId dataType, Format format, Class
4949
@Override
5050
Parameter doEncode(Point value) {
5151
Assert.requireNonNull(value, "value must not be null");
52-
String encodedValue = "(" + value.getX() + "," + value.getY() + ")";
53-
return create(POINT, FORMAT_BINARY, () -> ByteBufUtils.encode(this.byteBufAllocator, encodedValue));
52+
return create(POINT, FORMAT_BINARY, () -> ByteBufUtils.encode(this.byteBufAllocator, value.toString()));
5453
}
5554

5655
@Override

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

+11-17
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,13 @@ class PointCodecTest {
2020
@Test
2121
void constructorNoByteBufAllocator() {
2222
assertThatIllegalArgumentException().isThrownBy(() -> new PointCodec(null))
23-
.withMessage("byteBufAllocator must not be null");
23+
.withMessage("byteBufAllocator must not be null");
2424
}
2525

2626
@Test
2727
void doCanDecodeNoType() {
2828
assertThatIllegalArgumentException().isThrownBy(() -> new PointCodec(TEST).doCanDecode(null, FORMAT_BINARY))
29-
.withMessage("type must not be null");
30-
}
31-
32-
@Test
33-
void doCanDecodeNoFormat() {
34-
assertThatIllegalArgumentException().isThrownBy(() -> new PointCodec(TEST).doCanDecode(POINT, null))
35-
.withMessage("format must not be null");
29+
.withMessage("type must not be null");
3630
}
3731

3832
@Test
@@ -47,19 +41,19 @@ void doCanDecode() {
4741
@Test
4842
void doDecodeNoByteBuf() {
4943
assertThatIllegalArgumentException().isThrownBy(() -> new PointCodec(TEST).doDecode(null, POINT, FORMAT_BINARY, Point.class))
50-
.withMessage("byteBuf must not be null");
44+
.withMessage("byteBuf must not be null");
5145
}
5246

5347
@Test
5448
void doDecodeNoType() {
5549
assertThatIllegalArgumentException().isThrownBy(() -> new PointCodec(TEST).doDecode(TEST.buffer(), POINT, FORMAT_BINARY, null))
56-
.withMessage("type must not be null");
50+
.withMessage("type must not be null");
5751
}
5852

5953
@Test
6054
void doDecodeNoFormat() {
6155
assertThatIllegalArgumentException().isThrownBy(() -> new PointCodec(TEST).doDecode(TEST.buffer(), POINT, null, Point.class))
62-
.withMessage("format must not be null");
56+
.withMessage("format must not be null");
6357
}
6458

6559
@Test
@@ -69,28 +63,28 @@ void doDecode() {
6963
String encodedValue = "(1.12,2.12)";
7064

7165
assertThat(codec.doDecode(ByteBufUtils.encode(TEST, encodedValue), POINT, FORMAT_BINARY, Point.class))
72-
.isEqualTo(point);
66+
.isEqualTo(point);
7367
}
7468

7569
@Test
7670
void doEncodeNoValue() {
7771
assertThatIllegalArgumentException().isThrownBy(() -> new PointCodec(TEST).doEncode(null))
78-
.withMessage("value must not be null");
72+
.withMessage("value must not be null");
7973
}
8074

8175
@Test
8276
void doEncode() {
8377
PointCodec codec = new PointCodec(TEST);
8478

8579
ParameterAssert.assertThat(codec.doEncode(new Point(1.12, 2.12)))
86-
.hasFormat(FORMAT_BINARY)
87-
.hasType(POINT.getObjectId())
88-
.hasValue(encode(TEST, "(1.12,2.12)"));
80+
.hasFormat(FORMAT_BINARY)
81+
.hasType(POINT.getObjectId())
82+
.hasValue(encode(TEST, "(1.12,2.12)"));
8983
}
9084

9185
@Test
9286
void encodeNull() {
9387
ParameterAssert.assertThat(new PointCodec(TEST).encodeNull())
94-
.isEqualTo(new Parameter(FORMAT_BINARY, POINT.getObjectId(), NULL_VALUE));
88+
.isEqualTo(new Parameter(FORMAT_BINARY, POINT.getObjectId(), NULL_VALUE));
9589
}
9690
}

0 commit comments

Comments
 (0)