refactor: remove exception-based control-flow
This commit is contained in:
parent
60c6dcd0d9
commit
ad05e9e95a
|
@ -116,16 +116,17 @@ 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 {
|
||||
String caseInsensitiveSearchString = searchString.toLowerCase();
|
||||
return getFilteredPlantList(zone, plant ->
|
||||
plant.name().toLowerCase().contains(caseInsensitiveSearchString) ||
|
||||
plant.description().toLowerCase().contains(caseInsensitiveSearchString)
|
||||
plant.name().toLowerCase().contains(caseInsensitiveSearchString) ||
|
||||
plant.description().toLowerCase().contains(caseInsensitiveSearchString)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -169,15 +170,24 @@ 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
|
||||
* @param to the lastest date for the filter
|
||||
* @return List of Plants with selected saison
|
||||
* @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 {
|
||||
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]+");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue