Fixed few sonar warnings.
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / util / OsgiRegistrationUtil.java
index 2df28f0a154b8b7a6e37f1930b5e06d46efda675..7c02019544977b8e33d4ecd08ad91d2ab6af3642 100644 (file)
@@ -9,7 +9,6 @@
 package org.opendaylight.controller.config.manager.impl.util;
 
 import static com.google.common.base.Preconditions.checkNotNull;
-
 import java.util.ArrayList;
 import java.util.List;
 import java.util.ListIterator;
@@ -21,10 +20,13 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class OsgiRegistrationUtil {
-    private static final Logger logger = LoggerFactory.getLogger(OsgiRegistrationUtil.class);
+    private static final Logger LOG = LoggerFactory.getLogger(OsgiRegistrationUtil.class);
+
+    private OsgiRegistrationUtil() {
+    }
 
     @SafeVarargs
-    public static <T> AutoCloseable registerService(BundleContext bundleContext, T service, Class<? super T> ... interfaces) {
+    public static <T> AutoCloseable registerService(final BundleContext bundleContext, final T service, final Class<? super T> ... interfaces) {
         checkNotNull(service);
         checkNotNull(interfaces);
         List<AutoCloseable> autoCloseableList = new ArrayList<>();
@@ -37,32 +39,17 @@ public class OsgiRegistrationUtil {
 
     public static AutoCloseable wrap(final ServiceRegistration<?> reg) {
         checkNotNull(reg);
-        return new AutoCloseable() {
-            @Override
-            public void close() throws Exception {
-                reg.unregister();
-            }
-        };
+        return reg::unregister;
     }
 
-    public static AutoCloseable wrap(final BundleTracker bundleTracker) {
+    public static AutoCloseable wrap(final BundleTracker<?> bundleTracker) {
         checkNotNull(bundleTracker);
-        return new AutoCloseable() {
-            @Override
-            public void close() throws Exception {
-                bundleTracker.close();
-            }
-        };
+        return bundleTracker::close;
     }
 
     public static AutoCloseable wrap(final ServiceTracker<?, ?> serviceTracker) {
         checkNotNull(serviceTracker);
-        return new AutoCloseable() {
-            @Override
-            public void close() throws Exception {
-                serviceTracker.close();
-            }
-        };
+        return serviceTracker::close;
     }
 
     /**
@@ -71,26 +58,23 @@ public class OsgiRegistrationUtil {
     public static AutoCloseable aggregate(final List<? extends AutoCloseable> list) {
         checkNotNull(list);
 
-        return new AutoCloseable() {
-            @Override
-            public void close() throws Exception {
-                Exception firstException = null;
-                for (ListIterator<? extends AutoCloseable> it = list.listIterator(list.size()); it.hasPrevious();) {
-                    AutoCloseable ac = it.previous();
-                    try {
-                        ac.close();
-                    } catch (Exception e) {
-                        logger.warn("Exception while closing {}", ac, e);
-                        if (firstException == null) {
-                            firstException = e;
-                        } else {
-                            firstException.addSuppressed(e);
-                        }
+        return () -> {
+            Exception firstException = null;
+            for (ListIterator<? extends AutoCloseable> it = list.listIterator(list.size()); it.hasPrevious();) {
+                AutoCloseable ac = it.previous();
+                try {
+                    ac.close();
+                } catch (Exception e) {
+                    LOG.warn("Exception while closing {}", ac, e);
+                    if (firstException == null) {
+                        firstException = e;
+                    } else {
+                        firstException.addSuppressed(e);
                     }
                 }
-                if (firstException != null) {
-                    throw firstException;
-                }
+            }
+            if (firstException != null) {
+                throw firstException;
             }
         };
     }