blob: 28371fc00bbbc01f88fc658d2c0913e61ae36f9a [file] [log] [blame]
Matthias Andreas Benkard05114642021-12-29 21:51:29 +01001package eu.mulk.jgvariant.ostree;
2
3import static org.apiguardian.api.API.Status.STABLE;
4
5import org.apiguardian.api.API;
6
7/**
8 * An object type as found in an OSTree repository.
9 *
10 * <p>Each object type has its own file extension.
11 *
12 * <p>In an OSTree repository, objects are located in a subfolder of the {@code /objects} folder
13 * based on their {@link Checksum}. The schema for looking up objects is {@code
14 * /objects/{checksumHead}/{checksumRest}.{fileExtension}} where:
15 *
16 * <dl>
17 * <dt>{@code {checksumHead}}
18 * <dd>the first two characters of {@link Checksum#hex()}
19 * <dt>{@code {checksumRest}}
20 * <dd>the substring of {@link Checksum#hex()} starting from the 3rd character
21 * <dt>{@code {fileExtension}}
22 * <dd>the {@link #fileExtension()} of the object type
23 * </dl>
24 */
25@API(status = STABLE)
26public enum ObjectType {
27 FILE((byte) 1, "file"),
28 DIR_TREE((byte) 2, "dirtree"),
29 DIR_META((byte) 3, "dirmeta"),
30 COMMIT((byte) 4, "commit"),
31 TOMBSTONE_COMMIT((byte) 5, "commit-tombstone"),
32 COMMIT_META((byte) 6, "commitmeta"),
33 PAYLOAD_LINK((byte) 7, "payload-link");
34
35 private final byte byteValue;
36 private final String fileExtension;
37
38 /**
39 * The serialized byte value.
40 *
41 * @return a byte representing this value in serialized GVariant structures.
42 */
43 public byte byteValue() {
44 return byteValue;
45 }
46
47 /**
48 * The file extension carried by files of this type.
49 *
50 * @return a file extension.
51 */
52 public String fileExtension() {
53 return fileExtension;
54 }
55
56 ObjectType(byte byteValue, String fileExtension) {
57 this.byteValue = byteValue;
58 this.fileExtension = fileExtension;
59 }
60
61 /**
62 * Returns the {@link ObjectType} corresponding to a serialized GVariant value.
63 *
64 * @param byteValue a serialized value as used in GVariant.
65 * @return the {@link ObjectType} corresponding to the serialized value.
66 * @throws IllegalArgumentException if the byte value is invalid.
67 */
68 public static ObjectType valueOf(byte byteValue) {
69 return switch (byteValue) {
70 case 1 -> FILE;
71 case 2 -> DIR_TREE;
72 case 3 -> DIR_META;
73 case 4 -> COMMIT;
74 case 5 -> TOMBSTONE_COMMIT;
75 case 6 -> COMMIT_META;
76 case 7 -> PAYLOAD_LINK;
77 default -> throw new IllegalArgumentException("invalid ObjectType: %d".formatted(byteValue));
78 };
79 }
80}