2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.persisted;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.io.ByteStreams;
14 import java.io.DataInput;
15 import java.io.Externalizable;
16 import java.io.IOException;
17 import java.io.ObjectInput;
18 import java.io.ObjectOutput;
19 import java.io.Serializable;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.IdentifiablePayload;
22 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
23 import org.opendaylight.yangtools.concepts.Identifiable;
24 import org.opendaylight.yangtools.concepts.Identifier;
27 * Abstract base class for {@link Payload}s which hold a single {@link Identifier}.
29 * @author Robert Varga
31 public abstract class AbstractIdentifiablePayload<T extends Identifier> extends IdentifiablePayload<T>
32 implements Serializable {
33 protected abstract static class AbstractProxy<T extends Identifier> implements Externalizable {
34 private static final long serialVersionUID = 1L;
36 private byte[] serialized;
39 public AbstractProxy() {
43 protected AbstractProxy(final byte[] serialized) {
44 this.serialized = requireNonNull(serialized);
48 public final void writeExternal(final ObjectOutput out) throws IOException {
49 out.writeInt(serialized.length);
50 out.write(serialized);
54 public final void readExternal(final ObjectInput in) throws IOException {
55 final int length = in.readInt();
56 serialized = new byte[length];
57 in.readFully(serialized);
58 identifier = verifyNotNull(readIdentifier(ByteStreams.newDataInput(serialized)));
61 protected final Object readResolve() {
62 return verifyNotNull(createObject(identifier, serialized));
65 protected abstract @NonNull T readIdentifier(@NonNull DataInput in) throws IOException;
67 @SuppressWarnings("checkstyle:hiddenField")
68 protected abstract @NonNull Identifiable<T> createObject(@NonNull T identifier, byte @NonNull[] serialized);
71 private static final long serialVersionUID = 1L;
73 private final byte @NonNull [] serialized;
74 private final @NonNull T identifier;
76 AbstractIdentifiablePayload(final @NonNull T identifier, final byte @NonNull[] serialized) {
77 this.identifier = requireNonNull(identifier);
78 this.serialized = requireNonNull(serialized);
82 public final T getIdentifier() {
87 public final int size() {
88 return serialized.length;
91 protected final Object writeReplace() {
92 return verifyNotNull(externalizableProxy(serialized));
95 @SuppressWarnings("checkstyle:hiddenField")
96 protected abstract @NonNull AbstractProxy<T> externalizableProxy(byte @NonNull[] serialized);