Fix sonar warnings in sal-distributed-datastore
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / CompositeOnComplete.java
1 /*
2  * Copyright (c) 2017 Inocybe Technologies and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore.utils;
9
10 import akka.dispatch.OnComplete;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 /**
17  * An OnComplete implementation that aggrgates other OnComplete tasks.
18  *
19  * @author Thomas Pantelis
20  *
21  * @param <T> the result type
22  */
23 public abstract class CompositeOnComplete<T> extends OnComplete<T> {
24     private static final Logger LOG = LoggerFactory.getLogger(CompositeOnComplete.class);
25
26     private final List<OnComplete<T>> onCompleteTasks = new ArrayList<>();
27
28     public void addOnComplete(OnComplete<T> task) {
29         onCompleteTasks.add(task);
30     }
31
32     @SuppressWarnings({ "checkstyle:IllegalCatch", "squid:S1181" /*  Throwable and Error should not be caught */ })
33     protected void notifyOnCompleteTasks(Throwable failure, T result) {
34         for (OnComplete<T> task: onCompleteTasks) {
35             try {
36                 task.onComplete(failure, result);
37             } catch (Throwable e) {
38                 LOG.error("Caught unexpected exception", e);
39             }
40         }
41
42         onCompleteTasks.clear();
43     }
44 }