Skip to content

Commit

Permalink
Merge pull request #17 from dhis2/TECH-1449
Browse files Browse the repository at this point in the history
fix: Implement readable toString for PredictableRandomizer [TECH-1449]
  • Loading branch information
enricocolasante authored Oct 17, 2022
2 parents abfcd22 + 5f2d1bc commit 18bf4e8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
19 changes: 8 additions & 11 deletions src/main/java/org/hisp/dhis/utils/PredictableRandomizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ public class PredictableRandomizer implements Randomizer {

private final Faker faker;

private final long seed;

public PredictableRandomizer( long seed )
{
this.seed = seed;
this.rnd = new Random( seed );
this.faker = new Faker( rnd );
}
Expand Down Expand Up @@ -79,12 +82,6 @@ public <T> List<T> randomElementsFromList( List<T> list, int size )
return resultList;
}

@Override
public String randomString()
{
return this.faker.programmingLanguage().name();
}

public String randomString( int size )
{
return this.faker.lorem().characters( size, true, false );
Expand Down Expand Up @@ -132,11 +129,6 @@ public String randomNationalId() {
return this.faker.idNumber().valid();
}

@Override
public Date randomAdultBirthday() {
return this.faker.date().birthday( 18, 80 );
}

@Override
public Date randomBirthday(int minAge, int maxAge){
return this.faker.date().birthday( minAge, maxAge );
Expand Down Expand Up @@ -191,4 +183,9 @@ private LocalDate toLocalDate( Date dateToConvert )
{
return dateToConvert.toInstant().atZone( ZoneId.systemDefault() ).toLocalDate();
}

@Override
public String toString() {
return "PredictableRandomizer("+ this.seed +")";
}
}
10 changes: 7 additions & 3 deletions src/main/java/org/hisp/dhis/utils/Randomizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ default <T> T randomElementFromList( List<T> list )
*
* @return a String
*/
String randomString();
default String randomString(){
return randomString(randomInt(20));
}

/**
* Returns random string of alphabetical characters.
Expand Down Expand Up @@ -125,7 +127,9 @@ default <T> T randomElementFromList( List<T> list )
*
* @return a birthday date
*/
Date randomAdultBirthday();
default Date randomAdultBirthday(){
return randomBirthday(18, 80);
};

/**
* Generates random birthday date with age
Expand All @@ -141,7 +145,7 @@ default <T> T randomElementFromList( List<T> list )
* @param wordCount number of words in the text
* @return a long text
*/
String randomLongText(int wordCount );
String randomLongText( int wordCount );

/**
* Generates random phone number
Expand Down

0 comments on commit 18bf4e8

Please sign in to comment.