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