Skip to content

Commit 26d0f01

Browse files
committed
Polishing.
Use this. for instance field access. Align license headers. Simplify classpath detection. [#483][closes #491] Signed-off-by: Mark Paluch <[email protected]>
1 parent 6be02d9 commit 26d0f01

File tree

4 files changed

+44
-20
lines changed

4 files changed

+44
-20
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ This reference table shows the type mapping between [PostgreSQL][p] and Java dat
385385
| [`date`][psql-date-ref] | [`LocalDate`][java-ld-ref]|
386386
| [`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]|
387387
| [enumerated types][psql-enum-ref] | Client code `Enum` types through `EnumCodec`|
388+
| [`geometry`][postgis-ref] | **`org.locationtech.jts.geom.Geometry`**|
388389
| [`hstore`][psql-hstore-ref] | [**`Map`**][java-map-ref]|
389390
| [`inet`][psql-inet-ref] | [**`InetAddress`**][java-inet-ref]|
390391
| [`integer`][psql-integer-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]|
@@ -493,9 +494,10 @@ Support for the following single-dimensional arrays (read and write):
493494
[psql-uuid-ref]: https://www.postgresql.org/docs/current/datatype-uuid.html
494495
[psql-xml-ref]: https://www.postgresql.org/docs/current/datatype-xml.html
495496
[psql-runtime-config]: https://www.postgresql.org/docs/current/runtime-config-client.html
497+
[postgis-ref]: http://postgis.net/workshops/postgis-intro/geometries.html
496498

497-
[r2dbc-blob-ref]: https://r2dbc.io/spec/0.9.0.M1/api/io/r2dbc/spi/Blob.html
498-
[r2dbc-clob-ref]: https://r2dbc.io/spec/0.9.0.M1/api/io/r2dbc/spi/Clob.html
499+
[r2dbc-blob-ref]: https://r2dbc.io/spec/0.9.0.RELEASE/api/io/r2dbc/spi/Blob.html
500+
[r2dbc-clob-ref]: https://r2dbc.io/spec/0.9.0.RELEASE/api/io/r2dbc/spi/Clob.html
499501

500502
[java-bigdecimal-ref]: https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html
501503
[java-biginteger-ref]: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html
@@ -529,7 +531,7 @@ This driver accepts the following extensions:
529531

530532
Extensions can be registered programmatically using `PostgresConnectionConfiguration` or discovered using Java's `ServiceLoader` mechanism (from `META-INF/services/io.r2dbc.postgresql.extension.Extension`).
531533

532-
The driver ships with built-in dynamic codecs (e.g. `hstore`) that are registered during the connection handshake depending on their availability while connecting. Note that Postgres extensions registered after a connection was established require a reconnect to initialize the codec.
534+
The driver ships with built-in dynamic codecs (e.g. `hstore`, PostGIS `geometry`) that are registered during the connection handshake depending on their availability while connecting. Note that Postgres extensions registered after a connection was established require a reconnect to initialize the codec.
533535

534536
## Logging
535537
If SL4J is on the classpath, it will be used. Otherwise, there are two possible fallbacks: Console or `java.util.logging.Logger`). By default, the Console fallback is used. To use the JDK loggers, set the `reactor.logging.fallback` System property to `JDK`.

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

+11-12
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,17 @@ public class BuiltinDynamicCodecs implements CodecRegistrar {
3333

3434
private static final Object EMPTY = new Object();
3535

36-
private interface CodecSupport {
37-
default boolean isSupported() {
38-
return true;
39-
}
40-
}
4136

42-
enum BuiltinCodec implements CodecSupport {
37+
enum BuiltinCodec {
4338

4439
HSTORE("hstore"),
4540
POSTGIS_GEOMETRY("geometry") {
41+
42+
private final boolean jtsPresent = isPresent(BuiltinDynamicCodecs.class.getClassLoader(), "org.locationtech.jts.geom.Geometry");
43+
4644
@Override
4745
public boolean isSupported() {
48-
String className = "org.locationtech.jts.geom.Geometry";
49-
ClassLoader classLoader = getClass().getClassLoader();
50-
51-
return isPresent(classLoader, className);
46+
return this.jtsPresent;
5247
}
5348
};
5449

@@ -74,6 +69,10 @@ public String getName() {
7469
return this.name;
7570
}
7671

72+
boolean isSupported() {
73+
return true;
74+
}
75+
7776
static BuiltinCodec lookup(@Nullable String name) {
7877

7978
for (BuiltinCodec codec : values()) {
@@ -115,9 +114,9 @@ private static String getPlaceholders() {
115114
return Arrays.stream(BuiltinCodec.values()).map(s -> "'" + s.getName() + "'").collect(Collectors.joining(","));
116115
}
117116

118-
private static boolean isPresent(ClassLoader classLoader, String fullyQualifiedClassName) {
117+
private static boolean isPresent(ClassLoader classLoader, String name) {
119118
try {
120-
classLoader.loadClass(fullyQualifiedClassName);
119+
Class.forName(name, false, classLoader);
121120
return true;
122121
} catch (ClassNotFoundException e) {
123122
return false;

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import io.r2dbc.postgresql.util.Assert;
2424
import io.r2dbc.postgresql.util.ByteBufUtils;
2525
import org.locationtech.jts.geom.Geometry;
26+
import org.locationtech.jts.geom.GeometryFactory;
2627
import org.locationtech.jts.io.ParseException;
2728
import org.locationtech.jts.io.WKBReader;
29+
import org.locationtech.jts.io.WKTWriter;
2830
import reactor.core.publisher.Mono;
2931

3032
import javax.annotation.Nullable;
@@ -34,12 +36,17 @@
3436
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
3537
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
3638

39+
/**
40+
* PostGIS codec using {@link WKBReader} and {@link WKTWriter}.
41+
*/
3742
final class PostgisGeometryCodec implements Codec<Geometry>, CodecMetadata {
3843

3944
private static final Class<Geometry> TYPE = Geometry.class;
4045

4146
private final ByteBufAllocator byteBufAllocator;
4247

48+
private final GeometryFactory geometryFactory = new GeometryFactory();
49+
4350
private final int oid;
4451

4552
/**
@@ -83,7 +90,7 @@ public Geometry decode(@Nullable ByteBuf buffer, int dataType, Format format, Cl
8390
Assert.isTrue(format == FORMAT_TEXT, "format must be FORMAT_TEXT");
8491

8592
try {
86-
return new WKBReader().read(WKBReader.hexToBytes(ByteBufUtils.decode(buffer)));
93+
return new WKBReader(this.geometryFactory).read(WKBReader.hexToBytes(ByteBufUtils.decode(buffer)));
8794
} catch (ParseException e) {
8895
throw new IllegalArgumentException(e);
8996
}
@@ -94,8 +101,8 @@ public EncodedParameter encode(Object value) {
94101
Assert.requireType(value, Geometry.class, "value must be Geometry type");
95102
Geometry geometry = (Geometry) value;
96103

97-
return new EncodedParameter(Format.FORMAT_TEXT, oid, Mono.fromSupplier(
98-
() -> ByteBufUtils.encode(byteBufAllocator, geometry.toText())
104+
return new EncodedParameter(Format.FORMAT_TEXT, this.oid, Mono.fromSupplier(
105+
() -> ByteBufUtils.encode(this.byteBufAllocator, geometry.toText())
99106
));
100107
}
101108

@@ -106,7 +113,7 @@ public EncodedParameter encode(Object value, int dataType) {
106113

107114
@Override
108115
public EncodedParameter encodeNull() {
109-
return new EncodedParameter(FORMAT_BINARY, oid, NULL_VALUE);
116+
return new EncodedParameter(FORMAT_BINARY, this.oid, NULL_VALUE);
110117
}
111118

112119
@Override

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package io.r2dbc.postgresql.codec;
218

319
import io.netty.buffer.ByteBuf;
@@ -30,7 +46,7 @@
3046
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3147

3248
/**
33-
* Unit tests for {@link PostgisGeometryCodec }.
49+
* Unit tests for {@link PostgisGeometryCodec}.
3450
*/
3551
final class PostgisGeometryCodecUnitTests {
3652

0 commit comments

Comments
 (0)