sal-dom-broker: use lambdas 86/57186/2
authorStephen Kitt <skitt@redhat.com>
Tue, 16 May 2017 15:51:37 +0000 (17:51 +0200)
committerRobert Varga <nite@hq.sk>
Tue, 16 May 2017 23:16:15 +0000 (23:16 +0000)
This series of patches uses lambdas instead of anonymous classes for
functional interfaces when possible. Lambdas are replaced with method
references when appropriate.

Change-Id: I99d0a505511aa0deaf675ff3c149fdce40bfb552
Signed-off-by: Stephen Kitt <skitt@redhat.com>
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/AbstractDOMRpcRoutingTableEntry.java
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/DOMNotificationRouter.java
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/DOMNotificationRouterEvent.java
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/DOMRpcRouter.java
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/util/YangSchemaUtils.java
opendaylight/md-sal/sal-dom-broker/src/test/java/org/opendaylight/controller/md/sal/dom/broker/impl/DOMBrokerPerformanceTest.java
opendaylight/md-sal/sal-dom-broker/src/test/java/org/opendaylight/controller/md/sal/dom/broker/impl/DOMBrokerTest.java

index 0cce2f1..b346c4e 100644 (file)
@@ -13,6 +13,7 @@ import com.google.common.collect.ImmutableMap.Builder;
 import com.google.common.collect.Maps;
 import com.google.common.util.concurrent.CheckedFuture;
 import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -72,7 +73,7 @@ abstract class AbstractDOMRpcRoutingTableEntry {
 
                 // New implementation is at the end, this will move it to be the last among implementations
                 // with equal cost -- relying on sort() being stable.
-                i.sort((a, b) -> Long.compare(a.invocationCost(), b.invocationCost()));
+                i.sort(Comparator.comparingLong(DOMRpcImplementation::invocationCost));
                 vb.put(ve.getKey(), i);
             } else {
                 vb.put(ve);
index 41dfcb9..2dc1d24 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.controller.md.sal.dom.broker.impl;
 
 import com.google.common.base.Preconditions;
-import com.google.common.base.Predicate;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMultimap;
 import com.google.common.collect.ImmutableMultimap.Builder;
@@ -62,19 +61,10 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
     private static final Logger LOG = LoggerFactory.getLogger(DOMNotificationRouter.class);
     private static final ListenableFuture<Void> NO_LISTENERS = Futures.immediateFuture(null);
     private static final WaitStrategy DEFAULT_STRATEGY = PhasedBackoffWaitStrategy.withLock(1L, 30L, TimeUnit.MILLISECONDS);
-    private static final EventHandler<DOMNotificationRouterEvent> DISPATCH_NOTIFICATIONS = new EventHandler<DOMNotificationRouterEvent>() {
-        @Override
-        public void onEvent(final DOMNotificationRouterEvent event, final long sequence, final boolean endOfBatch) throws Exception {
-            event.deliverNotification();
-
-        }
-    };
-    private static final EventHandler<DOMNotificationRouterEvent> NOTIFY_FUTURE = new EventHandler<DOMNotificationRouterEvent>() {
-        @Override
-        public void onEvent(final DOMNotificationRouterEvent event, final long sequence, final boolean endOfBatch) {
-            event.setFuture();
-        }
-    };
+    private static final EventHandler<DOMNotificationRouterEvent> DISPATCH_NOTIFICATIONS =
+            (event, sequence, endOfBatch) -> event.deliverNotification();
+    private static final EventHandler<DOMNotificationRouterEvent> NOTIFY_FUTURE =
+            (event, sequence, endOfBatch) -> event.setFuture();
 
     private final Disruptor<DOMNotificationRouterEvent> disruptor;
     private final ExecutorService executor;
@@ -114,12 +104,7 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
                 final ListenerRegistration<T> me = this;
 
                 synchronized (DOMNotificationRouter.this) {
-                    replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, new Predicate<ListenerRegistration<? extends DOMNotificationListener>>() {
-                        @Override
-                        public boolean apply(final ListenerRegistration<? extends DOMNotificationListener> input) {
-                            return input != me;
-                        }
-                    })));
+                    replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, input -> input != me)));
                 }
             }
         };
