Moved null-check to loading method

This commit is contained in:
David Guler 2022-10-24 12:32:28 +02:00
parent 2c61cd3393
commit 0d24bcc2ad
1 changed files with 4 additions and 7 deletions

View File

@ -46,9 +46,6 @@ public class JsonPlantDatabase implements PlantDatabase {
*/ */
@Override @Override
public List<Plant> getPlantList(HardinessZone zone) throws IOException, HardinessZoneNotSetException { public List<Plant> getPlantList(HardinessZone zone) throws IOException, HardinessZoneNotSetException {
if (zone == null) {
throw new HardinessZoneNotSetException();
}
if (plantMap.isEmpty() || zone != currentZone) { if (plantMap.isEmpty() || zone != currentZone) {
loadPlantList(zone); loadPlantList(zone);
} }
@ -60,9 +57,6 @@ public class JsonPlantDatabase implements PlantDatabase {
*/ */
@Override @Override
public Optional<Plant> getPlantById(long id) throws HardinessZoneNotSetException, IOException { public Optional<Plant> getPlantById(long id) throws HardinessZoneNotSetException, IOException {
if (currentZone == null) {
throw new HardinessZoneNotSetException();
}
if (plantMap.isEmpty()) { if (plantMap.isEmpty()) {
loadPlantList(currentZone); loadPlantList(currentZone);
} }
@ -75,7 +69,10 @@ public class JsonPlantDatabase implements PlantDatabase {
* @param zone The {@link HardinessZone} for which data is to be loaded * @param zone The {@link HardinessZone} for which data is to be loaded
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
private void loadPlantList(HardinessZone zone) throws IOException { private void loadPlantList(HardinessZone zone) throws IOException, HardinessZoneNotSetException {
if (zone == null) {
throw new HardinessZoneNotSetException();
}
if (dataSource != null) { if (dataSource != null) {
currentZone = zone; currentZone = zone;
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();