Skip to content

Commit efd7899

Browse files
committed
Polishing
Add fast-path BigInteger conversion from BigDecimal. Use long value for all other numeric types to construct a BigInteger. Update copyright license header years. [#233][#235]
1 parent 69d832b commit efd7899

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ This reference table shows the type mapping between [PostgreSQL][p] and Java dat
312312
| [`cidr`][psql-cidr-ref] | Not yet supported.|
313313
| [`circle`][psql-circle-ref] | Not yet supported.|
314314
| [`date`][psql-date-ref] | [`LocalDate`][java-ld-ref]|
315-
| [`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]|
315+
| [`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]|
316316
| [`inet`][psql-inet-ref] | [**`InetAddress`**][java-inet-ref]|
317317
| [`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]|
318318
| [`interval`][psql-interval-ref] | Not yet supported.|

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 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.
@@ -25,6 +25,7 @@
2525
import io.r2dbc.postgresql.util.ByteBufUtils;
2626
import reactor.util.annotation.Nullable;
2727

28+
import java.math.BigDecimal;
2829
import java.math.BigInteger;
2930

3031
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
@@ -48,7 +49,9 @@ public Parameter encodeNull() {
4849
BigInteger doDecode(ByteBuf buffer, PostgresqlObjectId dataType, @Nullable Format format, @Nullable Class<? extends BigInteger> type) {
4950
Assert.requireNonNull(buffer, "byteBuf must not be null");
5051

51-
return this.decodeNumber(buffer, dataType, format, BigInteger.class, it -> new BigInteger(it.toString()));
52+
return this.decodeNumber(buffer, dataType, format, BigInteger.class, it -> {
53+
return it instanceof BigDecimal ? ((BigDecimal) it).toBigInteger() : BigInteger.valueOf(it.longValue());
54+
});
5255
}
5356

5457
@Override

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original author or authors.
2+
* Copyright 2019-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.
@@ -81,9 +81,9 @@ void bigDecimal() {
8181
void bigInteger() {
8282
testCodec(BigInteger.class, new BigInteger("1000"), "NUMERIC");
8383
testCodec(BigInteger.class, new BigInteger("-1"), "NUMERIC");
84-
testCodec(BigInteger.class, new BigInteger("10000"), "NUMERIC");
85-
testCodec(BigInteger.class, new BigInteger("10010"), "NUMERIC");
86-
testCodec(BigInteger.class, new BigInteger("2000010010"), "NUMERIC");
84+
testCodecReadAs(new BigDecimal("10000.0000023"), BigInteger.class, new BigInteger("10000"), "NUMERIC");
85+
testCodecReadAs(new BigDecimal("10010.1200023"), BigInteger.class, new BigInteger("10010"), "NUMERIC");
86+
testCodecReadAs(new BigDecimal("2000010010.1200023"), BigInteger.class, new BigInteger("2000010010"), "NUMERIC");
8787
testCodec(BigInteger.class, new BigInteger("0"), "NUMERIC");
8888
testCodec(BigInteger.class, new BigInteger("100"), "INT2");
8989
testCodec(BigInteger.class, new BigInteger("100"), "INT4");
@@ -490,7 +490,6 @@ private <T> void testCodec(Class<T> javaType, T value, BiConsumer<T, T> equality
490490
}
491491
}
492492

493-
494493
private <T> void testRead(Class<T> javaType, T value, String sqlType, String insertPlaceholder) {
495494
testCodec(javaType, value, (actual, expected) -> {
496495

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 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.
@@ -40,15 +40,15 @@ class BigIntegerCodecTest {
4040
@Test
4141
void constructorNoByteBufAllocator() {
4242
assertThatIllegalArgumentException().isThrownBy(() -> new BigIntegerCodec(null))
43-
.withMessage("byteBufAllocator must not be null");
43+
.withMessage("byteBufAllocator must not be null");
4444
}
4545

4646
@Test
4747
void decode() {
4848
BigInteger bigInteger = new BigInteger("100");
4949

5050
assertThat(new BigIntegerCodec(TEST).decode(encode(TEST, bigInteger.toString()), dataType, FORMAT_TEXT, BigInteger.class))
51-
.isEqualTo(bigInteger);
51+
.isEqualTo(bigInteger);
5252
}
5353

5454
@Test
@@ -68,35 +68,35 @@ void doCanDecode() {
6868
@Test
6969
void doCanDecodeNoFormat() {
7070
assertThatIllegalArgumentException().isThrownBy(() -> new BigIntegerCodec(TEST).doCanDecode(VARCHAR, null))
71-
.withMessage("format must not be null");
71+
.withMessage("format must not be null");
7272
}
7373

7474
@Test
7575
void doCanDecodeNoType() {
7676
assertThatIllegalArgumentException().isThrownBy(() -> new BigIntegerCodec(TEST).doCanDecode(null, FORMAT_TEXT))
77-
.withMessage("type must not be null");
77+
.withMessage("type must not be null");
7878
}
7979

8080
@Test
8181
void doEncode() {
8282
BigInteger bigInteger = new BigInteger("100");
8383

8484
assertThat(new BigIntegerCodec(TEST).doEncode(bigInteger))
85-
.hasFormat(FORMAT_TEXT)
86-
.hasType(NUMERIC.getObjectId())
87-
.hasValue(encode(TEST, "100"));
85+
.hasFormat(FORMAT_TEXT)
86+
.hasType(NUMERIC.getObjectId())
87+
.hasValue(encode(TEST, "100"));
8888
}
8989

9090
@Test
9191
void doEncodeNoValue() {
9292
assertThatIllegalArgumentException().isThrownBy(() -> new BigIntegerCodec(TEST).doEncode(null))
93-
.withMessage("value must not be null");
93+
.withMessage("value must not be null");
9494
}
9595

9696
@Test
9797
void encodeNull() {
9898
assertThat(new BigIntegerCodec(TEST).encodeNull())
99-
.isEqualTo(new Parameter(FORMAT_TEXT, NUMERIC.getObjectId(), NULL_VALUE));
99+
.isEqualTo(new Parameter(FORMAT_TEXT, NUMERIC.getObjectId(), NULL_VALUE));
100100
}
101-
101+
102102
}

0 commit comments

Comments
 (0)