blob: bb03b947dfcd96740a299426224028837fccf1a4 [file] [log] [blame]
Matthias Andreas Benkard31c61e72021-12-16 20:06:39 +01001package eu.mulk.jgvariant.core;
2
3import java.nio.ByteBuffer;
4import java.nio.charset.StandardCharsets;
5import java.text.ParseException;
6import java.util.ArrayList;
7import java.util.List;
8import java.util.Objects;
9
10/**
11 * A GVariant signature string.
12 *
13 * <p>Describes a type in the GVariant type system. The type can be arbitrarily complex.
14 *
15 * <p><strong>Examples</strong>
16 *
17 * <dl>
18 * <dt>{@code "i"}
19 * <dd>a single 32-bit integer
20 * <dt>{@code "ai"}
21 * <dd>an array of 32-bit integers
22 * <dt>{@code "(bbb(sai))"}
23 * <dd>a record consisting of three booleans and a nested record, which consists of a string and
24 * an array of 32-bit integers
25 * </dl>
26 */
27public final class Signature {
28
29 private final String signatureString;
30 private final Decoder<?> decoder;
31
32 Signature(ByteBuffer signatureBytes) throws ParseException {
33 this.decoder = parseSignature(signatureBytes);
34
35 signatureBytes.rewind();
36 this.signatureString = StandardCharsets.US_ASCII.decode(signatureBytes).toString();
37 }
38
39 static Signature parse(ByteBuffer signatureBytes) throws ParseException {
40 return new Signature(signatureBytes);
41 }
42
43 static Signature parse(String signatureString) throws ParseException {
44 var signatureBytes = ByteBuffer.wrap(signatureString.getBytes(StandardCharsets.US_ASCII));
45 return parse(signatureBytes);
46 }
47
48 /**
49 * Returns a {@link Decoder} that can decode values conforming to this signature.
50 *
51 * @return a {@link Decoder} for this signature
52 */
53 @SuppressWarnings("unchecked")
54 Decoder<Object> decoder() {
55 return (Decoder<Object>) decoder;
56 }
57
58 /**
59 * Returns the signature formatted as a GVariant signature string.
60 *
61 * @return a GVariant signature string.
62 */
63 @Override
64 public String toString() {
65 return signatureString;
66 }
67
68 @Override
69 public boolean equals(Object o) {
70 return (o instanceof Signature signature)
71 && Objects.equals(signatureString, signature.signatureString);
72 }
73
74 @Override
75 public int hashCode() {
76 return Objects.hash(signatureString);
77 }
78
79 private static Decoder<?> parseSignature(ByteBuffer signature) throws ParseException {
80 char c = (char) signature.get();
81 return switch (c) {
82 case 'b' -> Decoder.ofBoolean();
83 case 'y' -> Decoder.ofByte();
84 case 'n', 'q' -> Decoder.ofShort();
85 case 'i', 'u' -> Decoder.ofInt();
86 case 'x', 't' -> Decoder.ofLong();
87 case 'd' -> Decoder.ofDouble();
88 case 's', 'o', 'g' -> Decoder.ofString(StandardCharsets.UTF_8);
89 case 'v' -> Decoder.ofVariant();
90 case 'm' -> Decoder.ofMaybe(parseSignature(signature));
91 case 'a' -> Decoder.ofArray(parseSignature(signature));
92 case '(', '{' -> Decoder.ofStructure(parseTupleTypes(signature).toArray(new Decoder<?>[0]));
93 default -> throw new ParseException(
94 String.format("encountered unknown signature byte '%c'", c), signature.position());
95 };
96 }
97
98 private static List<Decoder<?>> parseTupleTypes(ByteBuffer signature) throws ParseException {
99 List<Decoder<?>> decoders = new ArrayList<>();
100
101 while (true) {
102 char c = (char) signature.get(signature.position());
103 if (c == ')' || c == '}') {
104 signature.get();
105 break;
106 }
107
108 decoders.add(parseSignature(signature));
109 }
110
111 return decoders;
112 }
113}