Skip to content

Commit 334d339

Browse files
committed
core: Opaque: Make OpaqueImmutableImpl a subclass of OpaqueImpl
That way, we don't have to declare the cache attributes for hashCode and base64 when not required. Related: #149 Signed-off-by: Christian Kohlschütter <[email protected]>
1 parent e78eb15 commit 334d339

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

core/src/main/java/org/dcache/nfs/util/Opaque.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public interface Opaque {
4040
* @return The {@link Opaque} instance.
4141
*/
4242
static Opaque forBytes(byte[] bytes) {
43-
return new OpaqueImpl(bytes.clone());
43+
return new OpaqueImmutableImpl(bytes.clone());
4444
}
4545

4646
/**
@@ -53,7 +53,7 @@ static Opaque forBytes(byte[] bytes) {
5353
* @return The {@link Opaque} instance.
5454
*/
5555
static Opaque forMutableByteArray(byte[] bytes) {
56-
return new OpaqueMutableImpl(bytes);
56+
return new OpaqueImpl(bytes);
5757
}
5858

5959
/**
@@ -68,7 +68,7 @@ static Opaque forBytes(ByteBuffer buf, int length) {
6868
byte[] bytes = new byte[length];
6969
buf.get(bytes);
7070

71-
return new OpaqueImpl(bytes);
71+
return new OpaqueImmutableImpl(bytes);
7272
}
7373

7474
/**
@@ -176,8 +176,6 @@ default void putBytes(ByteBuffer buf) {
176176

177177
class OpaqueImpl implements Opaque {
178178
final byte[] _opaque;
179-
private String base64 = null;
180-
private int hashCode;
181179

182180
OpaqueImpl(byte[] opaque) {
183181
_opaque = opaque;
@@ -188,29 +186,23 @@ public byte[] toBytes() {
188186
return _opaque.clone();
189187
}
190188

191-
protected String toBase64Impl() {
192-
return Base64.getEncoder().withoutPadding().encodeToString(_opaque);
189+
@Override
190+
public int hashCode() {
191+
return Arrays.hashCode(_opaque);
193192
}
194193

195194
@Override
196195
public String toBase64() {
197-
if (base64 == null) {
198-
base64 = toBase64Impl();
199-
}
200-
return base64;
196+
return toBase64Impl();
201197
}
202198

203-
@Override
204-
public void putBytes(ByteBuffer buf) {
205-
buf.put(_opaque);
199+
protected String toBase64Impl() {
200+
return Base64.getEncoder().withoutPadding().encodeToString(_opaque);
206201
}
207202

208203
@Override
209-
public int hashCode() {
210-
if (hashCode == 0) {
211-
hashCode = Arrays.hashCode(_opaque);
212-
}
213-
return hashCode;
204+
public void putBytes(ByteBuffer buf) {
205+
buf.put(_opaque);
214206
}
215207

216208
@Override
@@ -259,30 +251,38 @@ public int numBytes() {
259251

260252
@Override
261253
public Opaque toImmutableOpaque() {
262-
return this;
254+
return Opaque.forBytes(_opaque);
263255
}
264256
}
265257

266-
final class OpaqueMutableImpl extends OpaqueImpl {
267-
protected OpaqueMutableImpl(byte[] opaque) {
258+
final class OpaqueImmutableImpl extends OpaqueImpl {
259+
private String base64 = null;
260+
private int hashCode;
261+
262+
protected OpaqueImmutableImpl(byte[] opaque) {
268263
super(opaque);
269264
}
270265

271266
@Override
272-
public Opaque toImmutableOpaque() {
273-
return Opaque.forBytes(_opaque);
267+
public int hashCode() {
268+
if (hashCode == 0) {
269+
hashCode = Arrays.hashCode(_opaque);
270+
}
271+
return hashCode;
274272
}
275273

276274
@Override
277275
public String toBase64() {
278-
return toBase64Impl();
276+
if (base64 == null) {
277+
base64 = toBase64Impl();
278+
}
279+
return base64;
279280
}
280281

281282
@Override
282-
public int hashCode() {
283-
return Arrays.hashCode(_opaque);
283+
public Opaque toImmutableOpaque() {
284+
return this;
284285
}
285-
286286
}
287287

288288
final class OpaqueBufferImpl implements Opaque {

0 commit comments

Comments
 (0)