ProvidedStepsFactory.java

  1. package org.jbehave.core.steps;

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

  3. import java.util.List;

  4. import org.jbehave.core.steps.AbstractStepsFactory.StepsInstanceNotFound;

  5. /**
  6.  * An {@link InjectableStepsFactory} that is provided with the
  7.  * {@link CandidateSteps} instances.
  8.  */
  9. public class ProvidedStepsFactory implements InjectableStepsFactory {

  10.     private final List<CandidateSteps> candidateSteps;

  11.     public ProvidedStepsFactory() {
  12.         this(asList(new CandidateSteps[]{}));
  13.     }

  14.     public ProvidedStepsFactory(List<CandidateSteps> candidateSteps) {
  15.         this.candidateSteps = candidateSteps;
  16.     }

  17.     @Override
  18.     public List<CandidateSteps> createCandidateSteps() {
  19.         return candidateSteps;
  20.     }

  21.     @Override
  22.     public Object createInstanceOfType(Class<?> type) {
  23.         Object instance = null;
  24.         for (CandidateSteps steps : candidateSteps) {
  25.             try {
  26.                 if (type.equals(((Steps) steps).type())) {
  27.                     instance = ((Steps) steps).instance();
  28.                 }
  29.             } catch (RuntimeException e) {
  30.                 // creation failed on given factory, carry on
  31.             }
  32.         }
  33.         if (instance == null) {
  34.             throw new StepsInstanceNotFound(type, this);
  35.         }
  36.         return instance;
  37.     }

  38. }