29
29
import com .google .protobuf .ListValue ;
30
30
import com .google .protobuf .NullValue ;
31
31
import com .google .protobuf .Value .KindCase ;
32
+ import java .io .IOException ;
32
33
import java .io .Serializable ;
33
34
import java .math .BigDecimal ;
34
35
import java .util .ArrayList ;
@@ -227,16 +228,22 @@ public static Value pgJsonb(@Nullable String v) {
227
228
* @param v the value, which may be null
228
229
*/
229
230
public static Value bytes (@ Nullable ByteArray v ) {
230
- return new BytesImpl (v == null , v );
231
+ return new LazyBytesImpl (v == null , v );
231
232
}
232
233
233
234
/**
234
- * Returns a {@code BYTES} value that is lazily constructed when needed .
235
+ * Returns a {@code BYTES} value.
235
236
*
236
- * @param v the value, which may be null
237
+ * @param base64String the value in Base64 encoding, which may be null. This value must be a valid
238
+ * base64 string.
237
239
*/
238
- static Value lazyBytes (@ Nullable LazyByteArray v ) {
239
- return new LazyBytesImpl (v == null , v );
240
+ public static Value bytesFromBase64 (@ Nullable String base64String ) {
241
+ return new LazyBytesImpl (
242
+ base64String == null , base64String == null ? null : new LazyByteArray (base64String ));
243
+ }
244
+
245
+ static Value internalBytes (@ Nullable LazyByteArray bytes ) {
246
+ return new LazyBytesImpl (bytes == null , bytes );
240
247
}
241
248
242
249
/** Returns a {@code TIMESTAMP} value. */
@@ -441,11 +448,37 @@ public static Value pgJsonbArray(@Nullable Iterable<String> v) {
441
448
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
442
449
*/
443
450
public static Value bytesArray (@ Nullable Iterable <ByteArray > v ) {
444
- return new BytesArrayImpl (v == null , v == null ? null : immutableCopyOf (v ));
451
+ return new LazyBytesArrayImpl (v == null , v == null ? null : byteArraysToLazyByteArrayList (v ));
445
452
}
446
453
447
- static Value lazyBytesArray (@ Nullable Iterable <LazyByteArray > v ) {
448
- return new LazyBytesArrayImpl (v == null , v == null ? null : immutableCopyOf (v ));
454
+ private static List <LazyByteArray > byteArraysToLazyByteArrayList (Iterable <ByteArray > byteArrays ) {
455
+ List <LazyByteArray > list = new ArrayList <>();
456
+ for (ByteArray byteArray : byteArrays ) {
457
+ list .add (byteArray == null ? null : new LazyByteArray (byteArray ));
458
+ }
459
+ return Collections .unmodifiableList (list );
460
+ }
461
+
462
+ /**
463
+ * Returns an {@code ARRAY<BYTES>} value.
464
+ *
465
+ * @param base64Strings the source of element values. This may be {@code null} to produce a value
466
+ * for which {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
467
+ * Non-null values must be a valid Base64 string.
468
+ */
469
+ public static Value bytesArrayFromBase64 (@ Nullable Iterable <String > base64Strings ) {
470
+ return new LazyBytesArrayImpl (
471
+ base64Strings == null ,
472
+ base64Strings == null ? null : base64StringsToLazyByteArrayList (base64Strings ));
473
+ }
474
+
475
+ private static List <LazyByteArray > base64StringsToLazyByteArrayList (
476
+ Iterable <String > base64Strings ) {
477
+ List <LazyByteArray > list = new ArrayList <>();
478
+ for (String base64 : base64Strings ) {
479
+ list .add (base64 == null ? null : new LazyByteArray (base64 ));
480
+ }
481
+ return Collections .unmodifiableList (list );
449
482
}
450
483
451
484
/**
@@ -1400,41 +1433,16 @@ void valueToString(StringBuilder b) {
1400
1433
}
1401
1434
}
1402
1435
1403
- private static class BytesImpl extends AbstractObjectValue <ByteArray > {
1404
-
1405
- private BytesImpl (boolean isNull , ByteArray value ) {
1406
- super (isNull , Type .bytes (), value );
1407
- }
1408
-
1409
- @ Override
1410
- public ByteArray getBytes () {
1411
- checkNotNull ();
1412
- return value ;
1413
- }
1414
-
1415
- @ Override
1416
- com .google .protobuf .Value valueToProto () {
1417
- return com .google .protobuf .Value .newBuilder ().setStringValue (value .toBase64 ()).build ();
1418
- }
1419
-
1420
- @ Nonnull
1421
- @ Override
1422
- public String getAsString () {
1423
- return value == null ? NULL_STRING : value .toBase64 ();
1424
- }
1425
-
1426
- @ Override
1427
- void valueToString (StringBuilder b ) {
1428
- b .append (value .toString ());
1429
- }
1430
- }
1431
-
1432
1436
private static class LazyBytesImpl extends AbstractObjectValue <LazyByteArray > {
1433
1437
1434
1438
private LazyBytesImpl (boolean isNull , LazyByteArray value ) {
1435
1439
super (isNull , Type .bytes (), value );
1436
1440
}
1437
1441
1442
+ private LazyBytesImpl (boolean isNull , ByteArray value ) {
1443
+ super (isNull , Type .bytes (), value == null ? null : new LazyByteArray (value ));
1444
+ }
1445
+
1438
1446
@ Override
1439
1447
public ByteArray getBytes () {
1440
1448
checkNotNull ();
@@ -1449,12 +1457,12 @@ com.google.protobuf.Value valueToProto() {
1449
1457
@ Nonnull
1450
1458
@ Override
1451
1459
public String getAsString () {
1452
- return value .getBase64String ();
1460
+ return value == null ? NULL_STRING : value .getBase64String ();
1453
1461
}
1454
1462
1455
1463
@ Override
1456
1464
void valueToString (StringBuilder b ) {
1457
- b .append (value .toString ());
1465
+ b .append (value == null ? null : value .toString ());
1458
1466
}
1459
1467
}
1460
1468
@@ -1906,39 +1914,28 @@ void appendElement(StringBuilder b, String element) {
1906
1914
}
1907
1915
}
1908
1916
1909
- private static class BytesArrayImpl extends AbstractArrayValue <ByteArray > {
1910
- private BytesArrayImpl (boolean isNull , @ Nullable List <ByteArray > values ) {
1911
- super (isNull , Type .bytes (), values );
1912
- }
1913
-
1914
- @ Override
1915
- public List <ByteArray > getBytesArray () {
1916
- checkNotNull ();
1917
- return value ;
1918
- }
1917
+ private static class LazyBytesArrayImpl extends AbstractArrayValue <LazyByteArray > {
1918
+ private transient AbstractLazyInitializer <List <ByteArray >> bytesArray = defaultInitializer ();
1919
1919
1920
- @ Override
1921
- String elementToString (ByteArray element ) {
1922
- return element .toBase64 ();
1920
+ private LazyBytesArrayImpl (boolean isNull , @ Nullable List <LazyByteArray > values ) {
1921
+ super (isNull , Type .bytes (), values );
1923
1922
}
1924
1923
1925
- @ Override
1926
- void appendElement (StringBuilder b , ByteArray element ) {
1927
- b .append (elementToString (element ));
1924
+ private AbstractLazyInitializer <List <ByteArray >> defaultInitializer () {
1925
+ return new AbstractLazyInitializer <List <ByteArray >>() {
1926
+ @ Override
1927
+ protected List <ByteArray > initialize () {
1928
+ return value .stream ()
1929
+ .map (element -> element == null ? null : element .getByteArray ())
1930
+ .collect (Collectors .toList ());
1931
+ }
1932
+ };
1928
1933
}
1929
- }
1930
-
1931
- private static class LazyBytesArrayImpl extends AbstractArrayValue <LazyByteArray > {
1932
- private final AbstractLazyInitializer <List <ByteArray >> bytesArray =
1933
- new AbstractLazyInitializer <List <ByteArray >>() {
1934
- @ Override
1935
- protected List <ByteArray > initialize () {
1936
- return value .stream ().map (LazyByteArray ::getByteArray ).collect (Collectors .toList ());
1937
- }
1938
- };
1939
1934
1940
- private LazyBytesArrayImpl (boolean isNull , @ Nullable List <LazyByteArray > values ) {
1941
- super (isNull , Type .bytes (), values );
1935
+ private void readObject (java .io .ObjectInputStream in )
1936
+ throws IOException , ClassNotFoundException {
1937
+ in .defaultReadObject ();
1938
+ bytesArray = defaultInitializer ();
1942
1939
}
1943
1940
1944
1941
@ Override
0 commit comments