All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.k2.core.ModuleContext Maven / Gradle / Ivy

There is a newer version: 0.1.13
Show newest version
/* vim: set et ts=2 sw=2 cindent fo=qroca: */

package com.k2.core;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/** The context that k2 presents to a module during the registration phase.
 *
 * Modules use this class to look for other modules registration
 * implementations.
 */
public class ModuleContext {

  /** The module definition of the module that k2 asks to register its
   * information, never null.
   */
  private ModuleDefinition definition;

  /** The module definitions of all the modules in the application, never
   * null.
   */
  private Collection modules;

  /** A cache of the registries requested by the modules.
   *
   * K2 guarantees that a module is not asked more that once for a registry for
   * a specific requestor.
   *
   * This is never null.
   */
  private Map, Object> registries = new LinkedHashMap<>();

  /** Constructor, creates a module context for a specific module.
   *
   * @param theModules the list of modules in the application. It cannot be
   * null.
   *
   * @param theDefinition the module definition wrapped in this module
   * context. It cannot be null.
   */
  ModuleContext(final ModuleDefinition theDefinition,
      final Collection theModules) {
    definition = theDefinition;
    modules = theModules;
  }

  /** Obtains a registry of a specific type.
   *
   * @param  the type of the registry to return.
   *
   * @param registryType the type of registry to find.
   *
   * @return an implementation of the provided type, or null if none found.
   */
  @SuppressWarnings("unchecked")
  public  T get(final Class registryType) {

    T registry = (T) registries.get(registryType);

    if (registry == null) {
      for (ModuleDefinition definitionForProvider : modules) {
        Object configurer;
        configurer = definitionForProvider.getRegistry(definition);
        if (registryType.isInstance(configurer)) {
          registry = (T) configurer;
          registries.put(registryType, registry);
          break;
        }
      }
    }
    return registry;
  }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy