Skip to content

Commit 83ffd14

Browse files
committed
Polishing
Reformat code. Add null guards. Update readme. [pgjdbc#282][closes pgjdbc#300]
1 parent f649a3c commit 83ffd14

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ This reference table shows the type mapping between [PostgreSQL][p] and Java dat
348348
| [`character`][psql-character-ref] | [`String`][java-string-ref]|
349349
| [`character varying`][psql-character-ref] | [`String`][java-string-ref]|
350350
| [`cidr`][psql-cidr-ref] | Not yet supported.|
351-
| [`circle`][psql-circle-ref] | Not yet supported.|
351+
| [`circle`][psql-circle-ref] | **`Circle`**|
352352
| [`date`][psql-date-ref] | [`LocalDate`][java-ld-ref]|
353353
| [`double precision`][psql-floating-point-ref] | [**`Double`**][java-double-ref], [`Float`][java-float-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]|
354354
| [enumerated types][psql-enum-ref] | Client code `Enum` types through `EnumCodec`|

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,21 @@
1616

1717
package io.r2dbc.postgresql.codec;
1818

19+
import io.r2dbc.postgresql.util.Assert;
20+
21+
/**
22+
* Value object that maps to the {@code circle} datatype in Postgres.
23+
* <p>
24+
* Uses {@code double} to represent the coordinates.
25+
*/
1926
public final class Circle {
27+
2028
private final Point center;
29+
2130
private final double radius;
2231

2332
public Circle(Point center, double radius) {
24-
this.center = center;
33+
this.center = Assert.requireNonNull(center, "center must not be null");
2534
this.radius = radius;
2635
}
2736

@@ -45,20 +54,20 @@ public double getRadius() {
4554
public boolean equals(Object obj) {
4655
if (obj instanceof Circle) {
4756
Circle circle = (Circle) obj;
48-
return circle.center.equals(center) && circle.radius == radius;
57+
return circle.center.equals(this.center) && circle.radius == this.radius;
4958
}
5059
return false;
5160
}
5261

5362
@Override
5463
public int hashCode() {
55-
long v = Double.doubleToLongBits(radius);
56-
return (int) (center.hashCode() ^ v ^ (v >>> 32));
64+
long v = Double.doubleToLongBits(this.radius);
65+
return (int) (this.center.hashCode() ^ v ^ (v >>> 32));
5766
}
5867

5968
@Override
6069
public String toString() {
61-
return "<" + center.toString() + "," + radius + ">";
70+
return "<" + this.center.toString() + "," + this.radius + ">";
6271
}
6372

6473
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ Parameter doEncode(Circle value) {
7474
Assert.requireNonNull(value, "value must not be null");
7575
Point center = value.getCenter();
7676
return create(CIRCLE, FORMAT_BINARY, () -> this.byteBufAllocator.buffer(lengthInBytes())
77-
.writeDouble(center.getX())
78-
.writeDouble(center.getY())
79-
.writeDouble(value.getRadius()));
77+
.writeDouble(center.getX())
78+
.writeDouble(center.getY())
79+
.writeDouble(value.getRadius()));
8080
}
8181

8282
@Override

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

+11-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.
@@ -38,13 +38,13 @@ final class CircleCodecUnitTests {
3838
@Test
3939
void constructorNoByteBufAllocator() {
4040
assertThatIllegalArgumentException().isThrownBy(() -> new CircleCodec(null))
41-
.withMessage("byteBufAllocator must not be null");
41+
.withMessage("byteBufAllocator must not be null");
4242
}
4343

4444
@Test
4545
void doCanDecodeNoType() {
4646
assertThatIllegalArgumentException().isThrownBy(() -> new CircleCodec(TEST).doCanDecode(null, FORMAT_BINARY))
47-
.withMessage("type must not be null");
47+
.withMessage("type must not be null");
4848
}
4949

5050
@Test
@@ -59,19 +59,19 @@ void doCanDecode() {
5959
@Test
6060
void doDecodeNoByteBuf() {
6161
assertThatIllegalArgumentException().isThrownBy(() -> new CircleCodec(TEST).doDecode(null, CIRCLE, FORMAT_BINARY, Circle.class))
62-
.withMessage("byteBuf must not be null");
62+
.withMessage("byteBuf must not be null");
6363
}
6464

6565
@Test
6666
void doDecodeNoType() {
6767
assertThatIllegalArgumentException().isThrownBy(() -> new CircleCodec(TEST).doDecode(TEST.buffer(), CIRCLE, FORMAT_BINARY, null))
68-
.withMessage("type must not be null");
68+
.withMessage("type must not be null");
6969
}
7070

7171
@Test
7272
void doDecodeNoFormat() {
7373
assertThatIllegalArgumentException().isThrownBy(() -> new CircleCodec(TEST).doDecode(TEST.buffer(), CIRCLE, null, Circle.class))
74-
.withMessage("format must not be null");
74+
.withMessage("format must not be null");
7575
}
7676

7777
@Test
@@ -86,7 +86,7 @@ void doDecode() {
8686
@Test
8787
void doEncodeNoValue() {
8888
assertThatIllegalArgumentException().isThrownBy(() -> new CircleCodec(TEST).doEncode(null))
89-
.withMessage("value must not be null");
89+
.withMessage("value must not be null");
9090
}
9191

9292
@Test
@@ -95,15 +95,15 @@ void doEncode() {
9595
ByteBuf circleAsBinary = TEST.buffer(codec.lengthInBytes()).writeDouble(1.12).writeDouble(2.12).writeDouble(3.12);
9696

9797
ParameterAssert.assertThat(codec.doEncode(new Circle(Point.of(1.12, 2.12), 3.12)))
98-
.hasFormat(FORMAT_BINARY)
99-
.hasType(CIRCLE.getObjectId())
100-
.hasValue(circleAsBinary);
98+
.hasFormat(FORMAT_BINARY)
99+
.hasType(CIRCLE.getObjectId())
100+
.hasValue(circleAsBinary);
101101
}
102102

103103
@Test
104104
void encodeNull() {
105105
ParameterAssert.assertThat(new CircleCodec(TEST).encodeNull())
106-
.isEqualTo(new Parameter(FORMAT_BINARY, CIRCLE.getObjectId(), NULL_VALUE));
106+
.isEqualTo(new Parameter(FORMAT_BINARY, CIRCLE.getObjectId(), NULL_VALUE));
107107
}
108108

109109
}

0 commit comments

Comments
 (0)