2 * Copyright (c) 2017 Brocade Communications 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.collect.ImmutableList;
11 import java.io.Externalizable;
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15 import java.io.Serializable;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNull;
21 * Represents the persisted snapshot state for the ShardManager.
23 * @author Thomas Pantelis
25 public class ShardManagerSnapshot implements Serializable {
26 private static final long serialVersionUID = 1L;
28 private static final class Proxy implements Externalizable {
29 private static final long serialVersionUID = 1L;
31 private ShardManagerSnapshot snapshot;
33 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
34 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
35 @SuppressWarnings("checkstyle:RedundantModifier")
40 Proxy(final ShardManagerSnapshot snapshot) {
41 this.snapshot = snapshot;
45 public void writeExternal(final ObjectOutput out) throws IOException {
46 out.writeInt(snapshot.shardList.size());
47 for (String shard: snapshot.shardList) {
48 out.writeObject(shard);
53 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
54 int size = in.readInt();
55 List<String> localShardList = new ArrayList<>(size);
56 for (int i = 0; i < size; i++) {
57 localShardList.add((String) in.readObject());
60 snapshot = new ShardManagerSnapshot(localShardList);
63 private Object readResolve() {
68 private final List<String> shardList;
70 public ShardManagerSnapshot(final @NonNull List<String> shardList) {
71 this.shardList = ImmutableList.copyOf(shardList);
74 public List<String> getShardList() {
75 return this.shardList;
78 private Object writeReplace() {
79 return new Proxy(this);
83 public String toString() {
84 return "ShardManagerSnapshot [ShardList = " + shardList + " ]";