Clean AbstractServerChangeReply up 22/34622/9
authorRobert Varga <rovarga@cisco.com>
Sun, 14 Feb 2016 01:32:58 +0000 (02:32 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 17 Feb 2016 09:30:21 +0000 (09:30 +0000)
This is an abstract base class, make it abstract and non-instantiable
from outside of the world. It introduces public API, hence we cannot
hide it.

Also switch to using MoreObjects.ToStringHelper, as that allows us to
unify toString().

Change-Id: I21bdd4d0815a50393519414449c7e7eb7179b077
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/messages/AbstractServerChangeReply.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/messages/AddServerReply.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/messages/RemoveServerReply.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorServerConfigurationSupportTest.java

index 49bd94e..3f0cf43 100644 (file)
@@ -7,38 +7,42 @@
  */
 package org.opendaylight.controller.cluster.raft.messages;
 
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Preconditions;
 import java.io.Serializable;
+import java.util.Optional;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 
 /**
  * Abstract base class for a server configuration change reply.
  *
  * @author Thomas Pantelis
  */
-public class AbstractServerChangeReply implements Serializable {
+public abstract class AbstractServerChangeReply implements Serializable {
     private static final long serialVersionUID = 1L;
 
     private final String leaderHint;
     private final ServerChangeStatus status;
 
-    public AbstractServerChangeReply(ServerChangeStatus status, String leaderHint) {
-        this.status = status;
+    AbstractServerChangeReply(final @Nonnull ServerChangeStatus status, final @Nullable String leaderHint) {
+        this.status = Preconditions.checkNotNull(status);
         this.leaderHint = leaderHint;
     }
 
-    public static long getSerialversionuid() {
-        return serialVersionUID;
+    @VisibleForTesting
+    @Nonnull public final Optional<String> getLeaderHint() {
+        return Optional.ofNullable(leaderHint);
     }
 
-    public String getLeaderHint() {
-        return leaderHint;
-    }
-
-    public ServerChangeStatus getStatus() {
+    @Nonnull public final ServerChangeStatus getStatus() {
         return status;
     }
 
     @Override
-    public String toString() {
-        return getClass().getSimpleName() + " [status=" + status + ", leaderHint=" + leaderHint + "]";
+    public final String toString() {
+        return MoreObjects.toStringHelper(getClass()).omitNullValues()
+            .add("status", status).add("leaderHint", leaderHint).toString();
     }
 }
index 40d39f4..fc950aa 100644 (file)
@@ -12,7 +12,7 @@ package org.opendaylight.controller.cluster.raft.messages;
  *
  * @author Thomas Pantelis
  */
-public class AddServerReply extends AbstractServerChangeReply {
+public final class AddServerReply extends AbstractServerChangeReply {
     private static final long serialVersionUID = 1L;
 
     public AddServerReply(ServerChangeStatus status, String leaderHint) {
index 36a48fc..fe643d8 100644 (file)
@@ -13,15 +13,10 @@ import javax.annotation.Nullable;
 /**
  * Reply to a RemoveServer message (ยง4.1).
  */
-public class RemoveServerReply extends AbstractServerChangeReply {
+public final class RemoveServerReply extends AbstractServerChangeReply {
     private static final long serialVersionUID = 1L;
 
     public RemoveServerReply(ServerChangeStatus status, @Nullable String leaderHint) {
         super(status, leaderHint);
     }
-
-    @Override
-    public String toString() {
-        return "RemoveServerReply{" + "status=" + getStatus() + ", leaderHint='" + getLeaderHint() + '\'' + '}';
-    }
 }
index 6d102db..36f6fe7 100644 (file)
@@ -152,7 +152,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest {
 
         AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class);
         assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus());
-        assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint());
+        assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint().get());
 
         // Verify ServerConfigurationPayload entry in leader's log
 
@@ -235,7 +235,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest {
 
         AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class);
         assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus());
-        assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint());
+        assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint().get());
 
         // Verify ServerConfigurationPayload entry in leader's log
 
@@ -276,7 +276,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest {
 
         AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class);
         assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus());
-        assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint());
+        assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint().get());
 
         // Verify ServerConfigurationPayload entry in leader's log
 
@@ -313,7 +313,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest {
 
         addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class);
         assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus());
-        assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint());
+        assertEquals("getLeaderHint", java.util.Optional.of(LEADER_ID), addServerReply.getLeaderHint());
 
         expectFirstMatching(leaderCollectorActor, ApplyState.class);
         assertEquals("Leader journal last index", 1, leaderActorContext.getReplicatedLog().lastIndex());
@@ -409,7 +409,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest {
 
         AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class);
         assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus());
-        assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint());
+        assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint().get());
 
         expectFirstMatching(newFollowerCollectorActor, ApplySnapshot.class);
 
@@ -619,7 +619,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest {
         // The first AddServer should succeed with OK even though consensus wasn't reached
         AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class);
         assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus());
-        assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint());
+        assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint().get());
 
         // Verify ServerConfigurationPayload entry in leader's log
         verifyServerConfigurationPayloadEntry(leaderActorContext.getReplicatedLog(), votingServer(LEADER_ID),