implemented Methods to sort and filter PlantList
This commit is contained in:
parent
7dd157b9d5
commit
5b0e472ec7
|
@ -23,6 +23,7 @@ public class PlantListModel {
|
|||
public final Function<Plant, String> filterByName = Plant::name;
|
||||
public final Function<Plant, String> filterById = plant -> Long.toString(plant.id());
|
||||
|
||||
|
||||
public PlantListModel(List<Plant> plantList) {
|
||||
setPlantList(plantList);
|
||||
}
|
||||
|
@ -31,15 +32,30 @@ public class PlantListModel {
|
|||
this.plantList = plantList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get actual Plant List in alphabetic Order
|
||||
* @return actual Plant List in alphabetic Order
|
||||
*/
|
||||
public List<Plant> getPlantList() {
|
||||
return getSortedPlantList(sortByName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get actual Plant List sorted in custom order
|
||||
* @param comparator comparator Object which is used to sort list
|
||||
* @return actual Plant List in custom order
|
||||
*/
|
||||
public List<Plant> getSortedPlantList(Comparator<Plant> comparator) {
|
||||
return plantList.stream().sorted(comparator).toList();
|
||||
}
|
||||
|
||||
public List<Plant> getFilteredList(String filterString, Function<Plant, String> filterFunction) {
|
||||
/**
|
||||
* Method to get filtered Plant List with custom filter
|
||||
* @param filterString String to search for plants
|
||||
* @param filterFunction Function Object to get Plant attribute as String which must contain filter String
|
||||
* @return filtered Plant List
|
||||
*/
|
||||
public List<Plant> getFilteredPlantListbyString(String filterString, Function<Plant, String> filterFunction) {
|
||||
return plantList.stream().filter(plant -> filterFunction.apply(plant).contains(filterString)).toList();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue