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,16 +116,17 @@ public class PlantListModel {
if (searchString.length() == 0) { if (searchString.length() == 0) {
return getPlantList(zone); return getPlantList(zone);
} else if (searchString.charAt(0) == '#') { } else if (searchString.charAt(0) == '#') {
try { if (isPositiveIntegral(searchString.substring(1))) {
return getFilteredPlantListById(zone, Long.parseLong(searchString.substring(1))); Long searchId = Long.parseLong(searchString.substring(1));
} catch (NumberFormatException e) { return getFilteredPlantListById(zone, searchId);
} else {
return new ArrayList<>(); return new ArrayList<>();
} }
} else { } else {
String caseInsensitiveSearchString = searchString.toLowerCase(); String caseInsensitiveSearchString = searchString.toLowerCase();
return getFilteredPlantList(zone, plant -> return getFilteredPlantList(zone, plant ->
plant.name().toLowerCase().contains(caseInsensitiveSearchString) || plant.name().toLowerCase().contains(caseInsensitiveSearchString) ||
plant.description().toLowerCase().contains(caseInsensitiveSearchString) plant.description().toLowerCase().contains(caseInsensitiveSearchString)
); );
} }
} }
@ -169,15 +170,24 @@ public class PlantListModel {
} }
/** /**
*
* @param zone selected hardiness zone * @param zone selected hardiness zone
* @param from the earliest date to for the filter * @param from the earliest date to for the filter
* @param to the lastest date for the filter * @param to the lastest date for the filter
* @return List of Plants with selected saison * @return List of Plants with selected saison
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
public List<Plant> getFilteredPlantListBySaisonWithoutGrowthPhase(HardinessZone zone, MonthDay from, MonthDay to) throws HardinessZoneNotSetException, IOException { 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))); 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]+");
}
} }