Define efficient serialization proxies
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / PH.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore.persisted;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.io.ByteStreams;
14 import java.io.IOException;
15 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
16 import org.opendaylight.controller.cluster.datastore.persisted.AbstractIdentifiablePayload.SerialForm;
17
18 /**
19  * Serialization proxy for {@link PurgeLocalHistoryPayload}.
20  */
21 final class PH implements SerialForm {
22     private static final long serialVersionUID = 1L;
23
24     private LocalHistoryIdentifier identifier;
25     private byte[] bytes;
26
27     @SuppressWarnings("checkstyle:RedundantModifier")
28     public PH() {
29         // For Externalizable
30     }
31
32     PH(final byte[] bytes) {
33         this.bytes = requireNonNull(bytes);
34     }
35
36     @Override
37     public byte[] bytes() {
38         return bytes;
39     }
40
41     @Override
42     public void readExternal(final byte[] newBytes) throws IOException {
43         bytes = requireNonNull(newBytes);
44         identifier = verifyNotNull(LocalHistoryIdentifier.readFrom(ByteStreams.newDataInput(newBytes)));
45     }
46
47     @Override
48     public Object readResolve() {
49         return new PurgeLocalHistoryPayload(identifier, bytes);
50     }
51 }