Commit 12482a00 authored by Jean-Baptiste Nizet's avatar Jean-Baptiste Nizet
Browse files

refactor: add a builder to GeneticResource, also usable in production code

parent 9da5664d
......@@ -2,6 +2,7 @@ package fr.inra.urgi.rare.domain;
import static fr.inra.urgi.rare.util.Utils.nullSafeUnmodifiableCopy;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
......@@ -88,6 +89,29 @@ public class GeneticResource {
this.collectLongitude = collectLongitude;
}
private GeneticResource(Builder builder) {
this(builder.id,
builder.name,
builder.description,
builder.pillarName,
builder.databaseSource,
builder.portalURL,
builder.dataURL,
builder.domain,
builder.taxon,
builder.family,
builder.genus,
builder.species,
builder.materialType,
builder.biotopeType,
builder.countryOfOrigin,
builder.originLatitude,
builder.originLongitude,
builder.countryOfCollect,
builder.collectLatitude,
builder.collectLongitude);
}
public String getId() {
return id;
}
......@@ -248,4 +272,167 @@ public class GeneticResource {
", collectLongitude=" + collectLongitude +
'}';
}
public static Builder builder() {
return new Builder();
}
public static Builder builder(GeneticResource geneticResource) {
return new Builder(geneticResource);
}
public static class Builder {
private String id;
private String name;
private String description;
private String pillarName;
private String databaseSource;
private String portalURL;
private String dataURL;
private String domain;
private List<String> taxon = Collections.emptyList();
private List<String> family = Collections.emptyList();
private List<String> genus = Collections.emptyList();
private List<String> species = Collections.emptyList();
private List<String> materialType = Collections.emptyList();
private List<String> biotopeType = Collections.emptyList();
private String countryOfOrigin;
private Double originLatitude;
private Double originLongitude;
private String countryOfCollect;
private Double collectLatitude;
private Double collectLongitude;
private Builder() {
}
private Builder(GeneticResource geneticResource) {
this.id = geneticResource.getId();
this.name = geneticResource.getName();
this.description = geneticResource.getDescription();
this.pillarName = geneticResource.getPillarName();
this.databaseSource = geneticResource.getDatabaseSource();
this.portalURL = geneticResource.getPortalURL();
this.dataURL = geneticResource.getDataURL();
this.domain = geneticResource.getDomain();
this.taxon = geneticResource.getTaxon();
this.family = geneticResource.getFamily();
this.genus = geneticResource.getGenus();
this.species = geneticResource.getSpecies();
this.materialType = geneticResource.getMaterialType();
this.biotopeType = geneticResource.getBiotopeType();
this.countryOfOrigin = geneticResource.getCountryOfOrigin();
this.originLatitude = geneticResource.getOriginLatitude();
this.originLongitude = geneticResource.getOriginLongitude();
this.countryOfCollect = geneticResource.getCountryOfCollect();
this.collectLatitude = geneticResource.getCollectLatitude();
this.collectLongitude = geneticResource.getCollectLongitude();
}
public Builder withId(String id) {
this.id = id;
return this;
}
public Builder withName(String name) {
this.name = name;
return this;
}
public Builder withDescription(String description) {
this.description = description;
return this;
}
public Builder withPillarName(String pillarName) {
this.pillarName = pillarName;
return this;
}
public Builder withDatabaseSource(String databaseSource) {
this.databaseSource = databaseSource;
return this;
}
public Builder withPortalURL(String portalURL) {
this.portalURL = portalURL;
return this;
}
public Builder withDataURL(String dataURL) {
this.dataURL = dataURL;
return this;
}
public Builder withDomain(String domain) {
this.domain = domain;
return this;
}
public Builder withTaxon(List<String> taxon) {
this.taxon = taxon;
return this;
}
public Builder withFamily(List<String> family) {
this.family = family;
return this;
}
public Builder withGenus(List<String> genus) {
this.genus = genus;
return this;
}
public Builder withSpecies(List<String> species) {
this.species = species;
return this;
}
public Builder withMaterialType(List<String> materialType) {
this.materialType = materialType;
return this;
}
public Builder withBiotopeType(List<String> biotopeType) {
this.biotopeType = biotopeType;
return this;
}
public Builder withCountryOfOrigin(String countryOfOrigin) {
this.countryOfOrigin = countryOfOrigin;
return this;
}
public Builder withOriginLatitude(Double originLatitude) {
this.originLatitude = originLatitude;
return this;
}
public Builder withOriginLongitude(Double originLongitude) {
this.originLongitude = originLongitude;
return this;
}
public Builder withCountryOfCollect(String countryOfCollect) {
this.countryOfCollect = countryOfCollect;
return this;
}
public Builder withCollectLatitude(Double collectLatitude) {
this.collectLatitude = collectLatitude;
return this;
}
public Builder withCollectLongitude(Double collectLongitude) {
this.collectLongitude = collectLongitude;
return this;
}
public GeneticResource build() {
return new GeneticResource(this);
}
}
}
package fr.inra.urgi.rare.domain;
import java.util.Collections;
import java.util.List;
/**
* Builder for {@link GeneticResource}
* @author JB Nizet
*/
public class GeneticResourceBuilder {
private String id;
private String name;
private String description;
private String pillarName;
private String databaseSource;
private String portalURL;
private String dataURL;
private String domain;
private List<String> taxon = Collections.emptyList();
private List<String> family = Collections.emptyList();
private List<String> genus = Collections.emptyList();
private List<String> species = Collections.emptyList();
private List<String> materialType = Collections.emptyList();
private List<String> biotopeType = Collections.emptyList();
private String countryOfOrigin;
private Double originLatitude;
private Double originLongitude;
private String countryOfCollect;
private Double collectLatitude;
private Double collectLongitude;
public GeneticResourceBuilder withId(String id) {
this.id = id;
return this;
}
public GeneticResourceBuilder withName(String name) {
this.name = name;
return this;
}
public GeneticResourceBuilder withDescription(String description) {
this.description = description;
return this;
}
public GeneticResourceBuilder withPillarName(String pillarName) {
this.pillarName = pillarName;
return this;
}
public GeneticResourceBuilder withDatabaseSource(String databaseSource) {
this.databaseSource = databaseSource;
return this;
}
public GeneticResourceBuilder withPortalURL(String portalURL) {
this.portalURL = portalURL;
return this;
}
public GeneticResourceBuilder withDataURL(String dataURL) {
this.dataURL = dataURL;
return this;
}
public GeneticResourceBuilder withDomain(String domain) {
this.domain = domain;
return this;
}
public GeneticResourceBuilder withTaxon(List<String> taxon) {
this.taxon = taxon;
return this;
}
public GeneticResourceBuilder withFamily(List<String> family) {
this.family = family;
return this;
}
public GeneticResourceBuilder withGenus(List<String> genus) {
this.genus = genus;
return this;
}
public GeneticResourceBuilder withSpecies(List<String> species) {
this.species = species;
return this;
}
public GeneticResourceBuilder withMaterialType(List<String> materialType) {
this.materialType = materialType;
return this;
}
public GeneticResourceBuilder withBiotopeType(List<String> biotopeType) {
this.biotopeType = biotopeType;
return this;
}
public GeneticResourceBuilder withCountryOfOrigin(String countryOfOrigin) {
this.countryOfOrigin = countryOfOrigin;
return this;
}
public GeneticResourceBuilder withOriginLatitude(Double originLatitude) {
this.originLatitude = originLatitude;
return this;
}
public GeneticResourceBuilder withOriginLongitude(Double originLongitude) {
this.originLongitude = originLongitude;
return this;
}
public GeneticResourceBuilder withCountryOfCollect(String countryOfCollect) {
this.countryOfCollect = countryOfCollect;
return this;
}
public GeneticResourceBuilder withCollectLatitude(Double collectLatitude) {
this.collectLatitude = collectLatitude;
return this;
}
public GeneticResourceBuilder withCollectLongitude(Double collectLongitude) {
this.collectLongitude = collectLongitude;
return this;
}
public GeneticResource build() {
return new GeneticResource(id,
name,
description,
pillarName,
databaseSource,
portalURL,
dataURL,
domain,
taxon,
family,
genus,
species,
materialType,
biotopeType,
countryOfOrigin,
originLatitude,
originLongitude,
countryOfCollect,
collectLatitude,
collectLongitude);
}
}
......@@ -13,7 +13,7 @@ import org.junit.jupiter.api.Test;
class IndexedGeneticResourceTest {
@Test
public void shouldStoreSuggestions() {
GeneticResource resource = new GeneticResourceBuilder()
GeneticResource resource = GeneticResource.builder()
.withDatabaseSource("databaseResource")
.withFamily(Arrays.asList("family"))
.withName("name")
......
......@@ -26,7 +26,6 @@ import fr.inra.urgi.rare.config.HarvestConfig;
import fr.inra.urgi.rare.config.RareProperties;
import fr.inra.urgi.rare.dao.GeneticResourceDao;
import fr.inra.urgi.rare.domain.GeneticResource;
import fr.inra.urgi.rare.domain.GeneticResourceBuilder;
import fr.inra.urgi.rare.domain.IndexedGeneticResource;
import fr.inra.urgi.rare.harvest.HarvestResult.HarvestResultBuilder;
import org.junit.jupiter.api.BeforeEach;
......@@ -208,7 +207,7 @@ class HarvesterTest {
public void shouldSplitInBatches() throws JsonProcessingException {
List<GeneticResource> geneticResources = new ArrayList<>();
for (int i = 0; i < 250; i++) {
geneticResources.add(new GeneticResourceBuilder().withId("id-" + i).build());
geneticResources.add(GeneticResource.builder().withId("id-" + i).build());
}
byte[] jsonArray = objectMapper.writeValueAsBytes(geneticResources);
......
......@@ -13,7 +13,6 @@ import fr.inra.urgi.rare.dao.GeneticResourceDao;
import fr.inra.urgi.rare.dao.RareAggregation;
import fr.inra.urgi.rare.dao.SearchRefinements;
import fr.inra.urgi.rare.domain.GeneticResource;
import fr.inra.urgi.rare.domain.GeneticResourceBuilder;
import org.elasticsearch.search.aggregations.Aggregations;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
......@@ -42,7 +41,7 @@ class SearchControllerTest {
@Test
void shouldSearch() throws Exception {
GeneticResource resource = new GeneticResourceBuilder()
GeneticResource resource = GeneticResource.builder()
.withId("CFBP 8402")
.withName("CFBP 8402")
.withDescription("Xylella fastidiosa subsp. Pauca, risk group = Quarantine")
......@@ -65,7 +64,7 @@ class SearchControllerTest {
@Test
void shouldSearchAndAggregate() throws Exception {
GeneticResource resource = new GeneticResourceBuilder()
GeneticResource resource = GeneticResource.builder()
.withId("CFBP 8402")
.withName("CFBP 8402")
.withDescription("Xylella fastidiosa subsp. Pauca, risk group = Quarantine")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment