Skip to content

Commit

Permalink
Merge pull request #47 from Codearte/custom_DataMaster_fixed
Browse files Browse the repository at this point in the history
Fixed custom DataMaster implementation.
  • Loading branch information
OlgaMaciaszek committed Aug 9, 2015
2 parents dac6a10 + 35cb3cd commit b9ddc7c
Show file tree
Hide file tree
Showing 14 changed files with 336 additions and 184 deletions.
87 changes: 67 additions & 20 deletions src/main/java/io/codearte/jfairy/Bootstrap.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package io.codearte.jfairy;

import com.google.common.base.Optional;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Provider;
import io.codearte.jfairy.data.DataMaster;
import io.codearte.jfairy.data.DataMasterModule;
import io.codearte.jfairy.data.MapBasedDataMaster;
import io.codearte.jfairy.producer.util.LanguageCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.IOException;
import java.util.Locale;
Expand All @@ -27,27 +34,35 @@
* actually take effect.
*
* @author Jakub Kubrynski
* @author Olga Maciaszek-Sharma
*/
public class Bootstrap {

private static final Logger LOG = LoggerFactory.getLogger(Bootstrap.class);

private static final String DATA_FILE_PREFIX = "jfairy";

public static Fairy createFairy(Locale locale, String filePrefix, Random random) {
public static Fairy createFairy(DataMaster dataMaster, Locale locale, Random random) {


FairyModule fairyModule = getFairyModuleForLocale(locale, random);

FairyModule fairyModule = getFairyModuleForLocale(dataMaster, locale, random);

Injector injector = Guice.createInjector(fairyModule);

DataMaster dataMaster = injector.getInstance(DataMaster.class);
FairyFactory fairyFactory = injector.getInstance(FairyFactory.class);

return fairyFactory.createFairy();
}


private static void fillDefaultDataMaster(MapBasedDataMaster dataMaster, Locale locale, String filePrefix) {
try {
dataMaster.readResources(filePrefix + ".yml");
dataMaster.readResources(filePrefix + "_" + locale.getLanguage() + ".yml");
} catch (IOException e) {
throw new IllegalStateException(e);
}

FairyFactory instance = injector.getInstance(FairyFactory.class);
return instance.createFairy(locale, filePrefix);
}

/**
Expand Down Expand Up @@ -94,13 +109,41 @@ public static Fairy create(Locale locale, String dataFilePrefix) {
.build();
}


public static Fairy create(Provider<DataMaster> dataMaster, Locale locale) {
return builder().withDataMasterProvider(dataMaster).withLocale(locale).build();
}

private static FairyModule getFairyModuleForLocale(DataMaster dataMaster, Locale locale, Random random) {
LanguageCode code = LanguageCode.valueOf(locale.getLanguage().toUpperCase());
switch (code) {
case PL:
return new PlFairyModule(dataMaster, random);
case EN:
return new EnFairyModule(dataMaster, random);
case ES:
return new EsFairyModule(dataMaster, random);
default:
LOG.info("No data for your language - using EN");
return new EnFairyModule(dataMaster, random);
}
}

public static class Builder {

private Locale locale = Locale.ENGLISH;
private String filePrefix = DATA_FILE_PREFIX;
private Random random = new Random();
private DataMaster dataMaster;


private MapBasedDataMaster getDefaultDataMaster() {
Injector injector = Guice.createInjector(new DataMasterModule(random));
return injector.getInstance(MapBasedDataMaster.class);
}

private Builder() {

}

/**
Expand Down Expand Up @@ -148,27 +191,31 @@ public Builder withRandomSeed(long randomSeed) {
return this;
}

/**
* Sets a custom DataMaster implementation.
*
* @param dataMasterProvider The random seed to use.
* @return the same Builder (for chaining).
*/
public Builder withDataMasterProvider(Provider<DataMaster> dataMasterProvider) {
this.dataMaster = dataMasterProvider.get();
return this;
}


/**
* Returns the completed Fairy.
*
* @return Fairy instance
*/
public Fairy build() {
return createFairy(locale, filePrefix, random);
if (dataMaster == null) {
dataMaster = getDefaultDataMaster();
fillDefaultDataMaster((MapBasedDataMaster) dataMaster, locale, filePrefix);
}
return createFairy(dataMaster, locale, random);
}
}


private static FairyModule getFairyModuleForLocale(Locale locale, Random random) {
LanguageCode code = LanguageCode.valueOf(locale.getLanguage().toUpperCase());
switch (code) {
case PL:
return new PlFairyModule(random);
case EN:
return new EnFairyModule(random);
case ES:
return new EsFairyModule(random);
default:
return new EnFairyModule(random);
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/io/codearte/jfairy/EnFairyModule.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.codearte.jfairy;

import io.codearte.jfairy.data.DataMaster;
import io.codearte.jfairy.producer.VATIdentificationNumberProvider;
import io.codearte.jfairy.producer.company.locale.en.EmployerIdentificationNumberProvider;
import io.codearte.jfairy.producer.person.NationalIdentityCardNumberProvider;
Expand All @@ -17,8 +18,8 @@
*/
public class EnFairyModule extends FairyModule {

public EnFairyModule(Random random) {
super(random);
public EnFairyModule(DataMaster dataMaster, Random random) {
super(dataMaster, random);
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/codearte/jfairy/EsFairyModule.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.codearte.jfairy;

import io.codearte.jfairy.data.DataMaster;
import io.codearte.jfairy.producer.VATIdentificationNumberProvider;
import io.codearte.jfairy.producer.company.locale.es.CIFProvider;
import io.codearte.jfairy.producer.person.NationalIdentityCardNumberProvider;
Expand All @@ -17,8 +18,8 @@
*/
public class EsFairyModule extends FairyModule {

public EsFairyModule(Random random) {
super(random);
public EsFairyModule(DataMaster dataMaster, Random random) {
super(dataMaster, random);
}

@Override
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/io/codearte/jfairy/Fairy.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package io.codearte.jfairy;


import com.google.inject.Provider;
import io.codearte.jfairy.data.DataMaster;
import io.codearte.jfairy.producer.BaseProducer;
import io.codearte.jfairy.producer.DateProducer;
import io.codearte.jfairy.producer.company.Company;
Expand All @@ -16,7 +18,6 @@
import io.codearte.jfairy.producer.text.TextProducer;

import javax.inject.Inject;
import javax.inject.Provider;
import java.util.Locale;

public final class Fairy {
Expand Down Expand Up @@ -49,6 +50,10 @@ public static Fairy create(Locale locale) {
return Bootstrap.create(locale);
}

public static Fairy create(Provider<DataMaster> dataMasterProvider, Locale locale) {
return Bootstrap.create(dataMasterProvider, locale);
}


public static Bootstrap.Builder builder() {
return Bootstrap.builder();
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/io/codearte/jfairy/FairyFactory.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package io.codearte.jfairy;

import java.util.Locale;

/**
* @author Jakub Kubrynski
*/
interface FairyFactory {

Fairy createFairy(Locale locale, String filePrefix);
Fairy createFairy();
}
8 changes: 7 additions & 1 deletion src/main/java/io/codearte/jfairy/FairyModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.inject.AbstractModule;
import com.google.inject.assistedinject.FactoryModuleBuilder;
import io.codearte.jfairy.data.DataMaster;
import io.codearte.jfairy.producer.company.CompanyFactory;
import io.codearte.jfairy.producer.payment.IBANFactory;
import io.codearte.jfairy.producer.person.PersonFactory;
Expand All @@ -11,18 +12,23 @@

/**
* @author jkubrynski@gmail.com
* @author Olga Maciaszek-Sharma
* @since 2013-11-15
*/
public abstract class FairyModule extends AbstractModule {

private final Random random;

public FairyModule(Random random) {
private final DataMaster dataMaster;

public FairyModule(DataMaster dataMaster, Random random) {
this.random = random;
this.dataMaster = dataMaster;
}

@Override
protected void configure() {
bind(DataMaster.class).toInstance(dataMaster);
bind(Random.class).toInstance(random);

install(new FactoryModuleBuilder().build(PersonFactory.class));
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/codearte/jfairy/PlFairyModule.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.codearte.jfairy;

import io.codearte.jfairy.data.DataMaster;
import io.codearte.jfairy.producer.VATIdentificationNumberProvider;
import io.codearte.jfairy.producer.company.locale.pl.NIPProvider;
import io.codearte.jfairy.producer.person.NationalIdentityCardNumberProvider;
Expand All @@ -17,8 +18,8 @@
*/
public class PlFairyModule extends FairyModule {

public PlFairyModule(Random random) {
super(random);
public PlFairyModule(DataMaster dataMaster, Random random) {
super(dataMaster, random);
}

@Override
Expand Down
Loading

0 comments on commit b9ddc7c

Please sign in to comment.