Matthias Andreas Benkard | 261532a | 2021-12-12 20:09:27 +0100 | [diff] [blame] | 1 | package eu.mulk.jgvariant.core; |
| 2 | |
| 3 | import java.util.List; |
| 4 | import java.util.Optional; |
| 5 | |
Matthias Andreas Benkard | 4c32c39 | 2021-12-12 21:23:53 +0100 | [diff] [blame^] | 6 | /** |
| 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 Benkard | 261532a | 2021-12-12 20:09:27 +0100 | [diff] [blame] | 15 | public sealed interface Value { |
| 16 | |
Matthias Andreas Benkard | 4c32c39 | 2021-12-12 21:23:53 +0100 | [diff] [blame^] | 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 | */ |
Matthias Andreas Benkard | 261532a | 2021-12-12 20:09:27 +0100 | [diff] [blame] | 28 | record Array<T extends Value>(List<T> values) implements Value {} |
| 29 | |
Matthias Andreas Benkard | 4c32c39 | 2021-12-12 21:23:53 +0100 | [diff] [blame^] | 30 | /** |
| 31 | * A value that is either present or absent. |
| 32 | * |
| 33 | * @param <T> the contained type. |
| 34 | * @see Decoder#ofMaybe |
| 35 | */ |
Matthias Andreas Benkard | 261532a | 2021-12-12 20:09:27 +0100 | [diff] [blame] | 36 | record Maybe<T extends Value>(Optional<T> value) implements Value {} |
| 37 | |
Matthias Andreas Benkard | 4c32c39 | 2021-12-12 21:23:53 +0100 | [diff] [blame^] | 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(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 Benkard | 261532a | 2021-12-12 20:09:27 +0100 | [diff] [blame] | 52 | record Structure<T extends Record>(T values) implements Value {} |
| 53 | |
Matthias Andreas Benkard | 4c32c39 | 2021-12-12 21:23:53 +0100 | [diff] [blame^] | 54 | /** |
| 55 | * A dynamically typed box that can hold a single value of any GVariant type. |
| 56 | * |
| 57 | * @see Decoder#ofVariant |
| 58 | */ |
Matthias Andreas Benkard | 261532a | 2021-12-12 20:09:27 +0100 | [diff] [blame] | 59 | record Variant(Class<? extends Value> type, Value value) implements Value {} |
| 60 | |
Matthias Andreas Benkard | 4c32c39 | 2021-12-12 21:23:53 +0100 | [diff] [blame^] | 61 | /** |
| 62 | * Either true or false. |
| 63 | * |
| 64 | * @see Decoder#ofBool() |
| 65 | */ |
Matthias Andreas Benkard | 261532a | 2021-12-12 20:09:27 +0100 | [diff] [blame] | 66 | record Bool(boolean value) implements Value { |
| 67 | static Bool TRUE = new Bool(true); |
| 68 | static Bool FALSE = new Bool(false); |
| 69 | } |
| 70 | |
Matthias Andreas Benkard | 4c32c39 | 2021-12-12 21:23:53 +0100 | [diff] [blame^] | 71 | /** |
| 72 | * A {@code byte}-sized integer. |
| 73 | * |
| 74 | * @see Decoder#ofInt8() |
| 75 | */ |
Matthias Andreas Benkard | 261532a | 2021-12-12 20:09:27 +0100 | [diff] [blame] | 76 | record Int8(byte value) implements Value {} |
| 77 | |
Matthias Andreas Benkard | 4c32c39 | 2021-12-12 21:23:53 +0100 | [diff] [blame^] | 78 | /** |
| 79 | * A {@code short}-sized integer. |
| 80 | * |
| 81 | * @see Decoder#ofInt16() |
| 82 | */ |
Matthias Andreas Benkard | 261532a | 2021-12-12 20:09:27 +0100 | [diff] [blame] | 83 | record Int16(short value) implements Value {} |
| 84 | |
Matthias Andreas Benkard | 4c32c39 | 2021-12-12 21:23:53 +0100 | [diff] [blame^] | 85 | /** |
| 86 | * An {@code int}-sized integer. |
| 87 | * |
| 88 | * @see Decoder#ofInt32() |
| 89 | */ |
Matthias Andreas Benkard | 261532a | 2021-12-12 20:09:27 +0100 | [diff] [blame] | 90 | record Int32(int value) implements Value {} |
| 91 | |
Matthias Andreas Benkard | 4c32c39 | 2021-12-12 21:23:53 +0100 | [diff] [blame^] | 92 | /** |
| 93 | * A {@code long}-sized integer. |
| 94 | * |
| 95 | * @see Decoder#ofInt64() |
| 96 | */ |
Matthias Andreas Benkard | 261532a | 2021-12-12 20:09:27 +0100 | [diff] [blame] | 97 | record Int64(long value) implements Value {} |
| 98 | |
Matthias Andreas Benkard | 4c32c39 | 2021-12-12 21:23:53 +0100 | [diff] [blame^] | 99 | /** |
| 100 | * A double-precision floating point number. |
| 101 | * |
| 102 | * @see Decoder#ofFloat64() |
| 103 | */ |
Matthias Andreas Benkard | 261532a | 2021-12-12 20:09:27 +0100 | [diff] [blame] | 104 | record Float64(double value) implements Value {} |
| 105 | |
Matthias Andreas Benkard | 4c32c39 | 2021-12-12 21:23:53 +0100 | [diff] [blame^] | 106 | /** |
| 107 | * A character string. |
| 108 | * |
| 109 | * @see Decoder#ofStr |
| 110 | */ |
Matthias Andreas Benkard | 261532a | 2021-12-12 20:09:27 +0100 | [diff] [blame] | 111 | record Str(String value) implements Value {} |
| 112 | } |