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.base.MoreObjects;
14 import com.google.common.io.ByteStreams;
15 import java.io.DataInput;
16 import java.io.Externalizable;
17 import java.io.IOException;
18 import java.io.ObjectInput;
19 import java.io.ObjectOutput;
20 import java.io.Serializable;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.IdentifiablePayload;
23 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
24 import org.opendaylight.yangtools.concepts.Identifiable;
25 import org.opendaylight.yangtools.concepts.Identifier;
28 * Abstract base class for {@link Payload}s which hold a single {@link Identifier}.
30 * @author Robert Varga
32 public abstract class AbstractIdentifiablePayload<T extends Identifier> extends IdentifiablePayload<T>
33 implements Serializable {
34 protected abstract static class AbstractProxy<T extends Identifier> implements Externalizable {
35 private static final long serialVersionUID = 1L;
37 private byte[] serialized;
40 public AbstractProxy() {
44 protected AbstractProxy(final byte[] serialized) {
45 this.serialized = requireNonNull(serialized);
49 public final void writeExternal(final ObjectOutput out) throws IOException {
50 out.writeInt(serialized.length);
51 out.write(serialized);
55 public final void readExternal(final ObjectInput in) throws IOException {
56 final int length = in.readInt();
57 serialized = new byte[length];
58 in.readFully(serialized);
59 identifier = verifyNotNull(readIdentifier(ByteStreams.newDataInput(serialized)));
62 protected final Object readResolve() {
63 return verifyNotNull(createObject(identifier, serialized));
66 protected abstract @NonNull T readIdentifier(@NonNull DataInput in) throws IOException;
68 @SuppressWarnings("checkstyle:hiddenField")
69 protected abstract @NonNull Identifiable<T> createObject(@NonNull T identifier, byte @NonNull[] serialized);
72 private static final long serialVersionUID = 1L;
74 private final byte @NonNull [] serialized;
75 private final @NonNull T identifier;
77 AbstractIdentifiablePayload(final @NonNull T identifier, final byte @NonNull[] serialized) {
78 this.identifier = requireNonNull(identifier);
79 this.serialized = requireNonNull(serialized);
83 public final T getIdentifier() {
88 public final int size() {
89 return serialized.length;
93 public final String toString() {
94 return MoreObjects.toStringHelper(this).add("identifier", identifier).add("size", size()).toString();
97 protected final Object writeReplace() {
98 return verifyNotNull(externalizableProxy(serialized));
101 @SuppressWarnings("checkstyle:hiddenField")
102 protected abstract @NonNull AbstractProxy<T> externalizableProxy(byte @NonNull[] serialized);