Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 1 | package eu.mulk.jgvariant.ostree; |
| 2 | |
| 3 | import eu.mulk.jgvariant.core.Decoder; |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame^] | 4 | import java.nio.ByteBuffer; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 5 | |
| 6 | /** |
| 7 | * A wrapper for {@link ByteString} that refers to a content-addressed object in an OSTree |
| 8 | * repository. |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame^] | 9 | * |
| 10 | * @param byteString the bytes that make up this {@link Checksum}. |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 11 | */ |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame^] | 12 | public record Checksum(ByteString byteString) { |
| 13 | |
| 14 | private static final int SIZE = 32; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 15 | |
| 16 | private static final Decoder<Checksum> DECODER = ByteString.decoder().map(Checksum::new); |
| 17 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame^] | 18 | 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 Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 31 | public static Decoder<Checksum> decoder() { |
| 32 | return DECODER; |
| 33 | } |
| 34 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame^] | 35 | /** |
| 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 Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 42 | } |
| 43 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame^] | 44 | /** |
| 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 Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 70 | public static Checksum ofHex(String hex) { |
| 71 | return new Checksum(ByteString.ofHex(hex)); |
| 72 | } |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame^] | 73 | |
| 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 Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 85 | } |