content: /*
   *    Copyright (C) 2017 MINDORKS NEXTGEN PRIVATE LIMITED
   *
   *    Licensed under the Apache License, Version 2.0 (the "License");
   *    you may not use this file except in compliance with the License.
   *    You may obtain a copy of the License at
   *
   *        http://www.apache.org/licenses/LICENSE-2.0
   *
   *    Unless required by applicable law or agreed to in writing, software
   *    distributed under the License is distributed on an "AS IS" BASIS,
   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *    See the License for the specific language governing permissions and
   *    limitations under the License.
   */
  
  package com.mindorks.nybus.finder;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  /**
   * Created by amitshekhar on 30/08/17.
   */
  
  /**
   * The implementation of {@link EventClassFinder}.
   */
  public class NYEventClassFinder implements EventClassFinder {
  
      private final Map<Class<?>, List<Class<?>>> eventTypesCache;
  
      public NYEventClassFinder() {
          eventTypesCache = new HashMap<>();
      }
  
      /**
       * Get all interfaces and superclasses associated with the event class.
       *
       * @param eventClass the event class.
       * @return list of class associated with event class.
       */
      @Override
      public List<Class<?>> getAll(Class<?> eventClass) {
          synchronized (eventTypesCache) {
              List<Class<?>> eventTypes = eventTypesCache.get(eventClass);
              if (eventTypes == null) {
                  eventTypes = new ArrayList<>();
                  Class<?> clazz = eventClass;
                  while (clazz != null) {
                      eventTypes.add(clazz);
                      addInterfaces(eventTypes, clazz.getInterfaces());
                      clazz = clazz.getSuperclass();
                  }
                  eventTypesCache.put(eventClass, eventTypes);
              }
              return eventTypes;
          }
      }
  
      /**
       * Add associated interfaces for the class.
       *
       * @param eventTypes list of events.
       * @param interfaces the interface.
       */
      private void addInterfaces(List<Class<?>> eventTypes, Class<?>[] interfaces) {
          for (Class<?> interfaceClass : interfaces) {
              if (!eventTypes.contains(interfaceClass)) {
                  eventTypes.add(interfaceClass);
                  addInterfaces(eventTypes, interfaceClass.getInterfaces());
              }
          }
      }
  
  }
  