@@ -156,16 +141,12 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
 
     private void notifyListenerTypesChanged(final Set<SchemaPath> typesAfter) {
         final List<ListenerRegistration<DOMNotificationSubscriptionListener>> listenersAfter =ImmutableList.copyOf(subscriptionListeners.getListeners());
-        executor.submit(new Runnable() {
-
-            @Override
-            public void run() {
-                for (final ListenerRegistration<DOMNotificationSubscriptionListener> subListener : listenersAfter) {
-                    try {
-                        subListener.getInstance().onSubscriptionChanged(typesAfter);
-                    } catch (final Exception e) {
-                        LOG.warn("Uncaught exception during invoking listener {}", subListener.getInstance(), e);
-                    }
+        executor.submit(() -> {
+            for (final ListenerRegistration<DOMNotificationSubscriptionListener> subListener : listenersAfter) {
+                try {
+                    subListener.getInstance().onSubscriptionChanged(typesAfter);
+                } catch (final Exception e) {
+                    LOG.warn("Uncaught exception during invoking listener {}", subListener.getInstance(), e);
                 }
             }
         });
@@ -175,13 +156,7 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati
     public <L extends DOMNotificationSubscriptionListener> ListenerRegistration<L> registerSubscriptionListener(
             final L listener) {
         final Set<SchemaPath> initialTypes = listeners.keySet();
-        executor.submit(new Runnable() {
-
-            @Override
-            public void run() {
-                listener.onSubscriptionChanged(initialTypes);
-            }
-        });
+        executor.submit(() -> listener.onSubscriptionChanged(initialTypes));
         return subscriptionListeners.registerWithType(listener);
     }
 
index 0367cab..5d0fcd6 100644 (file)
@@ -24,12 +24,7 @@ import org.slf4j.LoggerFactory;
  */
 final class DOMNotificationRouterEvent {
     private static final Logger LOG = LoggerFactory.getLogger(DOMNotificationRouterEvent.class);
-    public static final EventFactory<DOMNotificationRouterEvent> FACTORY = new EventFactory<DOMNotificationRouterEvent>() {
-        @Override
-        public DOMNotificationRouterEvent newInstance() {
-            return new DOMNotificationRouterEvent();
-        }
-    };
+    public static final EventFactory<DOMNotificationRouterEvent> FACTORY = DOMNotificationRouterEvent::new;
 
     private Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers;
     private DOMNotification notification;
index 3c28d30..02d2710 100644 (file)
@@ -127,7 +127,7 @@ public final class DOMRpcRouter implements AutoCloseable, DOMRpcService, DOMRpcP
         b.add(ret);
         listeners = b.build();
 
-        listenerNotifier.execute(() -> ret.initialTable());
+        listenerNotifier.execute(ret::initialTable);
         return ret;
     }
 
index ad759a7..27630fb 100644 (file)
@@ -39,15 +39,11 @@ import org.opendaylight.yangtools.yang.model.api.UsesNode;
 
 public final class YangSchemaUtils {
 
-    private static final Function<PathArgument, QName> QNAME_FROM_PATH_ARGUMENT = new Function<PathArgument, QName>(){
-
-        @Override
-        public QName apply(final PathArgument input) {
-            if(input == null) {
-                return null;
-            }
-            return input.getNodeType();
+    private static final Function<PathArgument, QName> QNAME_FROM_PATH_ARGUMENT = input -> {
+        if (input == null) {
+            return null;
         }
+        return input.getNodeType();
     };
 
     private YangSchemaUtils() {
index 41c906d..81af5f3 100644 (file)
@@ -85,198 +85,142 @@ public class DOMBrokerPerformanceTest {
 
     @Test
     public void testPerformance() throws Exception {
-        measure("Test Suite (all tests)", new Callable<Void>() {
-
-            @Override
-            public Void call() throws Exception {
-                smallTestSuite(10, 1000);
-                //smallTestSuite(10, 100);
-                smallTestSuite(100, 100);
-                //smallTestSuite(100, 100);
-                //smallTestSuite(1000, 10);
-                smallTestSuite(1000, 10);
-                //smallTestSuite(1000, 1000);
-                return null;
-            }
+        measure("Test Suite (all tests)", (Callable<Void>) () -> {
+            smallTestSuite(10, 1000);
+            //smallTestSuite(10, 100);
+            smallTestSuite(100, 100);
+            //smallTestSuite(100, 100);
+            //smallTestSuite(1000, 10);
+            smallTestSuite(1000, 10);
+            //smallTestSuite(1000, 1000);
+            return null;
         });
     }
 
     private void smallTestSuite(final int txNum, final int innerListWriteNum) throws Exception {
-        measure("TestSuite (Txs:" + txNum + " innerWrites:" + innerListWriteNum + ")", new Callable<Void>() {
-
-            @Override
-            public Void call() throws Exception {
-                measureOneTransactionTopContainer();
-                measureSeparateWritesOneLevel(txNum, innerListWriteNum);
-                return null;
-            }
+        measure("TestSuite (Txs:" + txNum + " innerWrites:" + innerListWriteNum + ")", (Callable<Void>) () -> {
+            measureOneTransactionTopContainer();
+            measureSeparateWritesOneLevel(txNum, innerListWriteNum);
+            return null;
         });
     }
 
     private void measureSeparateWritesOneLevel(final int txNum, final int innerNum) throws Exception {
         final List<DOMDataReadWriteTransaction> transactions = measure("Txs:"+ txNum + " Allocate",
-                new Callable<List<DOMDataReadWriteTransaction>>() {
-                    @Override
-                    public List<DOMDataReadWriteTransaction> call() throws Exception {
-                        List<DOMDataReadWriteTransaction> builder = new ArrayList<>(txNum);
-                        for (int i = 0; i < txNum; i++) {
-                            DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
-                            builder.add(writeTx);
-                        }
-                        return builder;
+                () -> {
+                    List<DOMDataReadWriteTransaction> builder = new ArrayList<>(txNum);
+                    for (int i = 0; i < txNum; i++) {
+                        DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
+                        builder.add(writeTx);
                     }
+                    return builder;
                 });
         assertEquals(txNum, transactions.size());
-        measure("Txs:"+ txNum + " Writes:1", new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                int i = 0;
-                for (DOMDataReadWriteTransaction writeTx :transactions) {
-                    // Writes /test/outer-list/i in writeTx
-                    writeTx.put(OPERATIONAL, outerListPath(i), outerList(i));
-                    i++;
-                }
-                return null;
+        measure("Txs:"+ txNum + " Writes:1", (Callable<Void>) () -> {
+            int i = 0;
+            for (DOMDataReadWriteTransaction writeTx :transactions) {
+                // Writes /test/outer-list/i in writeTx
+                writeTx.put(OPERATIONAL, outerListPath(i), outerList(i));
+                i++;
             }
+            return null;
         });
 
-        measure("Txs:"+ txNum +  " Writes:" + innerNum, new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                int i = 0;
-                for (DOMDataReadWriteTransaction writeTx :transactions) {
-                    // Writes /test/outer-list/i in writeTx
-                    YangInstanceIdentifier path = YangInstanceIdentifier.builder(outerListPath(i))
-                            .node(TestModel.INNER_LIST_QNAME).build();
-                    writeTx.put(OPERATIONAL, path, ImmutableNodes.mapNodeBuilder(TestModel.INNER_LIST_QNAME).build());
-                    for (int j = 0; j < innerNum; j++) {
-                        YangInstanceIdentifier innerPath = YangInstanceIdentifier.builder(path)
-                                .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
-                                .build();
-                        writeTx.put(
-                                OPERATIONAL,
-                                innerPath,
-                                ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME,
-                                        String.valueOf(j)));
-                    }
-                    i++;
+        measure("Txs:"+ txNum +  " Writes:" + innerNum, (Callable<Void>) () -> {
+            int i = 0;
+            for (DOMDataReadWriteTransaction writeTx :transactions) {
+                // Writes /test/outer-list/i in writeTx
+                YangInstanceIdentifier path = YangInstanceIdentifier.builder(outerListPath(i))
+                        .node(TestModel.INNER_LIST_QNAME).build();
+                writeTx.put(OPERATIONAL, path, ImmutableNodes.mapNodeBuilder(TestModel.INNER_LIST_QNAME).build());
+                for (int j = 0; j < innerNum; j++) {
+                    YangInstanceIdentifier innerPath = YangInstanceIdentifier.builder(path)
+                            .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
+                            .build();
+                    writeTx.put(
+                            OPERATIONAL,
+                            innerPath,
+                            ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME,
+                                    String.valueOf(j)));
                 }
-                return null;
+                i++;
             }
+            return null;
         });
 
-        measure("Txs:" + txNum + " Submit, Finish", new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                List<ListenableFuture<?>> allFutures = measure(txNum + " Submits",
-                        new Callable<List<ListenableFuture<?>>>() {
-                            @Override
-                            public List<ListenableFuture<?>> call() throws Exception {
-                                List<ListenableFuture<?>> builder = new ArrayList<>(txNum);
-                                for (DOMDataReadWriteTransaction tx :transactions) {
-                                    builder.add(tx.submit());
-                                }
-                                return builder;
-                            }
-                        });
-                Futures.allAsList(allFutures).get();
-                return null;
-            }
+        measure("Txs:" + txNum + " Submit, Finish", (Callable<Void>) () -> {
+            List<ListenableFuture<?>> allFutures = measure(txNum + " Submits",
+                    () -> {
+                        List<ListenableFuture<?>> builder = new ArrayList<>(txNum);
+                        for (DOMDataReadWriteTransaction tx :transactions) {
+                            builder.add(tx.submit());
+                        }
+                        return builder;
+                    });
+            Futures.allAsList(allFutures).get();
+            return null;
         });
 
-        final DOMDataReadTransaction readTx = measure("Txs:1 (ro), Allocate", new Callable<DOMDataReadTransaction>() {
-            @Override
-            public DOMDataReadTransaction call() throws Exception {
-                return domBroker.newReadOnlyTransaction();
-
-            }
-        });
+        final DOMDataReadTransaction readTx = measure("Txs:1 (ro), Allocate",
+                (Callable<DOMDataReadTransaction>) () -> domBroker.newReadOnlyTransaction());
 
 
-        measure("Txs:1 (ro) Reads:" + txNum + " (1-level)" , new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                for (int i = 0; i < txNum; i++) {
-                    ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL,
-                            outerListPath(i));
-                    assertTrue("outerList/" + i, potential.get().isPresent());
-                }
-                return null;
+        measure("Txs:1 (ro) Reads:" + txNum + " (1-level)" , (Callable<Void>) () -> {
+            for (int i = 0; i < txNum; i++) {
+                ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL,
+                        outerListPath(i));
+                assertTrue("outerList/" + i, potential.get().isPresent());
             }
+            return null;
         });
 
-        measure("Txs:1 (ro) Reads:" + txNum * innerNum + " (2-level)", new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                for (int i = 0; i < txNum; i++) {
-                    for (int j = 0; j < innerNum; j++) {
-                        YangInstanceIdentifier path = YangInstanceIdentifier
-                                .builder(outerListPath(i))
-                                //
-                                .node(TestModel.INNER_LIST_QNAME)
-                                .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
-                                .build();
-                        ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL, path);
-                        assertTrue("outer-list/" + i + "/inner-list/" + j, potential.get().isPresent());
-                    }
+        measure("Txs:1 (ro) Reads:" + txNum * innerNum + " (2-level)", (Callable<Void>) () -> {
+            for (int i = 0; i < txNum; i++) {
+                for (int j = 0; j < innerNum; j++) {
+                    YangInstanceIdentifier path = YangInstanceIdentifier
+                            .builder(outerListPath(i))
+                            //
+                            .node(TestModel.INNER_LIST_QNAME)
+                            .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
+                            .build();
+                    ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL, path);
+                    assertTrue("outer-list/" + i + "/inner-list/" + j, potential.get().isPresent());
                 }
-                return null;
             }
+            return null;
         });
     }
 
     private void measureOneTransactionTopContainer() throws Exception {
 
-        final DOMDataReadWriteTransaction writeTx = measure("Txs:1 Allocate", new Callable<DOMDataReadWriteTransaction>() {
-            @Override
-            public DOMDataReadWriteTransaction call() throws Exception {
-                return domBroker.newReadWriteTransaction();
-            }
-        });
+        final DOMDataReadWriteTransaction writeTx = measure("Txs:1 Allocate", () -> domBroker.newReadWriteTransaction());
 
-        measure("Txs:1 Write", new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
-                writeTx.put(OPERATIONAL, TestModel.OUTER_LIST_PATH,
-                        ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
-                return null;
-            }
+        measure("Txs:1 Write", (Callable<Void>) () -> {
+            writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
+            writeTx.put(OPERATIONAL, TestModel.OUTER_LIST_PATH,
+                    ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
+            return null;
         });
 
-        measure("Txs:1 Reads:1", new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                // Reads /test in writeTx
-                ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
-                        TestModel.TEST_PATH);
-                assertTrue(writeTxContainer.get().isPresent());
-                return null;
-            }
+        measure("Txs:1 Reads:1", (Callable<Void>) () -> {
+            // Reads /test in writeTx
+            ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
+                    TestModel.TEST_PATH);
+            assertTrue(writeTxContainer.get().isPresent());
+            return null;
         });
 
-        measure("Txs:1 Reads:1", new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                // Reads /test in writeTx
-                ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
-                        TestModel.TEST_PATH);
-                assertTrue(writeTxContainer.get().isPresent());
-                return null;
-            }
+        measure("Txs:1 Reads:1", (Callable<Void>) () -> {
+            // Reads /test in writeTx
+            ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
+                    TestModel.TEST_PATH);
+            assertTrue(writeTxContainer.get().isPresent());
+            return null;
         });
 
-        measure("Txs:1 Submit, Finish", new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                measure("Txs:1 Submit", new Callable<ListenableFuture<?>>() {
-                    @Override
-                    public ListenableFuture<?> call() throws Exception {
-                        return writeTx.submit();
-                    }
-                }).get();
-                return null;
-            }
+        measure("Txs:1 Submit, Finish", (Callable<Void>) () -> {
+            measure("Txs:1 Submit", (Callable<ListenableFuture<?>>) () -> writeTx.submit()).get();
+            return null;
         });
     }
 }
index b310715..be07a45 100644 (file)
@@ -316,18 +316,13 @@ public class DOMBrokerTest {
 
     AtomicReference<Throwable> submitTxAsync( final DOMDataWriteTransaction writeTx ) {
         final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
-        new Thread() {
-            @Override
-            public void run() {
-
-                try {
-                    writeTx.submit();
-                } catch( Throwable e ) {
-                    caughtEx.set( e );
-                }
+        new Thread(() -> {
+            try {
+                writeTx.submit();
+            } catch (Throwable e) {
+                caughtEx.set(e);
             }
-
-        }.start();
+        }).start();
 
         return caughtEx;
     }

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