Skip to content

Commit

Permalink
Merge pull request Devskiller#35 from graux/master
Browse files Browse the repository at this point in the history
Support for Spanish / Spain
  • Loading branch information
jkubrynski committed May 14, 2015
2 parents 6b735a3 + 6526b77 commit 65f482f
Show file tree
Hide file tree
Showing 11 changed files with 402 additions and 141 deletions.
278 changes: 140 additions & 138 deletions src/main/java/io/codearte/jfairy/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,142 +30,144 @@
*/
public class Bootstrap {

private static final String DATA_FILE_PREFIX = "jfairy";

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

FairyModule fairyModule = getFairyModuleForLocale(locale, random);

Injector injector = Guice.createInjector(fairyModule);

DataMaster dataMaster = injector.getInstance(DataMaster.class);
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);
}

/**
* Creates a Builder that will let you configure a Fairy's fields one by one.
*
* @return a Builder instance.
*/
public static Builder builder() {
return new Builder();
}


/**
* Use this factory method to create dataset containing default jfairy.yml and jfairy_{langCode}.yml files
* merged with custom files with the same name
*
* @return Fairy instance
*/
public static Fairy create() {
return builder().build();
}

/**
* Use this factory method to create dataset containing default jfairy.yml and jfairy_{langCode}.yml files
* merged with custom files with the same name
*
* @param locale will be used to assess langCode for data file
* @return Fairy instance
*/
public static Fairy create(Locale locale) {
return builder().withLocale(locale).build();
}

/**
* Use this factory method to create your own dataset overriding bundled one
*
* @param locale will be used to assess langCode for data file
* @param dataFilePrefix prefix of the data file - final pattern will be jfairy.yml and dataFilePrefix_{langCode}.yml
* @return Fairy instance
*/
public static Fairy create(Locale locale, String dataFilePrefix) {
return builder().withLocale(locale)
.withFilePrefix(dataFilePrefix)
.build();
}

public static class Builder {

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

private Builder() {
}

/**
* Sets the locale for the resulting Fairy.
*
* @param locale The Locale to set.
* @return the same Builder (for chaining).
*/
public Builder withLocale(Locale locale) {
this.locale = locale;
return this;
}

/**
* Sets the data file prefix for the resulting Fairy.
*
* @param filePrefix The prefix of the file (such as "jfairy" for "jfairy_en.yml").
* @return the same Builder (for chaining).
*/
public Builder withFilePrefix(String filePrefix) {
this.filePrefix = filePrefix;
return this;
}

/**
* Sets the Random object to use to pick things randomly.
*
* @param random The Random to use.
* @return the same Builder (for chaining).
*/
public Builder withRandom(Random random) {
this.random = random;
return this;
}

/**
* Sets the random seed to use to pick things randomly. (If you set this, you will always
* get the same result when you generate things.)
*
* @param randomSeed The random seed to use.
* @return the same Builder (for chaining).
*/
public Builder withRandomSeed(long randomSeed) {
this.random = new Random(randomSeed);
return this;
}

/**
* Returns the completed Fairy.
*/
public Fairy build() {
return createFairy(locale, filePrefix, 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);
default:
return new EnFairyModule(random);
}
}
private static final String DATA_FILE_PREFIX = "jfairy";

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

FairyModule fairyModule = getFairyModuleForLocale(locale, random);

Injector injector = Guice.createInjector(fairyModule);

DataMaster dataMaster = injector.getInstance(DataMaster.class);
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);
}

/**
* Creates a Builder that will let you configure a Fairy's fields one by one.
*
* @return a Builder instance.
*/
public static Builder builder() {
return new Builder();
}


/**
* Use this factory method to create dataset containing default jfairy.yml and jfairy_{langCode}.yml files
* merged with custom files with the same name
*
* @return Fairy instance
*/
public static Fairy create() {
return builder().build();
}

/**
* Use this factory method to create dataset containing default jfairy.yml and jfairy_{langCode}.yml files
* merged with custom files with the same name
*
* @param locale will be used to assess langCode for data file
* @return Fairy instance
*/
public static Fairy create(Locale locale) {
return builder().withLocale(locale).build();
}

/**
* Use this factory method to create your own dataset overriding bundled one
*
* @param locale will be used to assess langCode for data file
* @param dataFilePrefix prefix of the data file - final pattern will be jfairy.yml and dataFilePrefix_{langCode}.yml
* @return Fairy instance
*/
public static Fairy create(Locale locale, String dataFilePrefix) {
return builder().withLocale(locale)
.withFilePrefix(dataFilePrefix)
.build();
}

