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

com.remondis.limbus.system.MockObjectFactory Maven / Gradle / Ivy

Go to download

The Limbus System is a small light-weight CDI framework managing the Limbus Core Components. The object graph is represented by an XML configuration file or can be build using the Limbus System API. This module delivers an optional system component that visualizes the object graph and its dependencies after initializing: com.remondis.limbus.system.visualize.LimbusSystemVisualizer This component can be added to the Limbus System. To keep the dependencies of this module transparent and light-weight, the graph renderer is declared as an optional dependency. Add the following dependencies to your project to use the visualisation component: <!-- Graph Stream for Visualization feature This is an optional dependency and only required if using the com.remondis.limbus.system.visualize.LimbusSystemVisualizer --> <dependency> <groupId>org.graphstream</groupId> <artifactId>gs-core</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>org.graphstream</groupId> <artifactId>gs-ui</artifactId> <version>1.3</version> </dependency>

There is a newer version: 3.1.0
Show newest version
package com.remondis.limbus.system;

import java.util.HashMap;
import java.util.Map;

import com.remondis.limbus.IInitializable;
import com.remondis.limbus.utils.ObjectCreateException;

/**
 * This {@link ObjectFactory} can be configured to be used by the {@link LimbusSystem} to inject mock instances of
 * components.
 *
 * @author schuettec
 *
 */
public class MockObjectFactory implements ObjectFactory {

  protected Map>, IInitializable> publicComponents = new HashMap<>();
  protected Map>, IInitializable> privateComponents = new HashMap<>();

  /**
   * Holds the optional delegation object factory. The delegation factory is used if a component instance is requested
   * that is not known by this mock object factory.
   */
  private ObjectFactory delegateObjectFactory;

  public MockObjectFactory() {

  }

  public MockObjectFactory(ObjectFactory objectFactory) {
    this.delegateObjectFactory = objectFactory;
  }

  @Override
  public IInitializable createObject(Class> implementationType)
      throws ObjectCreateException {
    if (hasPrivateObject(implementationType)) {
      return privateComponents.get(implementationType);
    } else {
      if (hasDelegate()) {
        return delegateObjectFactory.createObject(implementationType);
      } else {
        throw new NoSuchComponentException(implementationType);
      }
    }
  }

  @Override
  public IInitializable createObject(Class> requestType,
      Class> implementationType) throws ObjectCreateException {
    if (hasPublicObject(requestType)) {
      return publicComponents.get(requestType);
    } else {
      if (hasDelegate()) {
        return delegateObjectFactory.createObject(requestType, implementationType);
      } else {
        throw new NoSuchComponentException(requestType);
      }
    }

  }

  public void putPublicComponent(Class> requestType, IInitializable instance) {
    publicComponents.put(requestType, instance);
  }

  public void putPrivateComponent(Class> implementationType, IInitializable instance) {
    privateComponents.put(implementationType, instance);
  }

  private boolean hasPublicObject(Class> requestType) {
    return publicComponents.containsKey(requestType);
  }

  private boolean hasPrivateObject(Class> implementationType) {
    return privateComponents.containsKey(implementationType);
  }

  public void removePublicComponent(Class> requestType) {
    publicComponents.remove(requestType);
  }

  public void removePrivateComponent(Class> implementationType) {
    privateComponents.remove(implementationType);
  }

  private boolean hasDelegate() {
    return delegateObjectFactory != null;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy