Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / util / InterfacesHelper.java
index a6e9b1ba965b8532eb2e688742ca6ec66c5d5e10..059ba45ec9b79956fa4a3f07a0adad98888d6a3c 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,
@@ -7,28 +7,30 @@
  */
 package org.opendaylight.controller.config.manager.impl.util;
 
-import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
-import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
-import org.opendaylight.controller.config.spi.Module;
-import org.opendaylight.controller.config.spi.ModuleFactory;
-
-import javax.management.JMX;
+import com.google.common.collect.ImmutableSet;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
+import javax.management.JMX;
+import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
+import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
+import org.opendaylight.controller.config.spi.Module;
+import org.opendaylight.controller.config.spi.ModuleFactory;
+
+public final class InterfacesHelper {
 
-public class InterfacesHelper {
+    private InterfacesHelper() {
+    }
 
     public static Set<Class<?>> getAllInterfaces(Class<?> clazz) {
         if (clazz.isInterface()) {
-            throw new IllegalArgumentException(clazz
-                    + " should not be an interface");
+            throw new IllegalArgumentException(clazz + " should not be an interface");
         }
         // getInterfaces gets interfaces implemented directly by this class
         Set<Class<?>> toBeInspected = new HashSet<>();
-        while (clazz.equals(Object.class) == false) {
+        while (!clazz.equals(Object.class)) {
             toBeInspected.addAll(Arrays.asList(clazz.getInterfaces()));
             // get parent class
             clazz = clazz.getSuperclass();
@@ -37,18 +39,18 @@ public class InterfacesHelper {
 
     }
 
-    private static Set<Class<?>> getAllSuperInterfaces(Set<Class<?>> ifcs) {
-        ifcs = new HashSet<>(ifcs); // create copy to modify
+    private static Set<Class<?>> getAllSuperInterfaces(final Set<? extends Class<?>> ifcs) {
+        Set<Class<?>> interfaces = new HashSet<>(ifcs); // create copy to modify
         // each interface can extend other interfaces
         Set<Class<?>> result = new HashSet<>();
-        while (ifcs.size() > 0) {
-            Iterator<Class<?>> iterator = ifcs.iterator();
+        while (!interfaces.isEmpty()) {
+            Iterator<Class<?>> iterator = interfaces.iterator();
             Class<?> ifc = iterator.next();
             iterator.remove();
-            if (ifc.isInterface() == false)  {
+            if (!ifc.isInterface()) {
                 throw new IllegalArgumentException(ifc + " should be an interface");
             }
-            ifcs.addAll(Arrays.asList(ifc.getInterfaces()));
+            interfaces.addAll(Arrays.asList(ifc.getInterfaces()));
             result.add(ifc);
         }
         return result;
@@ -56,9 +58,12 @@ public class InterfacesHelper {
 
     /**
      * Get interfaces that this class is derived from that are JMX interfaces.
+     *
+     * @param configBeanClass
+     *            config bean class
+     * @return set containing classes
      */
-    public static Set<Class<?>> getMXInterfaces(
-            Class<? extends Module> configBeanClass) {
+    public static Set<Class<?>> getMXInterfaces(final Class<? extends Module> configBeanClass) {
         Set<Class<?>> allInterfaces = getAllInterfaces(configBeanClass);
         Set<Class<?>> result = new HashSet<>();
         for (Class<?> clazz : allInterfaces) {
@@ -73,15 +78,17 @@ public class InterfacesHelper {
      * Get all implemented interfaces that have
      * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation}
      * annotation.
+     *
+     * @param configBeanClass
+     *            config bean class
+     * @return set containing classes
      */
-    public static Set<Class<?>> getServiceInterfaces(
-            Class<? extends Module> configBeanClass) {
+    public static Set<Class<?>> getServiceInterfaces(final Class<? extends Module> configBeanClass) {
         Set<Class<?>> allInterfaces = getAllInterfaces(configBeanClass);
         Set<Class<?>> result = new HashSet<>();
         for (Class<?> clazz : allInterfaces) {
             if (AbstractServiceInterface.class.isAssignableFrom(clazz)) {
-                ServiceInterfaceAnnotation annotation = clazz
-                        .getAnnotation(ServiceInterfaceAnnotation.class);
+                ServiceInterfaceAnnotation annotation = clazz.getAnnotation(ServiceInterfaceAnnotation.class);
                 if (annotation != null) {
                     result.add(clazz);
                 }
@@ -90,53 +97,58 @@ public class InterfacesHelper {
         return result;
     }
 
-    public static Set<Class<? extends AbstractServiceInterface>> getAllAbstractServiceClasses(Class<? extends Module> configBeanClass) {
+    public static Set<Class<? extends AbstractServiceInterface>> getAllAbstractServiceClasses(
+            final Class<? extends Module> configBeanClass) {
 
         Set<Class<? extends AbstractServiceInterface>> foundGeneratedSIClasses = new HashSet<>();
         for (Class<?> clazz : getAllInterfaces(configBeanClass)) {
-            if (AbstractServiceInterface.class.isAssignableFrom(clazz) && AbstractServiceInterface.class.equals(clazz) == false) {
+            if (AbstractServiceInterface.class.isAssignableFrom(clazz)
+                    && !AbstractServiceInterface.class.equals(clazz)) {
                 foundGeneratedSIClasses.add((Class<? extends AbstractServiceInterface>) clazz);
             }
         }
         return getAllAbstractServiceInterfaceClasses(foundGeneratedSIClasses);
     }
 
-
     /**
      * Get OSGi registration types under which config bean instance should be
      * registered. This is specified in
      * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation#osgiRegistrationType()}
+     *
+     * @param configBeanClass config bean class
+     * @return set of classes
      */
-    public static Set<Class<?>> getOsgiRegistrationTypes(
-            Class<? extends Module> configBeanClass) {
-        // TODO test with service interface hierarchy
+    public static Set<Class<?>> getOsgiRegistrationTypes(final Class<? extends Module> configBeanClass) {
         Set<Class<?>> serviceInterfaces = getServiceInterfaces(configBeanClass);
         Set<Class<?>> result = new HashSet<>();
         for (Class<?> clazz : serviceInterfaces) {
-            ServiceInterfaceAnnotation annotation = clazz
-                    .getAnnotation(ServiceInterfaceAnnotation.class);
+            ServiceInterfaceAnnotation annotation = clazz.getAnnotation(ServiceInterfaceAnnotation.class);
             result.add(annotation.osgiRegistrationType());
         }
         return result;
     }
 
-    public static Set<String> getQNames(Set<ServiceInterfaceAnnotation> siAnnotations) {
-        Set<String> qNames = new HashSet<>();
-        for (ServiceInterfaceAnnotation sia: siAnnotations) {
-            qNames.add(sia.value());
+    public static Set<String> getQNames(final Set<ServiceInterfaceAnnotation> siAnnotations) {
+        Set<String> names = new HashSet<>();
+        for (ServiceInterfaceAnnotation sia : siAnnotations) {
+            names.add(sia.value());
         }
-        return Collections.unmodifiableSet(qNames);
+        return ImmutableSet.copyOf(names);
     }
 
-    public static Set<ServiceInterfaceAnnotation> getServiceInterfaceAnnotations(ModuleFactory factory) {
-        Set<Class<? extends AbstractServiceInterface>> implementedServiceIntefaces = Collections.unmodifiableSet(factory.getImplementedServiceIntefaces());
+    public static Set<ServiceInterfaceAnnotation> getServiceInterfaceAnnotations(final ModuleFactory factory) {
+        Set<Class<? extends AbstractServiceInterface>> implementedServiceIntefaces = Collections
+                .unmodifiableSet(factory.getImplementedServiceIntefaces());
         return getServiceInterfaceAnnotations(implementedServiceIntefaces);
     }
 
-    private static Set<ServiceInterfaceAnnotation> getServiceInterfaceAnnotations(Set<Class<? extends AbstractServiceInterface>> implementedServiceIntefaces) {
-        Set<Class<? extends AbstractServiceInterface>> inspected = getAllAbstractServiceInterfaceClasses(implementedServiceIntefaces);
+    private static Set<ServiceInterfaceAnnotation> getServiceInterfaceAnnotations(
+            final Set<Class<? extends AbstractServiceInterface>> implementedServiceIntefaces) {
+        Set<Class<? extends AbstractServiceInterface>> inspected = getAllAbstractServiceInterfaceClasses(
+                implementedServiceIntefaces);
         Set<ServiceInterfaceAnnotation> result = new HashSet<>();
-        // SIs can form hierarchies, inspect superclass until it does not extend AbstractSI
+        // SIs can form hierarchies, inspect superclass until it does not extend
+        // AbstractSI
         for (Class<?> clazz : inspected) {
             ServiceInterfaceAnnotation annotation = clazz.getAnnotation(ServiceInterfaceAnnotation.class);
             if (annotation != null) {
@@ -147,16 +159,14 @@ public class InterfacesHelper {
     }
 
     static Set<Class<? extends AbstractServiceInterface>> getAllAbstractServiceInterfaceClasses(
-            Set<Class<? extends AbstractServiceInterface>> directlyImplementedAbstractSIs) {
+            final Set<Class<? extends AbstractServiceInterface>> directlyImplementedAbstractSIs) {
 
-        Set<Class<?>> allInterfaces = getAllSuperInterfaces((Set) directlyImplementedAbstractSIs);
+        Set<Class<?>> allInterfaces = getAllSuperInterfaces(directlyImplementedAbstractSIs);
         Set<Class<? extends AbstractServiceInterface>> result = new HashSet<>();
-        for(Class<?> ifc: allInterfaces){
-            if (AbstractServiceInterface.class.isAssignableFrom(ifc) &&
-                    ifc.equals(AbstractServiceInterface.class) == false) {
+        for (Class<?> ifc : allInterfaces) {
+            if (AbstractServiceInterface.class.isAssignableFrom(ifc) && !ifc.equals(AbstractServiceInterface.class)) {
                 result.add((Class<? extends AbstractServiceInterface>) ifc);
             }
-
         }
         return result;
     }