Skip to content

Commit

Permalink
Merge ad5de31 into 1850b03
Browse files Browse the repository at this point in the history
  • Loading branch information
bianzheng123 committed May 25, 2020
2 parents 1850b03 + ad5de31 commit 4c28f3c
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 27 deletions.
65 changes: 55 additions & 10 deletions src/main/java/com/github/javafaker/Country.java
@@ -1,40 +1,85 @@
package com.github.javafaker;

/**
* Use to generate information of a same country.
* @author 韩霄
* @date 2020/5/18
*/
public class Country {
private final Faker faker;
private final String flagUrl;
private final String[] countryTotal;

protected Country(Faker faker) {
this.faker = faker;
this.flagUrl = "http://flags.fmcdn.net/data/flags/w580/";
this.countryTotal = getCountryTotal().split("/");
}

/**
* Randomly fetch a total country information which seperated by "/"
* For example, ramdomly fetch "au/aus/Australia/Canberra/Australian Dollar/AUD"
* from country.yml
* @return a String, 5 "/"s and 6 information about a same country.
*/
private String getCountryTotal(){
return faker.fakeValuesService().resolve("country.total", this, faker);
}

/**
* generate a URL of the graph of the country's flag.
* @return a URL
*/
public String flag() {
return flagUrl + faker.fakeValuesService().resolve("country.code2", this, faker) + ".png";
return flagUrl + countryTotal[0] + ".png";
}

/**
* Generates a two-digit abbreviation of a country name
* @return a String of 2-bits.
*/
public String countryCode2() {
return faker.fakeValuesService().resolve("country.code2", this, faker);
return countryTotal[0];
}

/**
* Generates a 3-digit abbreviation of a country name
* @return a String of 3-bits.
*/
public String countryCode3() {
return faker.fakeValuesService().resolve("country.code3", this, faker);
return countryTotal[1];
}

/**
* Generates a total country name
* @return a String of name.
*/
public String name() {
return countryTotal[2];
}

/**
* Generates the name of capital of the country we just generated.
* @return a String of capital name.
*/
public String capital() {
return faker.fakeValuesService().resolve("country.capital", this, faker);
return countryTotal[3];
}

/**
* Generates the currency of the country we just generated.
* @return a String currency name.
*/
public String currency() {
return faker.fakeValuesService().resolve("country.currency", this, faker);
return countryTotal[4];
}

/**
* Generates the currency abbreviation of the country we just generated.
* @return a String currency abbreviation.
*/
public String currencyCode() {
return faker.fakeValuesService().resolve("country.currency_code", this, faker);
}

public String name() {
return faker.fakeValuesService().resolve("country.name", this, faker);
return countryTotal[5];
}

}
31 changes: 20 additions & 11 deletions src/main/java/com/github/javafaker/Number.java
Expand Up @@ -28,9 +28,13 @@ public int randomDigitNotZero() {
*/
public int numberBetween(int min, int max) {
if (min == max) return min;

int value = decimalBetween(min,max).setScale(0, BigDecimal.ROUND_HALF_DOWN).intValue();
return value == max ? value - 1 : value;
else if (min > max){
int tmp = max;
max = min;
min = tmp;
}
int value = decimalBetween(min,max).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
return value >= max ? value - 1 : value;
}

/**
Expand All @@ -43,9 +47,13 @@ public int numberBetween(int min, int max) {
*/
public long numberBetween(long min, long max) {
if (min == max) return min;

long value = decimalBetween(min, max).setScale(0, BigDecimal.ROUND_HALF_DOWN).longValue();
return value == max ? value - 1 : value;
else if (min > max){
long tmp = max;
max = min;
min = tmp;
}
long value = decimalBetween(min, max).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
return value >= max ? value - 1 : value;
}

/**
Expand Down Expand Up @@ -101,12 +109,13 @@ private BigDecimal decimalBetween(long min, long max) {
final double range = (double) trueMax - (double) trueMin;

final double chunkCount = Math.sqrt(Math.abs(range));
final double chunkSize = chunkCount;
final long randomChunk = faker.random().nextLong((long) chunkCount);
final long decimalChunkCount=(long)Math.ceil(chunkCount);
final long randomChunk = faker.random().nextLong(decimalChunkCount);

final double chunkStart = trueMin + randomChunk * chunkCount;

final double chunkStart = trueMin + randomChunk * chunkSize;
final double adj = chunkSize * faker.random().nextDouble();
return new BigDecimal(chunkStart + adj);
final double adj = chunkCount * faker.random().nextDouble();
return chunkStart + adj >= trueMax ? new BigDecimal(trueMax -1) :new BigDecimal(chunkStart + adj);
}

public String digits(int count) {
Expand Down

0 comments on commit 4c28f3c

Please sign in to comment.