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 com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.io.ByteStreams;
13 import java.io.DataInput;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import java.io.Serializable;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
21 import org.opendaylight.yangtools.concepts.Identifiable;
22 import org.opendaylight.yangtools.concepts.Identifier;
25 * Abstract base class for {@link Payload}s which hold a single {@link Identifier}.
27 * @author Robert Varga
29 public abstract class AbstractIdentifiablePayload<T extends Identifier>
30 extends Payload implements Identifiable<T>, Serializable {
31 protected abstract static class AbstractProxy<T extends Identifier> implements Externalizable {
32 private static final long serialVersionUID = 1L;
33 private byte[] serialized;
36 public AbstractProxy() {
40 protected AbstractProxy(final byte[] serialized) {
41 this.serialized = Preconditions.checkNotNull(serialized);
45 public final void writeExternal(final ObjectOutput out) throws IOException {
46 out.writeInt(serialized.length);
47 out.write(serialized);
51 public final void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
52 final int length = in.readInt();
53 serialized = new byte[length];
54 in.readFully(serialized);
55 identifier = Verify.verifyNotNull(readIdentifier(ByteStreams.newDataInput(serialized)));
58 protected final Object readResolve() {
59 return Verify.verifyNotNull(createObject(identifier, serialized));
63 protected abstract T readIdentifier(@Nonnull DataInput in) throws IOException;
66 @SuppressWarnings("checkstyle:hiddenField")
67 protected abstract Identifiable<T> createObject(@Nonnull T identifier, @Nonnull byte[] serialized);
70 private static final long serialVersionUID = 1L;
71 private final byte[] serialized;
72 private final T identifier;
74 AbstractIdentifiablePayload(@Nonnull final T identifier, @Nonnull final byte[] serialized) {
75 this.identifier = Preconditions.checkNotNull(identifier);
76 this.serialized = Preconditions.checkNotNull(serialized);
80 public final T getIdentifier() {
85 public final int size() {
86 return serialized.length;
89 protected final Object writeReplace() {
90 return Verify.verifyNotNull(externalizableProxy(serialized));
94 @SuppressWarnings("checkstyle:hiddenField")
95 protected abstract AbstractProxy<T> externalizableProxy(@Nonnull byte[] serialized);