Skip to content

Commit 2c3312b

Browse files
committed
Polishing
Make Point final and reduce PointCodec visibility. Introduce factory method for Point type. Update documentation. [resolves #283][#282]
1 parent 69d1258 commit 2c3312b

File tree

5 files changed

+48
-67
lines changed

5 files changed

+48
-67
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ This reference table shows the type mapping between [PostgreSQL][p] and Java dat
360360
| [`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]|
361361
| [`path`][psql-path-ref] | Not yet supported.|
362362
| [`pg_lsn`][psql-pg_lsn-ref] | Not yet supported.|
363-
| [`point`][psql-point-ref] | Not yet supported.|
363+
| [`point`][psql-point-ref] | **`Point`**|
364364
| [`polygon`][psql-polygon-ref] | Not yet supported.|
365365
| [`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]|
366366
| [`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]|
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 the original author or authors.
2+
* Copyright 2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,103 +17,81 @@
1717
package io.r2dbc.postgresql.codec;
1818

1919
/**
20-
* <p>It maps to the point datatype in org.postgresql.</p>
21-
*
22-
* <p> It uses double to represent the coordinates.</p>
20+
* Value object that maps to the {@code point} datatype in Postgres.
21+
* <p>
22+
* Uses {@code double} to represent the coordinates.
2323
*/
24-
public class Point {
24+
public final class Point {
2525

2626
private final double x;
2727

2828
private final double y;
2929

30-
/**
31-
* @param x coordinate
32-
* @param y coordinate
33-
*/
34-
public Point(double x, double y) {
30+
private Point(double x, double y) {
3531
this.x = x;
3632
this.y = y;
3733
}
3834

35+
/**
36+
* Create a new {@link Point} given {@code x} and {@code y} coordinates.
37+
*
38+
* @param x the x axis coordinate
39+
* @param y the y axis coordinate
40+
* @return the new {@link Point} object
41+
*/
42+
public static Point of(double x, double y) {
43+
return new Point(x, y);
44+
}
45+
3946
public double getX() {
40-
return x;
47+
return this.x;
4148
}
4249

4350
public double getY() {
44-
return y;
51+
return this.y;
4552
}
4653

4754
/**
48-
* Translate the point by the supplied amount.
55+
* Translate the point by the supplied amount by adding {@code x} and {@code y} offsets.
4956
*
5057
* @param x integer amount to add on the x axis
5158
* @param y integer amount to add on the y axis
52-
* @return - new point with translated values
59+
* @return new {@link Point} with translated values
5360
*/
5461
public Point translate(int x, int y) {
5562
return translate((double) x, (double) y);
5663
}
5764

5865
/**
59-
* Translate the point by the supplied amount.
66+
* Translate the point by the supplied amount by adding {@code x} and {@code y} offsets.
6067
*
6168
* @param x double amount to add on the x axis
6269
* @param y double amount to add on the y axis
63-
* @return - new point with translated values
70+
* @return new {@link Point} with translated values
6471
*/
6572
public Point translate(double x, double y) {
6673
return new Point(this.x + x, this.y + y);
6774
}
6875

69-
/**
70-
* Moves the point to the supplied coordinates.
71-
*
72-
* @param x integer coordinate
73-
* @param y integer coordinate
74-
*/
75-
public Point move(int x, int y) {
76-
return setLocation(x, y);
77-
}
78-
79-
/**
80-
* Moves the point to the supplied coordinates.
81-
*
82-
* @param x double coordinate
83-
* @param y double coordinate
84-
* @return - new point with provided coordinates
85-
*/
86-
public Point move(double x, double y) {
87-
return new Point(x, y);
88-
}
89-
90-
/**
91-
* Moves the point to the supplied coordinates.
92-
*
93-
* @param x integer coordinate
94-
* @param y integer coordinate
95-
* @return - return new Point with these coordinates
96-
*/
97-
public Point setLocation(int x, int y) {
98-
return move((double) x, (double) y);
99-
}
100-
76+
@Override
10177
public boolean equals(Object obj) {
10278
if (obj instanceof Point) {
10379
Point p = (Point) obj;
104-
return x == p.x && y == p.y;
80+
return this.x == p.x && this.y == p.y;
10581
}
10682
return false;
10783
}
10884

85+
@Override
10986
public int hashCode() {
110-
long v1 = Double.doubleToLongBits(x);
111-
long v2 = Double.doubleToLongBits(y);
87+
long v1 = Double.doubleToLongBits(this.x);
88+
long v2 = Double.doubleToLongBits(this.y);
11289
return (int) (v1 ^ v2 ^ (v1 >>> 32) ^ (v2 >>> 32));
11390
}
11491

92+
@Override
11593
public String toString() {
116-
return "(" + x + "," + y + ")";
94+
return "(" + this.x + "," + this.y + ")";
11795
}
11896

11997
}

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

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 the original author or authors.
2+
* Copyright 2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@
2828
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
2929
import static io.r2dbc.postgresql.type.PostgresqlObjectId.POINT;
3030

31-
public class PointCodec extends AbstractCodec<Point> {
31+
final class PointCodec extends AbstractCodec<Point> {
3232

3333
private final ByteBufAllocator byteBufAllocator;
3434

@@ -53,15 +53,16 @@ Point doDecode(ByteBuf buffer, PostgresqlObjectId dataType, Format format, Class
5353
if (format == FORMAT_BINARY) {
5454
double x = buffer.readDouble();
5555
double y = buffer.readDouble();
56-
return new Point(x, y);
57-
} else {
58-
String decodedAsString = ByteBufUtils.decode(buffer);
59-
String parenRemovedVal = decodedAsString.replaceAll("[()]", "");
60-
String[] coordinatesAsString = parenRemovedVal.split(",");
61-
double x = Double.parseDouble(coordinatesAsString[0]);
62-
double y = Double.parseDouble(coordinatesAsString[1]);
63-
return new Point(x, y);
56+
return Point.of(x, y);
6457
}
58+
59+
60+
String decodedAsString = ByteBufUtils.decode(buffer);
61+
String parenRemovedVal = decodedAsString.replaceAll("[()]", "");
62+
String[] coordinatesAsString = parenRemovedVal.split(",");
63+
double x = Double.parseDouble(coordinatesAsString[0]);
64+
double y = Double.parseDouble(coordinatesAsString[1]);
65+
return Point.of(x, y);
6566
}
6667

6768
/**
@@ -79,7 +80,7 @@ public Parameter encodeNull() {
7980
return createNull(POINT, FORMAT_BINARY);
8081
}
8182

82-
public int lengthInBytes() {
83+
int lengthInBytes() {
8384
return 16;
8485
}
8586

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,9 @@ void offsetDateTime() {
363363

364364
@Test
365365
void point() {
366-
testCodec(Point.class, new Point(1.12, 2.12), "POINT");
366+
testCodec(Point.class, Point.of(1.12, 2.12), "POINT");
367+
testCodec(Point.class, Point.of(Integer.MIN_VALUE, Integer.MAX_VALUE), "POINT");
368+
testCodec(Point.class, Point.of(Double.MIN_VALUE, Double.MAX_VALUE), "POINT");
367369
}
368370

369371
@Test

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void doDecodeNoFormat() {
7474
@Test
7575
void doDecode() {
7676
PointCodec codec = new PointCodec(TEST);
77-
Point point = new Point(1.12, 2.12);
77+
Point point = Point.of(1.12, 2.12);
7878
ByteBuf pointAsBinary = TEST.buffer(codec.lengthInBytes()).writeDouble(1.12).writeDouble(2.12);
7979
assertThat(codec.doDecode(pointAsBinary, POINT, FORMAT_BINARY, Point.class)).isEqualTo(point);
8080
}
@@ -90,7 +90,7 @@ void doEncode() {
9090
PointCodec codec = new PointCodec(TEST);
9191
ByteBuf pointAsBinary = TEST.buffer(codec.lengthInBytes()).writeDouble(1.12).writeDouble(2.12);
9292

93-
ParameterAssert.assertThat(codec.doEncode(new Point(1.12, 2.12)))
93+
ParameterAssert.assertThat(codec.doEncode(Point.of(1.12, 2.12)))
9494
.hasFormat(FORMAT_BINARY)
9595
.hasType(POINT.getObjectId())
9696
.hasValue(pointAsBinary);

0 commit comments

Comments
 (0)