public static class Builder {

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

private Builder() {
}

/**
* Sets the locale for the resulting Fairy.
*
* @param locale The Locale to set.
* @return the same Builder (for chaining).
*/
public Builder withLocale(Locale locale) {
this.locale = locale;
return this;
}

/**
* Sets the data file prefix for the resulting Fairy.
*
* @param filePrefix The prefix of the file (such as "jfairy" for "jfairy_en.yml").
* @return the same Builder (for chaining).
*/
public Builder withFilePrefix(String filePrefix) {
this.filePrefix = filePrefix;
return this;
}

/**
* Sets the Random object to use to pick things randomly.
*
* @param random The Random to use.
* @return the same Builder (for chaining).
*/
public Builder withRandom(Random random) {
this.random = random;
return this;
}

/**
* Sets the random seed to use to pick things randomly. (If you set this, you will always
* get the same result when you generate things.)
*
* @param randomSeed The random seed to use.
* @return the same Builder (for chaining).
*/
public Builder withRandomSeed(long randomSeed) {
this.random = new Random(randomSeed);
return this;
}

/**
* Returns the completed Fairy.
*/
public Fairy build() {
return createFairy(locale, filePrefix, 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);
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/io/codearte/jfairy/EsFairyModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.codearte.jfairy;

import io.codearte.jfairy.producer.VATIdentificationNumberProvider;
import io.codearte.jfairy.producer.company.locale.es.CIFProvider;
import io.codearte.jfairy.producer.person.NationalIdentityCardNumberProvider;
import io.codearte.jfairy.producer.person.PassportNumberProvider;
import io.codearte.jfairy.producer.person.locale.es.DNINumberProvider;
import io.codearte.jfairy.producer.person.locale.es.EsPassportNumberProvider;

import java.util.Random;

/**
* @author graux
* @since 26.04.15
*/
public class EsFairyModule extends FairyModule {

public EsFairyModule(Random random) {
super(random);
}

@Override
protected void configure() {
super.configure();
bind(NationalIdentityCardNumberProvider.class).to(DNINumberProvider.class);
bind(VATIdentificationNumberProvider.class).to(CIFProvider.class);
bind(PassportNumberProvider.class).to(EsPassportNumberProvider.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.codearte.jfairy.producer.company.locale.es;

import io.codearte.jfairy.producer.VATIdentificationNumberProvider;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.regex.Pattern;

/**
* @author graux
* @since 26/04/2015
* Código de Identificación Fiscal (CIF) Español
*/
public class CIFProvider implements VATIdentificationNumberProvider {

private static final String REGEX_CIF = "^[A-Z][0-9]{2}[0-9]{5}([KPQSABEH]|[0-9]|[A-Z])$";
private Pattern regexCif;

public CIFProvider() {
this.regexCif = Pattern.compile(REGEX_CIF);
}

@Override
public String get() {
return String.format("%s%s%s", RandomStringUtils.randomAlphabetic(1).toUpperCase(), RandomStringUtils.randomNumeric(7), RandomStringUtils.randomAlphanumeric(1).toUpperCase());
}

public boolean isValid(String cif) {
return this.regexCif.matcher(cif).matches();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.codearte.jfairy.producer.person.locale.es;

import io.codearte.jfairy.producer.person.NationalIdentityCardNumberProvider;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.regex.Pattern;

/**
* @author graux
* @since 26/04/2015
* Documento Nacional de Identidad (DNI) Español
*/
public class DNINumberProvider implements NationalIdentityCardNumberProvider {

private static final String REGEX_DNI = "^\\d{8}([-]?)[A-Z]$";
private Pattern regexDni;

public DNINumberProvider() {
this.regexDni = Pattern.compile(REGEX_DNI);
}

@Override
public String get() {
return String.format("%s-%s", RandomStringUtils.randomNumeric(8), RandomStringUtils.randomAlphabetic(1).toUpperCase());
}

public boolean isValid(String dni) {
return this.regexDni.matcher(dni).matches();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.codearte.jfairy.producer.person.locale.es;

import io.codearte.jfairy.producer.person.PassportNumberProvider;
import org.apache.commons.lang3.RandomStringUtils;

/**
* @author graux
* @since 26/04/2015
*/
public class EsPassportNumberProvider implements PassportNumberProvider {
@Override
public String get() {
return RandomStringUtils.randomAlphanumeric(9);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
*/
public enum LanguageCode {
PL,
EN
EN,
ES
}

0 comments on commit 65f482f

Please sign in to comment.