blob: ce5f9b073bcc300b8caa7e7a5261c700ade80482 [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 {
Matthias Andreas Benkardf96d0e32021-12-29 21:53:50 +010019 if (byteString.size() == 0) {
20 byteString = zero().byteString;
21 }
22
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010023 if (byteString.size() != SIZE) {
24 throw new IllegalArgumentException(
25 "attempted to construct Checksum of length %d (expected: %d)"
26 .formatted(byteString.size(), SIZE));
27 }
28 }
29
30 /**
31 * A decoder for a {@code byte[]} that wraps the result in a {@link Checksum}.
32 *
33 * @return a possibly shared {@link Decoder}.
34 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010035 public static Decoder<Checksum> decoder() {
36 return DECODER;
37 }
38
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010039 /**
40 * Returns an empty checksum.
41 *
42 * @return a checksum whose bits are all zero.
43 */
44 public static Checksum zero() {
45 return new Checksum(new ByteString(new byte[SIZE]));
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010046 }
47
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010048 /**
49 * Checks whether the checksum contains only zero bits.
50 *
51 * @return {@code true} if the byte string is equal to {@link #zero()}, {@code false} otherwise.
52 */
53 public boolean isEmpty() {
54 return equals(zero());
55 }
56
57 /**
58 * Converts the contained byte array into a hex string.
59 *
60 * <p>Useful for printing.
61 *
62 * @return a hex string representation of the bytes making up this checksum.
63 */
64 public String hex() {
65 return byteString.hex();
66 }
67
68 /**
69 * Parses a hex string into a {@link Checksum}.
70 *
71 * @param hex a hex string.
72 * @return a {@link Checksum} corresponding to the given hex string.
73 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010074 public static Checksum ofHex(String hex) {
75 return new Checksum(ByteString.ofHex(hex));
76 }
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010077
78 /**
79 * Reads a Checksum for a {@link ByteBuffer}.
80 *
81 * @param byteBuffer the byte buffer to read from.
82 * @return a checksum.
83 */
84 public static Checksum readFrom(ByteBuffer byteBuffer) {
85 var bytes = new byte[SIZE];
86 byteBuffer.get(bytes);
87 return new Checksum(new ByteString(bytes));
88 }
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010089}