InstanceStepsFactory.java

  1. package org.jbehave.core.steps;

  2. import static java.util.Arrays.asList;

  3. import java.util.ArrayList;
  4. import java.util.LinkedHashMap;
  5. import java.util.List;
  6. import java.util.Map;

  7. import org.jbehave.core.configuration.Configuration;

  8. /**
  9.  * An {@link InjectableStepsFactory} that is provided Object instances.
  10.  */
  11. public class InstanceStepsFactory extends AbstractStepsFactory {

  12.     private final Map<Class<?>,Object> stepsInstances = new LinkedHashMap<>();

  13.     public InstanceStepsFactory(Configuration configuration, Object... stepsInstances) {
  14.         this(configuration, asList(stepsInstances));
  15.     }

  16.     public InstanceStepsFactory(Configuration configuration, List<?> stepsInstances) {
  17.         super(configuration);
  18.         for (Object instance : stepsInstances) {
  19.             this.stepsInstances.put(instance.getClass(), instance);
  20.         }
  21.     }

  22.     @Override
  23.     protected List<Class<?>> stepsTypes() {
  24.         return new ArrayList<>(stepsInstances.keySet());
  25.     }

  26.     @Override
  27.     public Object createInstanceOfType(Class<?> type) {
  28.         Object instance = stepsInstances.get(type);
  29.         if (instance == null) {
  30.             throw new StepsInstanceNotFound(type, this);
  31.         }
  32.         return instance;
  33.     }

  34. }