Move null check from getTotalMemory() 95/37495/2
authorRobert Varga <rovarga@cisco.com>
Tue, 12 Apr 2016 14:16:41 +0000 (16:16 +0200)
committerTom Pantelis <tpanteli@brocade.com>
Tue, 12 Apr 2016 20:42:30 +0000 (20:42 +0000)
This patch converts the interface so it uses a specialized LongSupplier,
which prevents autoboxing. It defines a default retriever, which returns
the memory returned from the JVM, which is used instead of a conditional
method dispatch.

Change-Id: I530baa6512daf3e4e4299783a822e16357723c9a
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorContext.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorContextImpl.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractRaftActorIntegrationTest.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/ReplicatedLogImplTest.java

index e20690f..6b5fffe 100644 (file)
@@ -14,7 +14,7 @@ import akka.actor.ActorSystem;
 import akka.actor.Props;
 import com.google.common.annotations.VisibleForTesting;
 import java.util.Collection;
-import java.util.function.Supplier;
+import java.util.function.LongSupplier;
 import javax.annotation.Nullable;
 import org.opendaylight.controller.cluster.DataPersistenceProvider;
 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
@@ -208,7 +208,7 @@ public interface RaftActorContext {
      * @param retriever a supplier of the total memory metric
      */
     @VisibleForTesting
-    void setTotalMemoryRetriever(Supplier<Long> retriever);
+    void setTotalMemoryRetriever(LongSupplier retriever);
 
     /**
      *
index fa85151..e00ed89 100644 (file)
@@ -22,7 +22,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.function.Supplier;
+import java.util.function.LongSupplier;
 import org.opendaylight.controller.cluster.DataPersistenceProvider;
 import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload.ServerInfo;
 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
@@ -30,6 +30,7 @@ import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
 import org.slf4j.Logger;
 
 public class RaftActorContextImpl implements RaftActorContext {
+    private static final LongSupplier JVM_MEMORY_RETRIEVER = () -> Runtime.getRuntime().totalMemory();
 
     private final ActorRef actor;
 
@@ -54,7 +55,7 @@ public class RaftActorContextImpl implements RaftActorContext {
     private boolean dynamicServerConfiguration = false;
 
     @VisibleForTesting
-    private Supplier<Long> totalMemoryRetriever;
+    private LongSupplier totalMemoryRetriever = JVM_MEMORY_RETRIEVER;
 
     // Snapshot manager will need to be created on demand as it needs raft actor context which cannot
     // be passed to it in the constructor
@@ -272,12 +273,12 @@ public class RaftActorContextImpl implements RaftActorContext {
 
     @Override
     public long getTotalMemory() {
-        return totalMemoryRetriever != null ? totalMemoryRetriever.get() : Runtime.getRuntime().totalMemory();
+        return totalMemoryRetriever.getAsLong();
     }
 
     @Override
-    public void setTotalMemoryRetriever(Supplier<Long> retriever) {
-        totalMemoryRetriever = retriever;
+    public void setTotalMemoryRetriever(LongSupplier retriever) {
+        totalMemoryRetriever = retriever == null ? JVM_MEMORY_RETRIEVER : retriever;
     }
 
     @Override
index b81dcc9..b30412f 100644 (file)
@@ -25,7 +25,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
-import java.util.function.Supplier;
 import org.junit.After;
 import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload;
 import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries;
@@ -85,17 +84,7 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest
         }
 
         void setMockTotalMemory(final long mockTotalMemory) {
-            if(mockTotalMemory > 0) {
-                getRaftActorContext().setTotalMemoryRetriever(new Supplier<Long>() {
-                    @Override
-                    public Long get() {
-                        return mockTotalMemory;
-                    }
-
-                });
-            } else {
-                getRaftActorContext().setTotalMemoryRetriever(null);
-            }
+            getRaftActorContext().setTotalMemoryRetriever(mockTotalMemory > 0 ? () -> mockTotalMemory : null);
         }
 
         @Override
index 9bef4fb..c6ee950 100644 (file)
@@ -15,7 +15,6 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
 import akka.japi.Procedure;
 import java.util.Collections;
-import java.util.function.Supplier;
 import org.hamcrest.BaseMatcher;
 import org.hamcrest.Description;
 import org.hamcrest.Matcher;
@@ -125,12 +124,7 @@ public class ReplicatedLogImplTest {
     public void testAppendAndPersistExpectingCaptureDueToDataSize() throws Exception {
         doReturn(1L).when(mockBehavior).getReplicatedToAllIndex();
 
-        context.setTotalMemoryRetriever(new Supplier<Long>() {
-            @Override
-            public Long get() {
-                return 100L;
-            }
-        });
+        context.setTotalMemoryRetriever(() -> 100);
 
         ReplicatedLog log = ReplicatedLogImpl.newInstance(context);
 

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.