blob: 705e27cb0d1e55c5d779646193642092a76f3846 [file] [log] [blame]
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +01001package eu.mulk.jgvariant.ostree;
2
3import eu.mulk.jgvariant.core.Decoder;
Matthias Andreas Benkard05114642021-12-29 21:51:29 +01004import java.nio.ByteBuffer;
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +01005
6/**
7 * A wrapper for {@link ByteString} that refers to a content-addressed object in an OSTree
8 * repository.
Matthias Andreas Benkard05114642021-12-29 21:51:29 +01009 *
10 * @param byteString the bytes that make up this {@link Checksum}.
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010011 */
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010012public record Checksum(ByteString byteString) {
13
14 private static final int SIZE = 32;
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010015
16 private static final Decoder<Checksum> DECODER = ByteString.decoder().map(Checksum::new);
17
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010018 public Checksum {
19 if (byteString.size() != SIZE) {
20 throw new IllegalArgumentException(
21 "attempted to construct Checksum of length %d (expected: %d)"
22 .formatted(byteString.size(), SIZE));
23 }
24 }
25
26 /**
27 * A decoder for a {@code byte[]} that wraps the result in a {@link Checksum}.
28 *
29 * @return a possibly shared {@link Decoder}.
30 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010031 public static Decoder<Checksum> decoder() {
32 return DECODER;
33 }
34
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010035 /**
36 * Returns an empty checksum.
37 *
38 * @return a checksum whose bits are all zero.
39 */
40 public static Checksum zero() {
41 return new Checksum(new ByteString(new byte[SIZE]));
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010042 }
43
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010044 /**
45 * Checks whether the checksum contains only zero bits.
46 *
47 * @return {@code true} if the byte string is equal to {@link #zero()}, {@code false} otherwise.
48 */
49 public boolean isEmpty() {
50 return equals(zero());
51 }
52
53 /**
54 * Converts the contained byte array into a hex string.
55 *
56 * <p>Useful for printing.
57 *
58 * @return a hex string representation of the bytes making up this checksum.
59 */
60 public String hex() {
61 return byteString.hex();
62 }
63
64 /**
65 * Parses a hex string into a {@link Checksum}.
66 *
67 * @param hex a hex string.
68 * @return a {@link Checksum} corresponding to the given hex string.
69 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010070 public static Checksum ofHex(String hex) {
71 return new Checksum(ByteString.ofHex(hex));
72 }
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010073
74 /**
75 * Reads a Checksum for a {@link ByteBuffer}.
76 *
77 * @param byteBuffer the byte buffer to read from.
78 * @return a checksum.
79 */
80 public static Checksum readFrom(ByteBuffer byteBuffer) {
81 var bytes = new byte[SIZE];
82 byteBuffer.get(bytes);
83 return new Checksum(new ByteString(bytes));
84 }
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010085}