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 static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.collect.ImmutableList;
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 java.util.ArrayList;
20 import java.util.List;
21 import org.eclipse.jdt.annotation.NonNull;
24 * Represents the persisted snapshot state for the ShardManager.
26 * @author Thomas Pantelis
28 public final class ShardManagerSnapshot implements Serializable {
29 interface SerializedForm extends Externalizable {
31 * Return the serial form of this object contents, corresponding to {@link ShardManagerSnapshot#shardList}.
33 * @return List of shards names.
35 List<String> shardNames();
38 * Resolve this proxy to an actual {@link ShardManagerSnapshot}. Implementations can rely on the object to be
39 * set via {@link #resolveTo(ShardManagerSnapshot)}.
46 * Set this proxy to return {@code snapshot} on next {@link #readResolve()}.
48 * @param newSnapshot Snapshot to set
50 void resolveTo(@NonNull ShardManagerSnapshot newSnapshot);
53 default void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
54 final int size = in.readInt();
55 final var shardList = new ArrayList<String>(size);
56 for (int i = 0; i < size; i++) {
57 shardList.add((String) in.readObject());
59 resolveTo(new ShardManagerSnapshot(shardList));
63 default void writeExternal(final ObjectOutput out) throws IOException {
64 final var shardList = shardNames();
65 out.writeInt(shardList.size());
66 for (var shardName : shardList) {
67 out.writeObject(shardName);
72 private static final class Proxy implements SerializedForm {
73 private static final long serialVersionUID = 1L;
75 private ShardManagerSnapshot snapshot;
77 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
78 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
79 @SuppressWarnings("checkstyle:RedundantModifier")
84 Proxy(final ShardManagerSnapshot snapshot) {
85 this.snapshot = snapshot;
89 public List<String> shardNames() {
90 return snapshot.getShardList();
94 public void resolveTo(final ShardManagerSnapshot newSnapshot) {
95 snapshot = requireNonNull(newSnapshot);
99 public Object readResolve() {
100 return verifyNotNull(snapshot);
104 private static final long serialVersionUID = 1L;
106 private final List<String> shardList;
108 public ShardManagerSnapshot(final @NonNull List<String> shardList) {
109 this.shardList = ImmutableList.copyOf(shardList);
112 public List<String> getShardList() {
116 private Object writeReplace() {
117 return new Proxy(this);
121 public String toString() {
122 return "ShardManagerSnapshot [ShardList = " + shardList + " ]";