@@ -43,6 +43,19 @@ static Opaque forBytes(byte[] bytes) {
43
43
return new OpaqueImpl (bytes .clone ());
44
44
}
45
45
46
+ /**
47
+ * Returns an mutable {@link Opaque} instance based on the given byte array.
48
+ * <p>
49
+ * Note that the returned {@link Opaque} is typically not suitable for <em>storing</em> in a
50
+ * {@link java.util.HashMap}, but merely for lookups. Call {@link #toImmutableOpaque()} when necessary.
51
+ *
52
+ * @param bytes The bytes.
53
+ * @return The {@link Opaque} instance.
54
+ */
55
+ static Opaque forMutableByteArray (byte [] bytes ) {
56
+ return new OpaqueMutableImpl (bytes );
57
+ }
58
+
46
59
/**
47
60
* Returns an immutable {@link Opaque} instance based on a copy of the {@code length} bytes from the given
48
61
* {@link ByteBuffer}.
@@ -161,12 +174,12 @@ default void putBytes(ByteBuffer buf) {
161
174
@ Override
162
175
boolean equals (Object o );
163
176
164
- final class OpaqueImpl implements Opaque {
165
- private final byte [] _opaque ;
177
+ class OpaqueImpl implements Opaque {
178
+ final byte [] _opaque ;
166
179
private String base64 = null ;
167
180
private int hashCode ;
168
181
169
- private OpaqueImpl (byte [] opaque ) {
182
+ OpaqueImpl (byte [] opaque ) {
170
183
_opaque = opaque ;
171
184
}
172
185
@@ -175,10 +188,14 @@ public byte[] toBytes() {
175
188
return _opaque .clone ();
176
189
}
177
190
191
+ protected String toBase64Impl () {
192
+ return Base64 .getEncoder ().withoutPadding ().encodeToString (_opaque );
193
+ }
194
+
178
195
@ Override
179
196
public String toBase64 () {
180
197
if (base64 == null ) {
181
- base64 = Base64 . getEncoder (). withoutPadding (). encodeToString ( _opaque );
198
+ base64 = toBase64Impl ( );
182
199
}
183
200
return base64 ;
184
201
}
@@ -246,6 +263,28 @@ public Opaque toImmutableOpaque() {
246
263
}
247
264
}
248
265
266
+ final class OpaqueMutableImpl extends OpaqueImpl {
267
+ protected OpaqueMutableImpl (byte [] opaque ) {
268
+ super (opaque );
269
+ }
270
+
271
+ @ Override
272
+ public Opaque toImmutableOpaque () {
273
+ return Opaque .forBytes (_opaque );
274
+ }
275
+
276
+ @ Override
277
+ public String toBase64 () {
278
+ return toBase64Impl ();
279
+ }
280
+
281
+ @ Override
282
+ public int hashCode () {
283
+ return Arrays .hashCode (_opaque );
284
+ }
285
+
286
+ }
287
+
249
288
final class OpaqueBufferImpl implements Opaque {
250
289
private final ByteBuffer buf ;
251
290
private final int index ;
0 commit comments