blob: 54c0b79a6c622e05d02c26dc271ac577a8ecaa7c [file] [log] [blame]
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +01001package eu.mulk.jgvariant.core;
2
3import java.util.List;
4import java.util.Optional;
5
Matthias Andreas Benkard4c32c392021-12-12 21:23:53 +01006/**
7 * A value representable by the <a href="https://docs.gtk.org/glib/struct.Variant.html">GVariant</a>
8 * serialization format.
9 *
10 * <p>{@link Value} is a sum type (sealed interface) that represents a GVariant value. Its subtypes
11 * represent the different types of values that GVariant supports.
12 *
13 * @see Decoder
14 */
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010015public sealed interface Value {
16
Matthias Andreas Benkard4c32c392021-12-12 21:23:53 +010017 /**
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 */
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010028 record Array<T extends Value>(List<T> values) implements Value {}
29
Matthias Andreas Benkard4c32c392021-12-12 21:23:53 +010030 /**
31 * A value that is either present or absent.
32 *
33 * @param <T> the contained type.
34 * @see Decoder#ofMaybe
35 */
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010036 record Maybe<T extends Value>(Optional<T> value) implements Value {}
37
Matthias Andreas Benkard4c32c392021-12-12 21:23:53 +010038 /**
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(Str s, Int32 i) {}
46 * var testStruct = new Structure<>(new TestRecord(new Str("hello"), new Int32(123));
47 * }</pre>
48 *
49 * @param <T> the {@link Record} type that represents the components of the structure.
50 * @see Decoder#ofStructure
51 */
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010052 record Structure<T extends Record>(T values) implements Value {}
53
Matthias Andreas Benkard4c32c392021-12-12 21:23:53 +010054 /**
55 * A dynamically typed box that can hold a single value of any GVariant type.
56 *
57 * @see Decoder#ofVariant
58 */
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010059 record Variant(Class<? extends Value> type, Value value) implements Value {}
60
Matthias Andreas Benkard4c32c392021-12-12 21:23:53 +010061 /**
62 * Either true or false.
63 *
64 * @see Decoder#ofBool()
65 */
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010066 record Bool(boolean value) implements Value {
67 static Bool TRUE = new Bool(true);
68 static Bool FALSE = new Bool(false);
69 }
70
Matthias Andreas Benkard4c32c392021-12-12 21:23:53 +010071 /**
72 * A {@code byte}-sized integer.
73 *
74 * @see Decoder#ofInt8()
75 */
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010076 record Int8(byte value) implements Value {}
77
Matthias Andreas Benkard4c32c392021-12-12 21:23:53 +010078 /**
79 * A {@code short}-sized integer.
80 *
81 * @see Decoder#ofInt16()
82 */
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010083 record Int16(short value) implements Value {}
84
Matthias Andreas Benkard4c32c392021-12-12 21:23:53 +010085 /**
86 * An {@code int}-sized integer.
87 *
88 * @see Decoder#ofInt32()
89 */
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010090 record Int32(int value) implements Value {}
91
Matthias Andreas Benkard4c32c392021-12-12 21:23:53 +010092 /**
93 * A {@code long}-sized integer.
94 *
95 * @see Decoder#ofInt64()
96 */
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +010097 record Int64(long value) implements Value {}
98
Matthias Andreas Benkard4c32c392021-12-12 21:23:53 +010099 /**
100 * A double-precision floating point number.
101 *
102 * @see Decoder#ofFloat64()
103 */
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100104 record Float64(double value) implements Value {}
105
Matthias Andreas Benkard4c32c392021-12-12 21:23:53 +0100106 /**
107 * A character string.
108 *
109 * @see Decoder#ofStr
110 */
Matthias Andreas Benkard261532a2021-12-12 20:09:27 +0100111 record Str(String value) implements Value {}
112}