Refactor AbstractClientHandle a bit 45/85745/10
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 12 Nov 2019 11:21:13 +0000 (12:21 +0100)
committerStephen Kitt <skitt@redhat.com>
Mon, 6 Jan 2020 10:47:09 +0000 (10:47 +0000)
We can pull the proxy creation method down into the subclass,
making it simpler to follow.

Change-Id: I0d025ace708a0646fb0c7dc49fc3f819453b90e5
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractClientHandle.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/ClientSnapshot.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/ClientTransaction.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractClientHandleTest.java

index 1246c2f..9077335 100644 (file)
@@ -13,10 +13,9 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.annotations.Beta;
 import com.google.common.base.MoreObjects;
 import java.util.Collection;
-import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
-import java.util.function.Function;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
 import org.opendaylight.yangtools.concepts.Identifiable;
@@ -103,17 +102,19 @@ public abstract class AbstractClientHandle<T extends AbstractProxyTransaction> e
         return local == null ? null : local.values();
     }
 
-    final T ensureProxy(final YangInstanceIdentifier path, final Function<Long, T> createProxy) {
-        final Map<Long, T> local = getState();
+    final T ensureProxy(final YangInstanceIdentifier path) {
+        final State<T> local = getState();
         final Long shard = parent.resolveShardForPath(path);
 
-        return local.computeIfAbsent(shard, createProxy);
+        return local.computeIfAbsent(shard, this::createProxy);
     }
 
     final AbstractClientHistory parent() {
         return parent;
     }
 
+    abstract @NonNull T createProxy(@NonNull Long shard);
+
     private State<T> getState() {
         final State<T> local = state;
         checkState(local != null, "Transaction %s is closed", transactionId);
index 74c4ae4..c8b1328 100644 (file)
@@ -27,14 +27,6 @@ public class ClientSnapshot extends AbstractClientHandle<AbstractProxyTransactio
         super(parent, transactionId);
     }
 
-    private AbstractProxyTransaction createProxy(final Long shard) {
-        return parent().createSnapshotProxy(getIdentifier(), shard);
-    }
-
-    private AbstractProxyTransaction ensureSnapshotProxy(final YangInstanceIdentifier path) {
-        return ensureProxy(path, this::createProxy);
-    }
-
     public FluentFuture<Boolean> exists(final YangInstanceIdentifier path) {
         return ensureSnapshotProxy(path).exists(path);
     }
@@ -42,4 +34,13 @@ public class ClientSnapshot extends AbstractClientHandle<AbstractProxyTransactio
     public FluentFuture<Optional<NormalizedNode<?, ?>>> read(final YangInstanceIdentifier path) {
         return ensureSnapshotProxy(path).read(path);
     }
+
+    @Override
+    final AbstractProxyTransaction createProxy(final Long shard) {
+        return parent().createSnapshotProxy(getIdentifier(), shard);
+    }
+
+    private AbstractProxyTransaction ensureSnapshotProxy(final YangInstanceIdentifier path) {
+        return ensureProxy(path);
+    }
 }
index 46e208b..c706d99 100644 (file)
@@ -60,12 +60,8 @@ public class ClientTransaction extends AbstractClientHandle<AbstractProxyTransac
         super(parent, transactionId);
     }
 
-    private AbstractProxyTransaction createProxy(final Long shard) {
-        return parent().createTransactionProxy(getIdentifier(), shard);
-    }
-
     private AbstractProxyTransaction ensureTransactionProxy(final YangInstanceIdentifier path) {
-        return ensureProxy(path, this::createProxy);
+        return ensureProxy(path);
     }
 
     public DOMDataTreeWriteCursor openCursor() {
@@ -116,6 +112,11 @@ public class ClientTransaction extends AbstractClientHandle<AbstractProxyTransac
         return parent().onTransactionReady(this, cohort);
     }
 
+    @Override
+    final AbstractProxyTransaction createProxy(final Long shard) {
+        return parent().createTransactionProxy(getIdentifier(), shard);
+    }
+
     void closeCursor(final @NonNull DOMDataTreeCursor cursorToClose) {
         if (cursorToClose.equals(this.cursor)) {
             this.cursor = null;
index 6ea02fe..a8ba69b 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.controller.cluster.databroker.actors.dds;
 
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.CLIENT_ID;
 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.HISTORY_ID;
@@ -22,7 +21,6 @@ import akka.testkit.TestProbe;
 import akka.testkit.javadsl.TestKit;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.function.Function;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
@@ -146,12 +144,9 @@ public abstract class AbstractClientHandleTest<T extends AbstractClientHandle<Ab
 
     @Test
     public void testEnsureProxy() {
-        final Function<Long, AbstractProxyTransaction> function = mock(Function.class);
         final AbstractProxyTransaction expected = mock(AbstractProxyTransaction.class);
-        when(function.apply(0L)).thenReturn(expected);
-        final AbstractProxyTransaction proxy = handle.ensureProxy(PATH, function);
-        verify(function).apply(0L);
-        Assert.assertEquals(expected, proxy);
+        final AbstractProxyTransaction proxy = handle.ensureProxy(PATH);
+        Assert.assertEquals(0, proxy.getIdentifier().getTransactionId());
     }
 
     @Test

©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.