Fix checkstyle reported by odlparent-3.0.0
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / AbstractIdentifiablePayload.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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 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;
23
24 /**
25  * Abstract base class for {@link Payload}s which hold a single {@link Identifier}.
26  *
27  * @author Robert Varga
28  */
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;
34         private T identifier;
35
36         public AbstractProxy() {
37             // For Externalizable
38         }
39
40         protected AbstractProxy(final byte[] serialized) {
41             this.serialized = Preconditions.checkNotNull(serialized);
42         }
43
44         @Override
45         public final void writeExternal(final ObjectOutput out) throws IOException {
46             out.writeInt(serialized.length);
47             out.write(serialized);
48         }
49
50         @Override
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)));
56         }
57
58         protected final Object readResolve() {
59             return Verify.verifyNotNull(createObject(identifier, serialized));
60         }
61
62         @Nonnull
63         protected abstract T readIdentifier(@Nonnull DataInput in) throws IOException;
64
65         @Nonnull
66         @SuppressWarnings("checkstyle:hiddenField")
67         protected abstract Identifiable<T> createObject(@Nonnull T identifier, @Nonnull byte[] serialized);
68     }
69
70     private static final long serialVersionUID = 1L;
71     private final byte[] serialized;
72     private final T identifier;
73
74     AbstractIdentifiablePayload(@Nonnull final T identifier, @Nonnull final byte[] serialized) {
75         this.identifier = Preconditions.checkNotNull(identifier);
76         this.serialized = Preconditions.checkNotNull(serialized);
77     }
78
79     @Override
80     public final T getIdentifier() {
81         return identifier;
82     }
83
84     @Override
85     public final int size() {
86         return serialized.length;
87     }
88
89     protected final Object writeReplace() {
90         return Verify.verifyNotNull(externalizableProxy(serialized));
91     }
92
93     @Nonnull
94     @SuppressWarnings("checkstyle:hiddenField")
95     protected abstract AbstractProxy<T> externalizableProxy(@Nonnull byte[] serialized);
96 }