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

br.com.objectos.way.etc.AbstractEtcModule Maven / Gradle / Ivy

The newest version!
/*
 * ObjectosComunsYamlModel.java criado em 04/09/2012
 * 
 * Propriedade de Objectos Fábrica de Software LTDA.
 * Reprodução parcial ou total proibida.
 */
package br.com.objectos.way.etc;

import java.io.File;

import org.yaml.snakeyaml.Yaml;

import br.com.objectos.way.core.io.Directory;

import com.google.common.base.Preconditions;
import com.google.inject.AbstractModule;
import com.google.inject.multibindings.MapBinder;

/**
 * @author [email protected] (Marcio Endo)
 */
public abstract class AbstractEtcModule extends AbstractModule {

  private MapBinder modelBinder;

  @Override
  protected final void configure() {
    modelBinder = MapBinder.newMapBinder(binder(), EtcKey.class, Etc.class);

    bind(Yaml.class).toProvider(YamlProvider.class);

    configureEtc();
  }

  protected abstract void configureEtc();

  protected  EtcBuilder etc(Class config) {
    return new Builder(config);
  }

  protected interface EtcBuilder {

    interface LoaderBuilder {

      void loadWith(Class> loader);

      void loadWith(EtcLoader instance);

    }

    LoaderBuilder at(File file);

    LoaderBuilder atDir(Directory dir);

  }

  private class Builder implements
      EtcBuilder,
      EtcBuilder.LoaderBuilder {

    private final Class type;

    private File file;

    public Builder(Class type) {
      this.type = Preconditions.checkNotNull(type);
    }

    @Override
    public LoaderBuilder at(File file) {
      this.file = file;
      return this;
    }

    @Override
    public LoaderBuilder atDir(Directory dir) {
      String typeName = type.getSimpleName();
      String name = String.format("%s.yaml", typeName.toLowerCase());
      this.file = dir.fileAt(name);
      return this;
    }

    @Override
    public void loadWith(Class> loader) {
      Preconditions.checkNotNull(loader);
      EtcLoaderWrapper wrapper = EtcLoaderWrapper.forType(loader);
      Etc etc = new Etc(type, file, wrapper);
      modelBinder.addBinding(new EtcKey(type)).toInstance(etc);
    }

    @Override
    public void loadWith(EtcLoader loader) {
      Preconditions.checkNotNull(loader);
      EtcLoaderWrapper wrapper = EtcLoaderWrapper.forInstance(loader);
      Etc etc = new Etc(type, file, wrapper);
      modelBinder.addBinding(new EtcKey(type)).toInstance(etc);
    }

  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy