Migrate nullness annotations
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / DatastoreSnapshot.java
1 /*
2  * Copyright (c) 2015 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.collect.ImmutableList;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.io.Serializable;
18 import java.util.ArrayList;
19 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
23
24 /**
25  * Stores a snapshot of the internal state of a data store.
26  *
27  * @author Thomas Pantelis
28  */
29 public class DatastoreSnapshot implements Serializable {
30     private static final long serialVersionUID = 1L;
31
32     private static final class Proxy implements Externalizable {
33         private static final long serialVersionUID = 1L;
34
35         private DatastoreSnapshot datastoreSnapshot;
36
37         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
38         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
39         @SuppressWarnings("checkstyle:RedundantModifier")
40         public Proxy() {
41             // For Externalizable
42         }
43
44         Proxy(final DatastoreSnapshot datastoreSnapshot) {
45             this.datastoreSnapshot = datastoreSnapshot;
46         }
47
48         @Override
49         public void writeExternal(ObjectOutput out) throws IOException {
50             out.writeObject(datastoreSnapshot.type);
51             out.writeObject(datastoreSnapshot.shardManagerSnapshot);
52
53             out.writeInt(datastoreSnapshot.shardSnapshots.size());
54             for (ShardSnapshot shardSnapshot: datastoreSnapshot.shardSnapshots) {
55                 out.writeObject(shardSnapshot);
56             }
57         }
58
59         @Override
60         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
61             String localType = (String)in.readObject();
62             ShardManagerSnapshot localShardManagerSnapshot = (ShardManagerSnapshot) in.readObject();
63
64             int size = in.readInt();
65             List<ShardSnapshot> localShardSnapshots = new ArrayList<>(size);
66             for (int i = 0; i < size; i++) {
67                 localShardSnapshots.add((ShardSnapshot) in.readObject());
68             }
69
70             datastoreSnapshot = new DatastoreSnapshot(localType, localShardManagerSnapshot, localShardSnapshots);
71         }
72
73         private Object readResolve() {
74             return datastoreSnapshot;
75         }
76     }
77
78     private final String type;
79     private final ShardManagerSnapshot shardManagerSnapshot;
80     private final List<ShardSnapshot> shardSnapshots;
81
82     public DatastoreSnapshot(@NonNull String type, @Nullable ShardManagerSnapshot shardManagerSnapshot,
83             @NonNull List<ShardSnapshot> shardSnapshots) {
84         this.type = requireNonNull(type);
85         this.shardManagerSnapshot = shardManagerSnapshot;
86         this.shardSnapshots = ImmutableList.copyOf(shardSnapshots);
87     }
88
89     public @NonNull String getType() {
90         return type;
91     }
92
93     public @Nullable ShardManagerSnapshot getShardManagerSnapshot() {
94         return shardManagerSnapshot;
95     }
96
97     public @NonNull List<ShardSnapshot> getShardSnapshots() {
98         return shardSnapshots;
99     }
100
101     private Object writeReplace() {
102         return new Proxy(this);
103     }
104
105     public static class ShardSnapshot implements Serializable {
106         private static final long serialVersionUID = 1L;
107
108         private static final class Proxy implements Externalizable {
109             private static final long serialVersionUID = 1L;
110
111             private ShardSnapshot shardSnapshot;
112
113             // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
114             // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
115             @SuppressWarnings("checkstyle:RedundantModifier")
116             public Proxy() {
117                 // For Externalizable
118             }
119
120             Proxy(final ShardSnapshot shardSnapshot) {
121                 this.shardSnapshot = shardSnapshot;
122             }
123
124             @Override
125             public void writeExternal(ObjectOutput out) throws IOException {
126                 out.writeObject(shardSnapshot.name);
127                 out.writeObject(shardSnapshot.snapshot);
128             }
129
130             @Override
131             public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
132                 shardSnapshot = new ShardSnapshot((String)in.readObject(), (Snapshot) in.readObject());
133             }
134
135             private Object readResolve() {
136                 return shardSnapshot;
137             }
138         }
139
140         private final String name;
141         private final Snapshot snapshot;
142
143         public ShardSnapshot(@NonNull String name, @NonNull Snapshot snapshot) {
144             this.name = requireNonNull(name);
145             this.snapshot = requireNonNull(snapshot);
146         }
147
148         public @NonNull String getName() {
149             return name;
150         }
151
152         public @NonNull Snapshot getSnapshot() {
153             return snapshot;
154         }
155
156         private Object writeReplace() {
157             return new Proxy(this);
158         }
159     }
160 }