-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathValueOut.java
More file actions
1787 lines (1640 loc) · 65.3 KB
/
Copy pathValueOut.java
File metadata and controls
1787 lines (1640 loc) · 65.3 KB
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 2013-2025 chronicle.software; SPDX-License-Identifier: Apache-2.0
*/
package net.openhft.chronicle.wire;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesMarshallable;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.bytes.WriteBytesMarshallable;
import net.openhft.chronicle.bytes.util.Compression;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.Maths;
import net.openhft.chronicle.core.io.InvalidMarshallableException;
import net.openhft.chronicle.core.pool.ClassLookup;
import net.openhft.chronicle.core.scoped.ScopedResource;
import net.openhft.chronicle.core.util.CoreDynamicEnum;
import net.openhft.chronicle.core.util.ObjectUtils;
import net.openhft.chronicle.core.values.*;
import net.openhft.chronicle.threads.NamedThreadFactory;
import net.openhft.chronicle.wire.internal.MapMarshaller;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.Serializable;
import java.lang.ref.Reference;
import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.stream.Stream;
import static net.openhft.chronicle.wire.Wires.isScalar;
/**
* Defines an interface for writing out values after writing a field.
* Implementations of this interface should provide methods to handle writing various data types.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public interface ValueOut {
/**
* Thread local {@link MapMarshaller} to support thread-safe marshalling operations.
*/
ThreadLocal<MapMarshaller> MM_TL = ThreadLocal.withInitial(MapMarshaller::new);
/**
* Defines a threshold for small messages.
*/
int SMALL_MESSAGE = 64;
/**
* Utility string of sixty-four zeros, often used when commenting binary output.
*/
String ZEROS_64 = "0000000000000000000000000000000000000000000000000000000000000000";
/**
* Utility to check whether {@code v} is a standard {@link Enum} or a {@link DynamicEnum}.
*/
@SuppressWarnings("deprecation")
static boolean isAnEnum(Object v) {
return (v instanceof Enum) || (v instanceof DynamicEnum);
}
/**
* Write a boolean value.
*
* @param flag value to write
* @return parent wire for chaining
*/
@NotNull
WireOut bool(Boolean flag);
/**
* Write a text value.
*
* @param s text to write
* @return parent wire for chaining
*/
@NotNull
WireOut text(@Nullable CharSequence s);
/**
* Write a text value. This method delegates the writing to {@link #text(CharSequence)}.
*
* @param s The String containing the text to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
default WireOut text(@Nullable String s) {
return text((CharSequence) s);
}
/**
* Write a null value.
*
* @return The WireOut instance for chained calls.
*/
@NotNull
default WireOut nu11() {
return text((CharSequence) null);
}
/**
* Write a text value that's made up of a single character.
*
* @param c The character to be written as text.
* @return The WireOut instance for chained calls.
*/
@NotNull
default WireOut text(char c) {
try (ScopedResource<StringBuilder> stlSb = Wires.acquireStringBuilderScoped()) {
return text(stlSb.get().append(c));
}
}
/**
* Alias for {@link #text(char)}.
*/
@NotNull
default WireOut character(char c) {
return text(c);
}
/**
* Write a text value based on the contents of a {@link BytesStore} object.
* The method casts the BytesStore to a CharSequence for processing.
*
* @param s The BytesStore whose contents are to be written as text.
* @return The WireOut instance for chained calls.
*/
@NotNull
default WireOut text(@Nullable BytesStore<?, ?> s) {
return text((CharSequence) s);
}
/**
* Write a signed 8-bit integer value. The provided long value is first checked
* to ensure it fits within the bounds of a signed 8-bit integer.
*
* @param value value to write
* @return parent wire for chaining
* @throws ArithmeticException if {@code x} does not fit in a byte
*/
@NotNull
default WireOut int8(long value) {
return int8(Maths.toInt8(value));
}
/**
* Write a signed 8-bit integer value.
*
* @param i8 The byte value representing the 8-bit integer to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut int8(byte i8);
/**
* Write a sequence of bytes based on the content of a {@link BytesStore} object.
*
* @param fromBytes The BytesStore containing the byte sequence to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut bytes(@Nullable BytesStore<?, ?> fromBytes);
/**
* Write a sequence of bytes from a {@link BytesStore} object as a literal value,
* if supported by the wire type. If not supported, this method defaults to {@link #bytes(BytesStore)}.
*
* @param fromBytes The BytesStore containing the byte sequence to be written as a literal.
* @return The WireOut instance for chained calls.
*/
@NotNull
default WireOut bytesLiteral(@Nullable BytesStore<?, ?> fromBytes) {
return bytes(fromBytes);
}
/**
* Write a typed sequence of bytes based on the content of a {@link BytesStore} object.
*
* @param type The string representing the type of byte sequence.
* @param fromBytes The BytesStore containing the byte sequence to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut bytes(String type, @Nullable BytesStore<?, ?> fromBytes);
/**
* Write a raw sequence of bytes. The exact behavior of this method depends on the implementation.
*
* @param value The array of bytes to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut rawBytes(byte[] value);
/**
* Write a raw text value. The exact behavior of this method depends on the implementation.
* If not supported, this method defaults to {@link #text(CharSequence)}.
*
* @param value The CharSequence representing the text to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
default WireOut rawText(CharSequence value) {
return text(value);
}
/**
* Write the length of a value if supported by the implementing class.
*
* @param remaining The length of the value to be written.
* @return A ValueOut instance for chained calls.
*/
@NotNull
ValueOut writeLength(long remaining);
/**
* Write a sequence of bytes.
*
* @param fromBytes The array of bytes to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut bytes(byte[] fromBytes);
/**
* Write a typed sequence of bytes.
*
* @param type The string representing the type of byte sequence.
* @param fromBytes The array of bytes to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut bytes(String type, byte[] fromBytes);
/**
* Write an unsigned 8-bit integer value. The provided integer value is first checked
* to ensure it fits within the bounds of an unsigned 8-bit integer.
*
* @param x The integer value to be written as an unsigned 8-bit integer.
* @return The WireOut instance for chained calls.
* @throws ArithmeticException if the supplied argument does not fit in an unsigned 8-bit integer.
*/
@NotNull
default WireOut uint8(int x) {
return uint8checked(Maths.toUInt8(x & 0xFF));
}
/**
* Write an unsigned 8-bit integer value. This method assumes the argument is within the
* correct bounds of an unsigned 8-bit integer and doesn't perform any additional checks.
*
* @param u8 The unsigned 8-bit integer value to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut uint8checked(int u8);
/**
* Write a signed 16-bit integer value. The provided long value is first checked
* to ensure it fits within the bounds of a signed 16-bit integer.
*
* @param x The long value to be written as a signed 16-bit integer.
* @return The WireOut instance for chained calls.
* @throws ArithmeticException if the supplied argument does not fit in a signed 16-bit integer.
*/
@NotNull
default WireOut int16(long x) {
return int16(Maths.toInt16(x));
}
/**
* Write a signed 16-bit integer value. This method assumes the argument is within the
* correct bounds of a signed 16-bit integer and doesn't perform any additional checks.
*
* @param i16 The signed 16-bit integer value to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut int16(short i16);
/**
* Write an unsigned 16-bit integer value. The provided long value is directly cast to an integer
* and passed to {@link #uint16checked(int)} without additional range checks.
*
* @param x The long value to be written as an unsigned 16-bit integer.
* @return The WireOut instance for chained calls.
*/
@NotNull
default WireOut uint16(long x) {
return uint16checked((int) x);
}
/**
* Write an unsigned 16-bit integer value. This method assumes the argument is within the
* correct bounds of an unsigned 16-bit integer and doesn't perform any additional checks.
*
* @param u16 The unsigned 16-bit integer value to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut uint16checked(int u16);
/**
* Write a single 16-bit Unicode codepoint as UTF-8. The exact behavior of this method depends on the implementation.
*
* @param codepoint The 16-bit Unicode codepoint to be written as UTF-8.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut utf8(int codepoint);
/**
* Write a signed 32-bit integer value. The provided long value is first checked
* to ensure it fits within the bounds of a signed 32-bit integer.
*
* @param x The long value to be written as a signed 32-bit integer.
* @return The WireOut instance for chained calls.
* @throws ArithmeticException if the supplied argument does not fit in a signed 32-bit integer.
*/
@NotNull
default WireOut int32(long x) {
return int32(Maths.toInt32(x));
}
/**
* Write a signed 32-bit integer value. This method assumes the argument is within the
* correct bounds of a signed 32-bit integer and doesn't perform any additional checks.
*
* @param i32 The signed 32-bit integer value to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut int32(int i32);
/**
* Write a signed 32-bit integer value. This overloaded method accepts a previous value, but
* currently ignores it and delegates to the simpler {@link #int32(int)} method.
*
* @param i32 The signed 32-bit integer value to be written.
* @param previous The previous value, currently not used in this method.
* @return The WireOut instance for chained calls.
*/
@NotNull
default WireOut int32(int i32, int previous) {
return int32(i32);
}
/**
* Write an unsigned 32-bit integer value. The provided long value is directly passed
* to {@link #uint32checked(long)} without additional range checks.
*
* @param x The long value to be written as an unsigned 32-bit integer.
* @return The WireOut instance for chained calls.
*/
@NotNull
default WireOut uint32(long x) {
return uint32checked(x);
}
/**
* Write an unsigned 32-bit integer value. This method assumes the argument is within the
* correct bounds of an unsigned 32-bit integer and doesn't perform any additional checks.
*
* @param u32 The unsigned 32-bit integer value to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut uint32checked(long u32);
/**
* Write a signed 64-bit integer value.
*
* @param i64 The signed 64-bit integer value to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut int64(long i64);
/**
* Write a signed 64-bit integer value. This overloaded method accepts a previous value, but
* currently ignores it and delegates to the simpler {@link #int64(long)} method.
*
* @param i64 The signed 64-bit integer value to be written.
* @param previous The previous value, currently not used in this method.
* @return The WireOut instance for chained calls.
*/
@NotNull
default WireOut int64(long i64, long previous) {
return int64(i64);
}
/**
* Write two signed 64-bit integer values bound to a TwoLongValue object.
*
* @param i64x0 The first 64-bit integer value.
* @param i64x1 The second 64-bit integer value.
* @param value The TwoLongValue object to which the values are bound.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut int128forBinding(long i64x0, long i64x1, TwoLongValue value);
/**
* Write a signed 64-bit integer value as a hexadecimal representation. The behavior
* of this method might differ based on the wire type in use.
*
* @param i64 The 64-bit integer value to be written in hexadecimal format.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut int64_0x(long i64);
/**
* Allocate space for writing an array of 64-bit integers. The exact behavior might
* vary depending on the wire type or the underlying implementation.
*
* @param capacity The desired capacity of the 64-bit integer array.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut int64array(long capacity);
/**
* Write a sequence of 64-bit integers into an array, using the provided LongArrayValues
* object as the source of the values.
*
* @param capacity The desired capacity of the 64-bit integer array.
* @param values The LongArrayValues object containing the 64-bit integers to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut int64array(long capacity, LongArrayValues values);
/**
* Write a 32-bit floating-point value.
*
* @param f The 32-bit float value to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut float32(float f);
/**
* Write a 64-bit floating-point value, also known as a double.
*
* @param d The 64-bit double value to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut float64(double d);
/**
* Write a 32-bit floating-point value. This overloaded method accepts a previous value,
* but currently ignores it and delegates to the simpler {@link #float32(float)} method.
*
* @param f The 32-bit float value to be written.
* @param previous The previous float value, currently not used in this method.
* @return The WireOut instance for chained calls.
*/
@NotNull
default WireOut float32(float f, float previous) {
return float32(f);
}
/**
* Write a 64-bit floating-point value. This overloaded method accepts a previous value,
* but currently ignores it and delegates to the simpler {@link #float64(double)} method.
*
* @param d The 64-bit double value to be written.
* @param previous The previous double value, currently not used in this method.
* @return The WireOut instance for chained calls.
*/
@NotNull
default WireOut float64(double d, double previous) {
return float64(d);
}
/**
* Write a local time value. The exact format and representation might vary depending
* on the wire type or the underlying implementation.
*
* @param localTime The LocalTime instance to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut time(LocalTime localTime);
/**
* Write a zoned date-time value, which includes information about date, time, and the
* associated time zone.
*
* @param zonedDateTime The ZonedDateTime instance to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut zonedDateTime(ZonedDateTime zonedDateTime);
/**
* Write a date value. The exact format and representation might vary depending on the
* wire type or the underlying implementation.
*
* @param localDate The LocalDate instance to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut date(LocalDate localDate);
/**
* Write a local date-time value, which represents both date and time without a time zone.
* The exact format and representation might vary depending on the wire type or the underlying implementation.
*
* @param localDateTime The LocalDateTime instance to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut dateTime(LocalDateTime localDateTime);
/**
* Write a prefix that denotes a type for the upcoming value. This is useful for
* wire formats that include type information, allowing for dynamic deserialization.
*
* @param typeName The name of the type as a CharSequence.
* @return The ValueOut instance for chained calls.
*/
@NotNull
ValueOut typePrefix(CharSequence typeName);
/**
* Write a type prefix for a specified {@link Class} object. If the class object is null,
* no action is taken; otherwise, it fetches the type name from a lookup method.
*
* @param type The Class object representing the type.
* @return The ValueOut instance for chained calls.
*/
@NotNull
default ValueOut typePrefix(Class<?> type) {
return type == null ? this : typePrefix(classLookup().nameFor(type));
}
/**
* Provides a lookup mechanism to resolve class names. The exact mechanism and source
* of class name data is determined by the implementation.
*
* @return A ClassLookup instance used for resolving class names.
*/
ClassLookup classLookup();
/**
* Write a type literal for a specified {@link Class}. If the class is null, a null value
* is written. This is used to denote type information directly in the wire format.
*
* @param type The Class object for which to write a type literal.
* @return The WireOut instance for chained calls.
*/
@NotNull
default WireOut typeLiteral(@Nullable Class<?> type) {
return type == null ? nu11()
: typeLiteral((t, b) -> b.appendUtf8(classLookup().nameFor(t)), type);
}
/**
* Write a type literal for a specified {@link Type}. If the type is null, a null value
* is written. If the type is an instance of Class, then it delegates to the corresponding
* method to write a type literal for a class.
*
* @param type The Type object for which to write a type literal.
* @return The WireOut instance for chained calls.
*/
@NotNull
default WireOut typeLiteral(@Nullable Type type) {
return type == null ? nu11()
: type instanceof Class<?>? typeLiteral((Class) type)
: typeLiteral(type.getTypeName());
}
/**
* Write a type literal value. This is useful for wire formats that require direct type
* annotations within the serialized data.
*
* @param type The type name as a CharSequence.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut typeLiteral(@Nullable CharSequence type);
/**
* Writes a type literal using the specified type translator function. This allows custom
* translations for type literals depending on the implementation.
*
* @param typeTranslator A bi-consumer function to transform the Class object into a byte representation.
* @param type The Class object to be translated.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut typeLiteral(@NotNull BiConsumer<Class, Bytes<?>> typeTranslator, @Nullable Class<?> type);
/**
* Writes a universally unique identifier (UUID) to the wire.
*
* @param uuid The UUID value to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
WireOut uuid(UUID uuid);
/**
* Writes a 32-bit integer for binding. The specifics of "for binding" may be implementation dependent.
*/
@NotNull
WireOut int32forBinding(int value);
/**
* Writes a 32-bit integer for binding with the given IntValue. This may be used for additional metadata or configuration.
*/
@NotNull
WireOut int32forBinding(int value, @NotNull IntValue intValue);
/**
* Writes a 64-bit integer for binding. The specifics of "for binding" may be implementation dependent.
*/
@NotNull
WireOut int64forBinding(long value);
/**
* Throws an unsupported operation exception by default. May be overridden by specific implementations.
*/
@NotNull
default WireOut int128forBinding(long value, long value2) {
throw new UnsupportedOperationException();
}
/**
* Writes a 64-bit integer for binding with the given LongValue. This may be used for additional metadata or configuration.
*/
@NotNull
WireOut int64forBinding(long value, @NotNull LongValue longValue);
/**
* Writes a boolean value for binding with the given BooleanValue. This may be used for additional metadata or configuration.
*/
@NotNull
WireOut boolForBinding(boolean value, @NotNull BooleanValue longValue);
/**
* Writes a sequence of values to the wire using the provided writer. The default behavior uses the writer's `writeValue` method.
*/
@NotNull
default WireOut sequence(WriteValue writer) {
return sequence(writer, WriteValue::writeValue);
}
/**
* Writes a sequence of values from an {@link Iterator}. This method handles different types of iterables,
* applying type prefixes as appropriate for Sets and SortedSets.
*
* @param t The Iterable of values to be written.
* @return The WireOut instance for chained calls.
*/
@NotNull
default <T> WireOut sequence(Iterable<T> t) {
final Class<?> typePrefix;
if (t instanceof SortedSet)
typePrefix = SortedSet.class;
else if (t instanceof Set)
typePrefix = Set.class;
else
typePrefix = null;
if (typePrefix != null)
typePrefix(typePrefix);
final WireOut result = sequence(t, (it, out) -> {
for (T o : it) {
out.object(o);
}
});
if (typePrefix != null)
endTypePrefix();
return result;
}
/**
* Writes a sequence of values using the provided writer.
*
* @param t The input to be consumed by the writer.
* @param writer A bi-consumer that writes values using the given ValueOut instance.
* @return The WireOut instance for chained calls.
*/
@NotNull <T> WireOut sequence(T t, BiConsumer<T, ValueOut> writer);
/**
* Writes a sequence of values using the provided parametrized writer.
*
* @param t The primary input to be consumed by the writer.
* @param param A secondary input parameter for the writer.
* @param writer A tri-consumer that writes values using the given ValueOut instance and additional parameter.
* @return The WireOut instance for chained calls.
* @throws InvalidMarshallableException When there's an issue marshalling the data.
*/
@NotNull <T, K> WireOut sequence(T t, K param, TriConsumer<T, K, ValueOut> writer) throws InvalidMarshallableException;
/**
* Writes a sequence of values of a specified length.
*
* @param t The input to be consumed by the writer.
* @param length The length of the sequence.
* @param writer An object-int-object consumer that writes values using the given ValueOut instance.
* @return The WireOut instance for chained calls.
*/
default <T> WireOut sequenceWithLength(T t, int length, ObjectIntObjectConsumer<T, ValueOut> writer) {
boolean b = swapLeaf(true);
WireOut sequence = sequence(t, length, writer::accept);
swapLeaf(b);
return sequence;
}
/**
* Writes an array of byte sequences of a specified length.
*
* @param array The array of byte sequences.
* @param length The length of the sequence.
* @return The WireOut instance for chained calls.
*/
default WireOut array(Bytes[] array, int length) {
return sequenceWithLength(array, length, (a, len, out) -> {
for (int i = 0; i < len; i++)
out.bytes(a[i]);
});
}
/**
* Writes an array of doubles of a specified length.
*
* @param array The array of doubles.
* @param length The length of the sequence.
* @return The WireOut instance for chained calls.
*/
default WireOut array(double[] array, int length) {
return sequenceWithLength(array, length, (a, len, out) -> {
for (int i = 0; i < len; i++)
out.float64(a[i]);
});
}
/**
* This write value's relative to the first one using 6 digit precision
*
* @param array to write
* @param length to write
* @return this
*/
default WireOut arrayDelta(double[] array, int length) {
return sequenceWithLength(array, length, (a, len, out) -> {
if (len <= 0) return;
out.float64(a[0]);
double a0 = a[0];
if (Double.isNaN(a0)) a0 = 0.0;
for (int i = 1; i < len; i++)
out.float64(Maths.round6(a[i] - a0));
});
}
/**
* Writes an array of boolean values to the wire output.
*
* @param array The array of booleans to be written.
* @param length The number of elements from the array to write.
* @return The current instance of the WireOut.
*/
default WireOut array(boolean[] array, int length) {
return sequenceWithLength(array, length, (a, len, out) -> {
for (int i = 0; i < len; i++)
out.bool(a[i]);
});
}
/**
* Writes an array of long values to the wire output.
*
* @param array The array of longs to be written.
* @param length The number of elements from the array to write.
* @return The current instance of the WireOut.
*/
default WireOut array(long[] array, int length) {
return sequenceWithLength(array, length, (a, len, out) -> {
for (int i = 0; i < len; i++)
out.int64(a[i]);
});
}
/**
* Writes the delta of long values in an array to the wire output.
* The first value is written as is, and the subsequent values are written as differences
* from the first value.
*
* @param array The array of longs whose deltas are to be written.
* @param length The number of elements from the array to write.
* @return The current instance of the WireOut.
*/
default WireOut arrayDelta(long[] array, int length) {
return sequenceWithLength(array, length, (a, len, out) -> {
if (len <= 0) return;
out.int64(a[0]);
long a0 = a[0];
for (int i = 1; i < len; i++)
out.int64(a[i] - a0);
});
}
/**
* Writes an array of int values to the wire output.
*
* @param array The array of ints to be written.
* @param length The number of elements from the array to write.
* @return The current instance of the WireOut.
*/
default WireOut array(int[] array, int length) {
return sequenceWithLength(array, length, (a, len, out) -> {
for (int i = 0; i < len; i++)
out.int32(a[i]);
});
}
/**
* Writes an array of byte values to the wire output.
*
* @param array The array of bytes to be written.
* @param length The number of elements from the array to write.
* @return The current instance of the WireOut.
*/
default WireOut array(byte[] array, int length) {
return sequenceWithLength(array, length, (a, len, out) -> {
for (int i = 0; i < len; i++)
out.int8(a[i]);
});
}
/**
* Writes an array of specified type objects to the wire output, using the provided writer.
* Recognizes arrays of type String[] and other object arrays, appending appropriate type prefixes.
*
* @param writer The writer to use for writing the array.
* @param arrayType The type of the array to be written.
* @return The current instance of the WireOut.
*/
@NotNull
default WireOut array(@NotNull WriteValue writer, @NotNull Class<?> arrayType) {
if (arrayType == String[].class) {
typePrefix("String[] ");
} else if (arrayType != Object[].class) {
typePrefix(classLookup().nameFor(arrayType.getComponentType()) + "[]");
}
return sequence(writer);
}
/**
* Writes a value that implements the {@link WriteMarshallable} interface to the wire output.
*
* @param object The WriteMarshallable object to be written.
* @return The current instance of the WireOut.
* @throws InvalidMarshallableException If the object cannot be marshalled.
*/
@NotNull
WireOut marshallable(WriteMarshallable object) throws InvalidMarshallableException;
/**
* Writes a value that implements the {@link Serializable} interface to the wire output.
*
* @param object The Serializable object to be written.
* @return The current instance of the WireOut.
* @throws InvalidMarshallableException If the object cannot be marshalled.
*/
@NotNull
WireOut marshallable(Serializable object) throws InvalidMarshallableException;
/**
* Writes the contents of a specified map to the wire output.
* The key and value type of the map must be either Marshallable, String, or Autoboxed primitives.
*
* @param map The map whose contents are to be written.
* @return The current instance of the WireOut.
* @throws InvalidMarshallableException If the map cannot be marshalled.
* @throws IllegalArgumentException If the type of the map is not one of those listed above.
*/
@NotNull
WireOut map(Map map) throws InvalidMarshallableException;
/**
* Determines if the current WireOut is in a leaf node state. This method is intended for internal use.
* The default behavior is to always return false.
*
* @param isLeaf Flag indicating whether the current node is a leaf.
* @return A boolean indicating the previous state (always false in the default implementation).
*/
default boolean swapLeaf(boolean isLeaf) {
return false;
}
/**
* To be used when you know it is a typed marshallable object. e.g. an Enum is not, it's a scalar.
* If you are not sure, use the {@link #object(Object)} method.
*
* @param marshallable to write
* @return the original wire
*/
@NotNull
default WireOut typedMarshallable(@Nullable WriteMarshallable marshallable) throws InvalidMarshallableException {
if (marshallable == null)
return nu11();
String typeName = Wires.typeNameFor(classLookup(), marshallable);
if (typeName != null)
typePrefix(typeName);
final WireOut wire = marshallable(marshallable);
if (typeName != null)
endTypePrefix();
return wire;
}
/**
* Ends the type prefix scope for the written data.
* In the default implementation, this method does nothing. Override it in subclasses if specific end logic is required.
*/
default void endTypePrefix() {
}
/**
* Writes a value that implements the {@link Serializable} interface to the wire output.
* Specifically used when the object is known to be a typed serializable. If uncertain about the object's type,
* it's recommended to use the {@link #object(Object)} method instead.
*
* @param object The Serializable object to be written.
* @return The current instance of the WireOut.
* @throws InvalidMarshallableException If the object cannot be marshalled.
*/
@NotNull
default WireOut typedMarshallable(@Nullable Serializable object) throws InvalidMarshallableException {
if (object == null)
return nu11();
try {
typePrefix(object.getClass());
if (object instanceof WriteMarshallable) {
return marshallable((WriteMarshallable) object);
} else if (object instanceof Enum) {
return asEnum((Enum) object);
} else if (isScalar(object)) {
if (object instanceof LocalDate) {
LocalDate d = (LocalDate) object;
try (ScopedResource<StringBuilder> stlSb = Wires.acquireStringBuilderScoped()) {
return text(stlSb.get()
.append(d.getYear())
.append('-')
.append(d.getMonthValue() < 10 ? "0" : "")
.append(d.getMonthValue())
.append('-')
.append(d.getDayOfMonth() < 10 ? "0" : "")
.append(d.getDayOfMonth()));
}
}
return text(object.toString());
} else if (object instanceof Locale) {
return text(((Locale) object).toLanguageTag());
} else {
return marshallable(object);
}
} finally {
// Ensure the type prefix scope is closed, even if there was an error
endTypePrefix();
}
}
/**
* Writes a value that implements the {@link WriteMarshallable} interface to the wire output,
* while prepending it with a specified type prefix.
*
* @param typeName The type prefix to be written before the object.
* @param object The WriteMarshallable object to be written.
* @return The current instance of the WireOut.
* @throws InvalidMarshallableException If the object cannot be marshalled.
*/
@NotNull
default WireOut typedMarshallable(CharSequence typeName, WriteMarshallable object) throws InvalidMarshallableException {
typePrefix(typeName);
return marshallable(object);
}