/
Value.java
1648 lines (1409 loc) · 48.1 KB
/
Value.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.spanner;
import com.google.cloud.ByteArray;
import com.google.cloud.Date;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.Type.Code;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.protobuf.ListValue;
import com.google.protobuf.NullValue;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/**
* Represents a value to be consumed by the Cloud Spanner API. A value can be {@code NULL} or
* non-{@code NULL}; regardless, values always have an associated type.
*
* <p>The {@code Value} API is optimized for construction, since this is the majority use-case when
* using this class with the Cloud Spanner libraries. The factory method signatures and internal
* representations are design to minimize memory usage and object creation while still maintaining
* the immutability contract of this class. In particular, arrays of primitive types can be
* constructed without requiring boxing into collections of wrapper types. The getters in this class
* are intended primarily for test purposes, and so do not share the same performance
* characteristics; in particular, getters for array types may be expensive.
*
* <p>{@code Value} instances are immutable.
*/
@Immutable
public abstract class Value implements Serializable {
/**
* Placeholder value to be passed to a mutation to make Cloud Spanner store the commit timestamp
* in that column. The commit timestamp is the timestamp corresponding to when Cloud Spanner
* commits the transaction containing the mutation.
*
* <p>Note that this particular timestamp instance has no semantic meaning. In particular the
* value of seconds and nanoseconds in this timestamp are meaningless. This placeholder can only
* be used for columns that have set the option "(allow_commit_timestamp=true)" in the schema.
*
* <p>When reading the value stored in such a column, the value returned is an actual timestamp
* corresponding to the commit time of the transaction, which has no relation to this placeholder.
*
* @see <a href="https://cloud.google.com/spanner/docs/transactions#rw_transaction_semantics">
* Transaction Semantics</a>
*/
public static final Timestamp COMMIT_TIMESTAMP = Timestamp.ofTimeMicroseconds(0L);
private static final int MAX_DEBUG_STRING_LENGTH = 36;
private static final String ELLIPSIS = "...";
private static final String NULL_STRING = "NULL";
private static final char LIST_SEPARATOR = ',';
private static final char LIST_OPEN = '[';
private static final char LIST_CLOSE = ']';
private static final long serialVersionUID = -5289864325087675338L;
/**
* Returns a {@code BOOL} value.
*
* @param v the value, which may be null
*/
public static Value bool(@Nullable Boolean v) {
return new BoolImpl(v == null, v == null ? false : v);
}
/** Returns a {@code BOOL} value. */
public static Value bool(boolean v) {
return new BoolImpl(false, v);
}
/**
* Returns an {@code INT64} value.
*
* @param v the value, which may be null
*/
public static Value int64(@Nullable Long v) {
return new Int64Impl(v == null, v == null ? 0 : v);
}
/** Returns an {@code INT64} value. */
public static Value int64(long v) {
return new Int64Impl(false, v);
}
/**
* Returns a {@code FLOAT64} value.
*
* @param v the value, which may be null
*/
public static Value float64(@Nullable Double v) {
return new Float64Impl(v == null, v == null ? 0 : v);
}
/** Returns a {@code FLOAT64} value. */
public static Value float64(double v) {
return new Float64Impl(false, v);
}
/**
* Returns a {@code NUMERIC} value. The valid value range for the whole component of the {@link
* BigDecimal} is from -9,999,999,999,999,999,999,999,999 to +9,999,999,999,999,999,999,999,999
* (both inclusive), i.e. the max length of the whole component is 29 digits. The max length of
* the fractional part is 9 digits. Trailing zeros in the fractional part are not considered and
* will be lost, as Cloud Spanner does not preserve the precision of a numeric value.
*
* <p>If you set a numeric value of a record to for example 0.10, Cloud Spanner will return this
* value as 0.1 in subsequent queries. Use {@link BigDecimal#stripTrailingZeros()} to compare
* inserted values with retrieved values if your application might insert numeric values with
* trailing zeros.
*
* @param v the value, which may be null
*/
public static Value numeric(@Nullable BigDecimal v) {
if (v != null) {
// Cloud Spanner does not preserve the precision, so 0.1 is considered equal to 0.10.
BigDecimal test = v.stripTrailingZeros();
if (test.scale() > 9) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.OUT_OF_RANGE,
String.format(
"Max scale for a numeric is 9. The requested numeric has scale %d", test.scale()));
}
if (test.precision() - test.scale() > 29) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.OUT_OF_RANGE,
String.format(
"Max precision for the whole component of a numeric is 29. The requested numeric has a whole component with precision %d",
test.precision() - test.scale()));
}
}
return new NumericImpl(v == null, v);
}
/**
* Returns a {@code STRING} value.
*
* @param v the value, which may be null
*/
public static Value string(@Nullable String v) {
return new StringImpl(v == null, v);
}
/**
* Returns a {@code BYTES} value.
*
* @param v the value, which may be null
*/
public static Value bytes(@Nullable ByteArray v) {
return new BytesImpl(v == null, v);
}
/** Returns a {@code TIMESTAMP} value. */
public static Value timestamp(@Nullable Timestamp v) {
return new TimestampImpl(v == null, v == Value.COMMIT_TIMESTAMP, v);
}
/**
* Returns a {@code DATE} value. The range [1678-01-01, 2262-01-01) is the legal interval for
* cloud spanner dates. A write to a date column is rejected if the value is outside of that
* interval.
*/
public static Value date(@Nullable Date v) {
return new DateImpl(v == null, v);
}
/** Returns a non-{@code NULL} {#code STRUCT} value. */
public static Value struct(Struct v) {
Preconditions.checkNotNull(v, "Illegal call to create a NULL struct value.");
return new StructImpl(v);
}
/**
* Returns a {@code STRUCT} value of {@code Type} type.
*
* @param type the type of the {@code STRUCT} value
* @param v the struct {@code STRUCT} value. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. If non-{@code null}, {@link Struct#getType()} must match
* type.
*/
public static Value struct(Type type, @Nullable Struct v) {
if (v == null) {
Preconditions.checkArgument(
type.getCode() == Code.STRUCT,
"Illegal call to create a NULL struct with a non-struct type.");
return new StructImpl(type);
} else {
Preconditions.checkArgument(
type.equals(v.getType()), "Mismatch between struct value and type.");
return new StructImpl(v);
}
}
/**
* Returns an {@code ARRAY<BOOL>} value.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
*/
public static Value boolArray(@Nullable boolean[] v) {
return boolArray(v, 0, v == null ? 0 : v.length);
}
/**
* Returns an {@code ARRAY<BOOL>} value that takes its elements from a region of an array.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
* @param pos the start position of {@code v} to copy values from. Ignored if {@code v} is {@code
* null}.
* @param length the number of values to copy from {@code v}. Ignored if {@code v} is {@code
* null}.
*/
public static Value boolArray(@Nullable boolean[] v, int pos, int length) {
return boolArrayFactory.create(v, pos, length);
}
/**
* Returns an {@code ARRAY<BOOL>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value boolArray(@Nullable Iterable<Boolean> v) {
// TODO(user): Consider memory optimizing boolArray() to use BitSet instead of boolean[].
return boolArrayFactory.create(v);
}
/**
* Returns an {@code ARRAY<INT64>} value.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
*/
public static Value int64Array(@Nullable long[] v) {
return int64Array(v, 0, v == null ? 0 : v.length);
}
/**
* Returns an {@code ARRAY<INT64>} value that takes its elements from a region of an array.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
* @param pos the start position of {@code v} to copy values from. Ignored if {@code v} is {@code
* null}.
* @param length the number of values to copy from {@code v}. Ignored if {@code v} is {@code
* null}.
*/
public static Value int64Array(@Nullable long[] v, int pos, int length) {
return int64ArrayFactory.create(v, pos, length);
}
/**
* Returns an {@code ARRAY<INT64>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value int64Array(@Nullable Iterable<Long> v) {
return int64ArrayFactory.create(v);
}
/**
* Returns an {@code ARRAY<FLOAT64>} value.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
*/
public static Value float64Array(@Nullable double[] v) {
return float64Array(v, 0, v == null ? 0 : v.length);
}
/**
* Returns an {@code ARRAY<FLOAT64>} value that takes its elements from a region of an array.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
* @param pos the start position of {@code v} to copy values from. Ignored if {@code v} is {@code
* null}.
* @param length the number of values to copy from {@code v}. Ignored if {@code v} is {@code
* null}.
*/
public static Value float64Array(@Nullable double[] v, int pos, int length) {
return float64ArrayFactory.create(v, pos, length);
}
/**
* Returns an {@code ARRAY<FLOAT64>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value float64Array(@Nullable Iterable<Double> v) {
return float64ArrayFactory.create(v);
}
/**
* Returns an {@code ARRAY<NUMERIC>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value numericArray(@Nullable Iterable<BigDecimal> v) {
return new NumericArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
/**
* Returns an {@code ARRAY<STRING>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value stringArray(@Nullable Iterable<String> v) {
return new StringArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
/**
* Returns an {@code ARRAY<BYTES>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value bytesArray(@Nullable Iterable<ByteArray> v) {
return new BytesArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
/**
* Returns an {@code ARRAY<TIMESTAMP>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value timestampArray(@Nullable Iterable<Timestamp> v) {
return new TimestampArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
/**
* Returns an {@code ARRAY<DATE>} value. The range [1678-01-01, 2262-01-01) is the legal interval
* for cloud spanner dates. A write to a date column is rejected if the value is outside of that
* interval.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value dateArray(@Nullable Iterable<Date> v) {
return new DateArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
/**
* Returns an {@code ARRAY<STRUCT<...>>} value.
*
* @param elementType
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value structArray(Type elementType, @Nullable Iterable<Struct> v) {
if (v == null) {
Preconditions.checkArgument(
elementType.getCode() == Code.STRUCT,
"Illegal call to create a NULL array-of-struct with a non-struct element type.");
return new StructArrayImpl(elementType, null);
}
List<Struct> values = immutableCopyOf(v);
for (Struct value : values) {
if (value != null) {
Preconditions.checkArgument(
value.getType().equals(elementType),
"Members of v must have type %s (found %s)",
elementType,
value.getType());
}
}
return new StructArrayImpl(elementType, values);
}
private Value() {}
/** Returns the type of this value. This will return a type even if {@code isNull()} is true. */
public abstract Type getType();
/** Returns {@code true} if this instance represents a {@code NULL} value. */
public abstract boolean isNull();
/**
* Returns the value of a {@code BOOL}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract boolean getBool();
/**
* Returns the value of a {@code INT64}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract long getInt64();
/**
* Returns the value of a {@code FLOAT64}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract double getFloat64();
/**
* Returns the value of a {@code NUMERIC}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract BigDecimal getNumeric();
/**
* Returns the value of a {@code STRING}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract String getString();
/**
* Returns the value of a {@code BYTES}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract ByteArray getBytes();
/**
* Returns the value of a {@code TIMESTAMP}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type or
* {@link #isCommitTimestamp()}.
*/
public abstract Timestamp getTimestamp();
/** Returns true if this is a commit timestamp value. */
public abstract boolean isCommitTimestamp();
/**
* Returns the value of a {@code DATE}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract Date getDate();
/**
* Returns the value of a {@code STRUCT}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract Struct getStruct();
/**
* Returns the value of an {@code ARRAY<BOOL>}-typed instance. While the returned list itself will
* never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List<Boolean> getBoolArray();
/**
* Returns the value of an {@code ARRAY<INT64>}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List<Long> getInt64Array();
/**
* Returns the value of an {@code ARRAY<FLOAT64>}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List<Double> getFloat64Array();
/**
* Returns the value of an {@code ARRAY<NUMERIC>}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List<BigDecimal> getNumericArray();
/**
* Returns the value of an {@code ARRAY<STRING>}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List<String> getStringArray();
/**
* Returns the value of an {@code ARRAY<BYTES>}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List<ByteArray> getBytesArray();
/**
* Returns the value of an {@code ARRAY<TIMESTAMP>}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List<Timestamp> getTimestampArray();
/**
* Returns the value of an {@code ARRAY<DATE>}-typed instance. While the returned list itself will
* never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List<Date> getDateArray();
/**
* Returns the value of an {@code ARRAY<STRUCT<...>>}-typed instance. While the returned list
* itself will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List<Struct> getStructArray();
@Override
public String toString() {
StringBuilder b = new StringBuilder();
toString(b);
return b.toString();
}
// END OF PUBLIC API.
abstract void toString(StringBuilder b);
abstract com.google.protobuf.Value toProto();
private static <T> List<T> immutableCopyOf(Iterable<T> v) {
ArrayList<T> copy = new ArrayList<>();
Iterables.addAll(copy, v);
return Collections.unmodifiableList(copy);
}
private abstract static class PrimitiveArrayValueFactory<A, T> {
Value create(A v, int pos, int length) {
if (v == null) {
return newValue(true, null, null);
}
A copy = newArray(length);
System.arraycopy(v, pos, copy, 0, length);
return newValue(false, null, copy);
}
Value create(@Nullable Iterable<T> v) {
if (v == null) {
return newValue(true, null, null);
}
Collection<T> values;
if (v instanceof Collection) {
values = (Collection<T>) v;
} else {
// The wrapper objects already exist (or would be created in our iterator below), and we
// need to do the same amount of array buffering to come up with a backing array of the
// correct size; it is as CPU-efficient to simply copy into an ArrayList (except in the rare
// case where the underlying ArrayList buffer is exactly the size of the data - in that
// case, we incur an additional buffer copy over managing the array ourselves). Note that
// this simpler strategy does use more memory for booleans though as Boolean[] uses more
// memory than a boolean[] of the same size.
values = Lists.newArrayList(v);
}
BitSet nulls = null;
A arr = newArray(values.size());
int i = 0;
for (T element : values) {
if (element == null) {
if (nulls == null) {
nulls = new BitSet();
}
nulls.set(i);
} else {
set(arr, i, element);
}
++i;
}
return newValue(false, nulls, arr);
}
abstract A newArray(int size);
abstract void set(A arr, int i, T value);
abstract Value newValue(boolean isNull, BitSet nulls, A values);
}
private static final PrimitiveArrayValueFactory<long[], Long> int64ArrayFactory =
new PrimitiveArrayValueFactory<long[], Long>() {
@Override
long[] newArray(int size) {
return new long[size];
}
@Override
void set(long[] arr, int i, Long value) {
arr[i] = value;
}
@Override
Value newValue(boolean isNull, BitSet nulls, long[] values) {
return new Int64ArrayImpl(isNull, nulls, values);
}
};
private static final PrimitiveArrayValueFactory<double[], Double> float64ArrayFactory =
new PrimitiveArrayValueFactory<double[], Double>() {
@Override
double[] newArray(int size) {
return new double[size];
}
@Override
void set(double[] arr, int i, Double value) {
arr[i] = value;
}
@Override
Value newValue(boolean isNull, BitSet nulls, double[] values) {
return new Float64ArrayImpl(isNull, nulls, values);
}
};
private static final PrimitiveArrayValueFactory<boolean[], Boolean> boolArrayFactory =
new PrimitiveArrayValueFactory<boolean[], Boolean>() {
@Override
boolean[] newArray(int size) {
return new boolean[size];
}
@Override
void set(boolean[] arr, int i, Boolean value) {
arr[i] = value;
}
@Override
Value newValue(boolean isNull, BitSet nulls, boolean[] values) {
return new BoolArrayImpl(isNull, nulls, values);
}
};
/** Template class for {@code Value} implementations. */
private abstract static class AbstractValue extends Value {
static final com.google.protobuf.Value NULL_PROTO =
com.google.protobuf.Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
private final boolean isNull;
private final Type type;
private AbstractValue(boolean isNull, Type type) {
this.isNull = isNull;
this.type = type;
}
@Override
public Type getType() {
return type;
}
@Override
public final boolean isNull() {
return isNull;
}
@Override
public boolean isCommitTimestamp() {
return false;
}
@Override
public boolean getBool() {
throw defaultGetter(Type.bool());
}
@Override
public long getInt64() {
throw defaultGetter(Type.int64());
}
@Override
public double getFloat64() {
throw defaultGetter(Type.float64());
}
@Override
public BigDecimal getNumeric() {
throw defaultGetter(Type.numeric());
}
@Override
public String getString() {
throw defaultGetter(Type.string());
}
@Override
public ByteArray getBytes() {
throw defaultGetter(Type.bytes());
}
@Override
public Timestamp getTimestamp() {
throw defaultGetter(Type.timestamp());
}
@Override
public Date getDate() {
throw defaultGetter(Type.date());
}
@Override
public Struct getStruct() {
if (getType().getCode() != Type.Code.STRUCT) {
throw new IllegalStateException(
"Illegal call to getter of incorrect type. Expected: STRUCT<...> actual: " + getType());
}
throw new AssertionError("Should have been overridden");
}
@Override
public List<Boolean> getBoolArray() {
throw defaultGetter(Type.array(Type.bool()));
}
@Override
public List<Long> getInt64Array() {
throw defaultGetter(Type.array(Type.int64()));
}
@Override
public List<Double> getFloat64Array() {
throw defaultGetter(Type.array(Type.float64()));
}
@Override
public List<BigDecimal> getNumericArray() {
throw defaultGetter(Type.array(Type.numeric()));
}
@Override
public List<String> getStringArray() {
throw defaultGetter(Type.array(Type.string()));
}
@Override
public List<ByteArray> getBytesArray() {
throw defaultGetter(Type.array(Type.bytes()));
}
@Override
public List<Timestamp> getTimestampArray() {
throw defaultGetter(Type.array(Type.timestamp()));
}
@Override
public List<Date> getDateArray() {
throw defaultGetter(Type.array(Type.date()));
}
@Override
public List<Struct> getStructArray() {
if (getType().getCode() != Type.Code.ARRAY
|| getType().getArrayElementType().getCode() != Type.Code.STRUCT) {
throw new IllegalStateException(
"Illegal call to getter of incorrect type. Expected: ARRAY<STRUCT<...>> actual: "
+ getType());
}
throw new AssertionError("Should have been overridden");
}
@Override
final void toString(StringBuilder b) {
// TODO(user): Truncate long arrays.
if (isNull()) {
b.append(NULL_STRING);
} else {
valueToString(b);
}
}
/**
* Appends a representation of {@code this} to {@code b}. {@code this} is guaranteed to
* represent a non-null value.
*/
abstract void valueToString(StringBuilder b);
@Override
final com.google.protobuf.Value toProto() {
return isNull() ? NULL_PROTO : valueToProto();
}
/**
* Returns a proto representation of this value. {@code this} is guaranteed to represent a
* non-null value.
*/
abstract com.google.protobuf.Value valueToProto();
@Override
public final boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AbstractValue that = (AbstractValue) o;
if (!getType().equals(that.getType()) || isNull != that.isNull) {
return false;
}
return isNull || valueEquals(that);
}
/**
* Returns true if {@code v} has the same value as {@code this}. {@code v} is guaranteed to have
* the same Java class as {@code this}, and both {@code this} and {@code v} are guaranteed to
* represent a non-null values.
*/
abstract boolean valueEquals(Value v);
@Override
public final int hashCode() {
int result = Objects.hash(getType(), isNull);
if (!isNull) {
result = 31 * result + valueHash();
}
return result;
}
/**
* Returns a hash code for the underlying value. {@code this} is guaranteed to represent a
* non-null value.
*/
abstract int valueHash();
private AssertionError defaultGetter(Type expectedType) {
checkType(expectedType);
throw new AssertionError("Should have been overridden");
}
final void checkType(Type expected) {
if (!getType().equals(expected)) {
throw new IllegalStateException(
"Illegal call to getter of incorrect type. Expected: "
+ expected
+ " actual: "
+ getType());
}
}
final void checkNotNull() {
Preconditions.checkState(!isNull(), "Illegal call to getter of null value.");
}
}
private static class BoolImpl extends AbstractValue {
private final boolean value;
private BoolImpl(boolean isNull, boolean value) {
super(isNull, Type.bool());
this.value = value;
}
@Override
public boolean getBool() {
checkType(Type.bool());
checkNotNull();
return value;
}
@Override
com.google.protobuf.Value valueToProto() {
return com.google.protobuf.Value.newBuilder().setBoolValue(value).build();
}
@Override
void valueToString(StringBuilder b) {
b.append(value);
}
@Override
boolean valueEquals(Value v) {
return ((BoolImpl) v).value == value;
}
@Override
int valueHash() {
return Boolean.valueOf(value).hashCode();
}
}
private static class Int64Impl extends AbstractValue {
private final long value;
private Int64Impl(boolean isNull, long value) {
super(isNull, Type.int64());
this.value = value;
}
@Override
public long getInt64() {
checkType(Type.int64());
checkNotNull();
return value;
}
@Override
com.google.protobuf.Value valueToProto() {
return com.google.protobuf.Value.newBuilder().setStringValue(Long.toString(value)).build();
}
@Override
void valueToString(StringBuilder b) {
b.append(value);
}
@Override
boolean valueEquals(Value v) {
return ((Int64Impl) v).value == value;
}
@Override
int valueHash() {
return Long.valueOf(value).hashCode();
}
}
private static class Float64Impl extends AbstractValue {
private final double value;
private Float64Impl(boolean isNull, double value) {
super(isNull, Type.float64());
this.value = value;
}
@Override
public double getFloat64() {
checkType(Type.float64());
checkNotNull();
return value;
}
@Override
com.google.protobuf.Value valueToProto() {
return com.google.protobuf.Value.newBuilder().setNumberValue(value).build();
}
@Override
void valueToString(StringBuilder b) {
b.append(value);
}
@Override
boolean valueEquals(Value v) {
return ((Float64Impl) v).value == value;
}
@Override
int valueHash() {
return Double.valueOf(value).hashCode();
}
}
abstract static class AbstractObjectValue<T> extends AbstractValue {
final T value;
private AbstractObjectValue(boolean isNull, Type type, T value) {
super(isNull, type);
this.value = value;
}
@Override