Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / util / OsgiRegistrationUtil.java
index fe8e30ee9ad2816a63f38365fb39c032193dd798..2642e560224c4697c93461c5a91bbc9cd422572e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2013, 2017 Cisco Systems, Inc. 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,
@@ -27,7 +27,8 @@ public class OsgiRegistrationUtil {
     }
 
     @SafeVarargs
-    public static <T> AutoCloseable registerService(final BundleContext bundleContext, final T service, final 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<>();
@@ -40,60 +41,40 @@ 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) {
         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;
     }
 
-    /**
-     * Close list of auto closeables in reverse order
-     */
+    @SuppressWarnings("IllegalCatch")
     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) {
-                        LOG.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 (final 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;
             }
         };
     }