Bug 8385: Fix testMultipleRegistrationsAtOnePrefix failure
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / CompositeOnComplete.java
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/CompositeOnComplete.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/CompositeOnComplete.java
new file mode 100644 (file)
index 0000000..0ae3fef
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2017 Inocybe Technologies and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.datastore.utils;
+
+import akka.dispatch.OnComplete;
+import java.util.ArrayList;
+import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * An OnComplete implementation that aggrgates other OnComplete tasks.
+ *
+ * @author Thomas Pantelis
+ *
+ * @param <T> the result type
+ */
+public abstract class CompositeOnComplete<T> extends OnComplete<T> {
+    private static final Logger LOG = LoggerFactory.getLogger(CompositeOnComplete.class);
+
+    private final List<OnComplete<T>> onCompleteTasks = new ArrayList<>();
+
+    public void addOnComplete(OnComplete<T> task) {
+        onCompleteTasks.add(task);
+    }
+
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    protected void notifyOnCompleteTasks(Throwable failure, T result) {
+        for (OnComplete<T> task: onCompleteTasks) {
+            try {
+                task.onComplete(failure, result);
+            } catch (Throwable e) {
+                LOG.error("Caught unexpected exception", e);
+            }
+        }
+
+        onCompleteTasks.clear();
+    }
+}