blob: 05e28d57f4f0a9709acebd4496bd91f43beab60d [file] [log] [blame]
Matthias Andreas Benkard35f7a202021-12-14 19:29:26 +01001package eu.mulk.jgvariant.core;
2
3import java.util.List;
4import java.util.Optional;
5
6/**
7 * A value representable by the <a href="https://docs.gtk.org/glib/struct.Variant.html">GVariant</a>
8 * serialization format, tagged with its type.
9 *
10 * <p>{@link Variant} is a sum type (sealed interface) that represents a GVariant value. Its
11 * subtypes represent the different types of values that GVariant supports.
12 *
13 * @see Decoder#ofVariant()
14 */
15public sealed interface Variant {
16
17 /**
18 * A homogeneous sequence of GVariant values.
19 *
20 * <p>Arrays of fixed width (i.e. of values of fixed size) are represented in a similar way to
21 * plain C arrays. Arrays of variable width require additional space for padding and framing.
22 *
23 * <p>Heterogeneous sequences are represented by {@code Array<Variant>}.
24 *
25 * @param <T> the type of the elements of the array.
26 * @see Decoder#ofArray
27 */
28 record Array<T>(List<T> values) implements Variant {}
29
30 /**
31 * A value that is either present or absent.
32 *
33 * @param <T> the contained type.
34 * @see Decoder#ofMaybe
35 */
36 record Maybe<T>(Optional<T> value) implements Variant {}
37
38 /**
39 * A tuple of values of fixed types.
40 *
41 * <p>GVariant structures are represented as {@link Record} types. For example, a two-element
42 * structure consisting of a string and an int can be modelled as follows:
43 *
44 * <pre>{@code
45 * record TestRecord(String s, int i) {}
46 * var testStruct = new Structure<>(new TestRecord("hello", 123);
47 * }</pre>
48 *
49 * @param <T> the {@link Record} type that represents the components of the structure.
50 * @see Decoder#ofStructure
51 */
52 record Structure<T extends Record>(T values) implements Variant {}
53
54 /**
55 * Either true or false.
56 *
57 * @see Decoder#ofBoolean()
58 */
59 record Bool(boolean value) implements Variant {}
60
61 /**
62 * A {@code byte}-sized integer.
63 *
64 * @see Decoder#ofByte()
65 */
66 record Byte(byte value) implements Variant {}
67
68 /**
69 * A {@code short}-sized integer.
70 *
71 * @see Decoder#ofShort()
72 */
73 record Short(short value) implements Variant {}
74
75 /**
76 * An {@code int}-sized integer.
77 *
78 * @see Decoder#ofInt()
79 */
80 record Int(int value) implements Variant {}
81
82 /**
83 * A {@code long}-sized integer.
84 *
85 * @see Decoder#ofLong()
86 */
87 record Long(long value) implements Variant {}
88
89 /**
90 * A double-precision floating point number.
91 *
92 * @see Decoder#ofDouble()
93 */
94 record Double(double value) implements Variant {}
95
96 /**
97 * A character string.
98 *
99 * @see Decoder#ofString
100 */
101 record String(java.lang.String value) implements Variant {}
102}