refactor: remove exception-based control-flow

This commit is contained in:
David Guler 2022-11-08 07:31:04 +01:00
parent 60c6dcd0d9
commit ad05e9e95a
1 changed files with 18 additions and 8 deletions

View File

@ -116,9 +116,10 @@ public class PlantListModel {
if (searchString.length() == 0) {
return getPlantList(zone);
} else if (searchString.charAt(0) == '#') {
try {
return getFilteredPlantListById(zone, Long.parseLong(searchString.substring(1)));
} catch (NumberFormatException e) {
if (isPositiveIntegral(searchString.substring(1))) {
Long searchId = Long.parseLong(searchString.substring(1));
return getFilteredPlantListById(zone, searchId);
} else {
return new ArrayList<>();
}
} else {
@ -169,7 +170,6 @@ public class PlantListModel {
}
/**
*
* @param zone selected hardiness zone
* @param from the earliest date to for the filter
* @param to the lastest date for the filter
@ -180,4 +180,14 @@ public class PlantListModel {
public List<Plant> getFilteredPlantListBySaisonWithoutGrowthPhase(HardinessZone zone, MonthDay from, MonthDay to) throws HardinessZoneNotSetException, IOException {
return getFilteredPlantList(zone, plant -> plant.lifecycle().stream().anyMatch(growthPhase -> growthPhase.startDate().compareTo(from) >= 0 && (growthPhase.startDate().compareTo(to) <= 0)));
}
/**
* Check if a string can safely be parsed as a positive Integral value (short/int/long)
*
* @param subject The string to be tested
* @return Whether the string contains only digits
*/
private boolean isPositiveIntegral(String subject) {
return subject != null && subject.matches("[0-9]+");
}
}