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

autofixture.generators.composition.DefaultGeneratorsPipeline Maven / Gradle / Ivy

package autofixture.generators.composition;

import autofixture.exceptions.ObjectCreationException;
import autofixture.interfaces.FixtureContract;
import autofixture.interfaces.GeneratorsPipeline;
import autofixture.interfaces.InstanceGenerator;
import autofixture.interfaces.InstanceType;

import java.util.List;

public class DefaultGeneratorsPipeline implements GeneratorsPipeline {

  private final List generators;
  private int customizationsCount = 0;
  private boolean autoPropertiesDisabled = false;

  public DefaultGeneratorsPipeline(final List generators) {
    this.generators = generators;
  }

  @Override
  public  T generateInstanceOf(final InstanceType instanceType, final FixtureContract fixture) {
    for (final InstanceGenerator generator : generators) {
      if (generator.appliesTo(instanceType)) {
        return generator.next(instanceType, fixture);
      }
    }
    throw new ObjectCreationException(instanceType);
  }

  @Override
  public  T generateEmptyInstanceOf(final InstanceType instanceType, final FixtureContract fixture) {
    //todo some dupl. between here and generateFor...
    for (final InstanceGenerator generator : generators) {
      if (generator.appliesTo(instanceType)) {
        return generator.nextEmpty(instanceType, fixture);
      }
    }
    throw new ObjectCreationException(instanceType);
  }

  @Override
  public void registerCustomization(final InstanceGenerator instanceGenerator) {
    generators.add(0, instanceGenerator);
    instanceGenerator.setOmittingAutoProperties(this.autoPropertiesDisabled);
    customizationsCount++;
  }

  @Override
  public void clearCustomizations() {
    for (int i = 0; i < customizationsCount; ++i) {
      generators.remove(0);
    }
    customizationsCount = 0;
  }

  @Override
  public void setOmittingAutoProperties(final boolean isOn) {
    autoPropertiesDisabled = isOn;
    for (final InstanceGenerator generator : generators) {
      generator.setOmittingAutoProperties(isOn);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy