77import com .apicatalog .uvarint .UVarInt ;
88
99/**
10- * Represents a multicodec definition and encoder/decoder instance.
10+ * Represents a
11+ * <a href="https://github.com/multiformats/multicodec">multicodec</a>
12+ * definition and provides encoding/decoding operations using its varint code.
13+ *
14+ * <p>
15+ * A multicodec is a self-describing format where the encoded data is prefixed
16+ * with a varint code identifying the format. This class stores metadata about
17+ * the codec, including its name, tag, status, and binary varint code, and can
18+ * be used to:
19+ * <ul>
20+ * <li>Encode raw byte arrays with the codec prefix</li>
21+ * <li>Check if a value is encoded with this codec</li>
22+ * <li>Decode a previously encoded value</li>
23+ * </ul>
24+ *
25+ * <p>
26+ * Instances of this class are immutable and thread-safe.
1127 */
1228public class Multicodec {
1329
1430 /**
15- * Recognized multicodec tags
31+ * Categories of recognized multicodec tags.
1632 */
1733 public enum Tag {
1834 Key ,
@@ -28,7 +44,7 @@ public enum Tag {
2844 }
2945
3046 /**
31- * Common registration status
47+ * Registration status of the codec.
3248 */
3349 public enum Status {
3450 Deprecated ,
@@ -42,6 +58,15 @@ public enum Status {
4258 protected final Tag tag ;
4359 protected final Status status ;
4460
61+ /**
62+ * Constructs a new {@code Multicodec}.
63+ *
64+ * @param name the codec name
65+ * @param tag the codec category tag
66+ * @param code the codec numeric code
67+ * @param uvarint the varint-encoded form of the code
68+ * @param status the registration status
69+ */
4570 protected Multicodec (String name , Tag tag , long code , byte [] uvarint , Status status ) {
4671 this .tag = tag ;
4772 this .name = name ;
@@ -50,45 +75,38 @@ protected Multicodec(String name, Tag tag, long code, byte[] uvarint, Status sta
5075 this .status = status ;
5176 }
5277
78+ /**
79+ * Creates a new {@code Multicodec} with no explicit status.
80+ *
81+ * @param name the codec name
82+ * @param tag the codec category tag
83+ * @param code the codec numeric code
84+ * @return a new {@code Multicodec} instance
85+ */
5386 public static Multicodec of (String name , Tag tag , long code ) {
5487 return of (name , tag , code , null );
5588 }
5689
90+ /**
91+ * Creates a new {@code Multicodec}.
92+ *
93+ * @param name the codec name
94+ * @param tag the codec category tag
95+ * @param code the codec numeric code
96+ * @param status the codec registration status
97+ * @return a new {@code Multicodec} instance
98+ */
5799 public static Multicodec of (String name , Tag tag , long code , Status status ) {
58100 return new Multicodec (name , tag , code , UVarInt .encode (code ), status );
59101 }
60102
61- public int length () {
62- return codeVarint .length ;
63- }
64-
65- public byte [] varint () {
66- return codeVarint ;
67- }
68-
69- public Tag tag () {
70- return tag ;
71- }
72-
73- public String name () {
74- return name ;
75- }
76-
77- public long code () {
78- return code ;
79- }
80-
81- public Status status () {
82- return status ;
83- }
84-
85103 /**
86- * Encode a value with a codec.
87- *
88- * @param value a value to encode
89- * @return an encoded value
104+ * Encodes the given value by prefixing it with this codec's varint code.
90105 *
91- * @throws IllegalArgumentException if the value cannot be encoded
106+ * @param value the non-empty byte array to encode
107+ * @return the encoded byte array (varint prefix + original value)
108+ * @throws NullPointerException if {@code value} is {@code null}
109+ * @throws IllegalArgumentException if {@code value} is empty
92110 */
93111 public byte [] encode (final byte [] value ) {
94112
@@ -107,11 +125,11 @@ public byte[] encode(final byte[] value) {
107125 }
108126
109127 /**
110- * Checks if the given value is encoded with the codec.
128+ * Checks if the given encoded byte array starts with this codec's varint code .
111129 *
112- * @param encoded an encoded value to test
113- * @return < code> true</code> is the given value is encoded with the codec,
114- * < code> false</code> otherwise
130+ * @param encoded the byte array to test
131+ * @return {@ code true} if {@code encoded} starts with this codec's varint
132+ * prefix, {@ code false} otherwise
115133 */
116134 public boolean isEncoded (final byte [] encoded ) {
117135 return encoded != null
@@ -120,25 +138,28 @@ public boolean isEncoded(final byte[] encoded) {
120138 }
121139
122140 /**
123- * Decode an encoded value
124- *
125- * @param encoded value to decode
126- * @return a decoded value
141+ * Decodes an encoded value by removing this codec's varint prefix.
127142 *
128- * @throws IllegalArgumentException if the encoded value cannot be decoded
143+ * @param encoded the encoded value
144+ * @return the decoded (original) byte array
145+ * @throws NullPointerException if {@code encoded} is {@code null}
146+ * @throws IllegalArgumentException if {@code encoded} is too short or does not
147+ * begin with this codec's varint prefix
129148 */
130149 public byte [] decode (final byte [] encoded ) {
131150 return decode (encoded , 0 );
132151 }
133-
152+
134153 /**
135- * Decode an encoded value
136- *
137- * @param encoded value to decode
138- * @param index an index from which to start (included)
139- * @return a decoded value
154+ * Decodes an encoded value starting from a given index.
140155 *
141- * @throws IllegalArgumentException if the encoded value cannot be decoded
156+ * @param encoded the encoded value
157+ * @param index the starting index (inclusive)
158+ * @return the decoded (original) byte array
159+ * @throws NullPointerException if {@code encoded} is {@code null}
160+ * @throws IllegalArgumentException if the value is too short from {@code index}
161+ * onward or does not begin with this codec's
162+ * varint prefix
142163 */
143164 public byte [] decode (final byte [] encoded , final int index ) {
144165
@@ -176,4 +197,58 @@ public boolean equals(Object obj) {
176197 public String toString () {
177198 return "Multicodec [name=" + name + ", tag=" + tag + ", code=" + code + "]" ;
178199 }
200+
201+ /**
202+ * Returns the length of this codec's varint code in bytes.
203+ *
204+ * @return varint length
205+ */
206+ public int length () {
207+ return codeVarint .length ;
208+ }
209+
210+ /**
211+ * Returns the varint-encoded form of this codec's code.
212+ *
213+ * @return varint byte array
214+ */
215+ public byte [] varint () {
216+ return codeVarint ;
217+ }
218+
219+ /**
220+ * Returns this codec's category tag.
221+ *
222+ * @return the tag
223+ */
224+ public Tag tag () {
225+ return tag ;
226+ }
227+
228+ /**
229+ * Returns this codec's name.
230+ *
231+ * @return the name
232+ */
233+ public String name () {
234+ return name ;
235+ }
236+
237+ /**
238+ * Returns this codec's numeric code.
239+ *
240+ * @return the numeric code
241+ */
242+ public long code () {
243+ return code ;
244+ }
245+
246+ /**
247+ * Returns this codec's registration status.
248+ *
249+ * @return the status, or {@code null} if unspecified
250+ */
251+ public Status status () {
252+ return status ;
253+ }
179254}
0 commit comments