Fixup checkstyle
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / ShardSnapshotState.java
1 /*
2  * Copyright (c) 2017 Brocade Communications 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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
20
21 /**
22  * Encapsulates the snapshot State for a Shard.
23  *
24  * @author Thomas Pantelis
25  */
26 public class ShardSnapshotState implements Snapshot.State {
27     private static final long serialVersionUID = 1L;
28
29     private static final class Proxy implements Externalizable {
30         private static final long serialVersionUID = 1L;
31
32         private ShardSnapshotState snapshotState;
33
34         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
35         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
36         @SuppressWarnings("checkstyle:RedundantModifier")
37         public Proxy() {
38             // For Externalizable
39         }
40
41         Proxy(final ShardSnapshotState snapshotState) {
42             this.snapshotState = snapshotState;
43         }
44
45         @Override
46         public void writeExternal(final ObjectOutput out) throws IOException {
47             snapshotState.snapshot.serialize(out);
48         }
49
50         @Override
51         public void readExternal(final ObjectInput in) throws IOException {
52             snapshotState = ShardDataTreeSnapshot.deserialize(in);
53         }
54
55         private Object readResolve() {
56             return snapshotState;
57         }
58     }
59
60     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
61             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
62             + "aren't serialized. FindBugs does not recognize this.")
63     private final @NonNull ShardDataTreeSnapshot snapshot;
64     private final boolean migrated;
65
66     @VisibleForTesting
67     public ShardSnapshotState(final @NonNull ShardDataTreeSnapshot snapshot, final boolean migrated) {
68         this.snapshot = requireNonNull(snapshot);
69         this.migrated = migrated;
70     }
71
72     public ShardSnapshotState(final @NonNull ShardDataTreeSnapshot snapshot) {
73         this(snapshot, false);
74     }
75
76     public @NonNull ShardDataTreeSnapshot getSnapshot() {
77         return snapshot;
78     }
79
80     @Override
81     public boolean needsMigration() {
82         return migrated;
83     }
84
85     private Object writeReplace() {
86         return new Proxy(this);
87     }
88 }