package singleton.builder;

/**
 * This class implements a factory using the Singleton design pattern.
 *
 * @author fpereira
 *
 */
public final class BuilderFactory {

  /**
   * The instance that we must return.
   */
  private static BuilderFactory instance;

  static {
    instance = null;
  }

  /**
   * Private constructor to avoid instantiation.
   */
  private BuilderFactory() { }

  /**
   * This method gives back an instance of the BuilderFactory.
   * @return a BuilderFactory object that is unique.
   */
  static public BuilderFactory getInstance() {
    if (instance == null) {
      instance = new BuilderFactory();
    }
    return instance;
  }

  /**
   * This method returns a builder.
   * @param bName the tag used to identify the builder.
   * @return an instance of the Builder class.
   * @throws NoBuilderException in case the tag does not identify any valid
   * builder.
   */
  public Builder getBuilder(final String bName) throws NoBuilderException {
    if (bName.equals("necklace")) {
      return new NecklaceBuilder();
    } else if (bName.equals("treasure")) {
      return new TreasureBuilder();
    } else {
      throw new NoBuilderException(bName + " is not a valid builder");
    }
  }
}
