implemented Methods to sort and filter PlantList
This commit is contained in:
parent
1e4dd07879
commit
7dd157b9d5
|
@ -0,0 +1,45 @@
|
||||||
|
package ch.zhaw.gartenverwaltung.plantList;
|
||||||
|
|
||||||
|
import ch.zhaw.gartenverwaltung.types.Plant;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
public class PlantListModel {
|
||||||
|
private List<Plant> plantList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comparators to create sorted Plant List
|
||||||
|
*/
|
||||||
|
public final Comparator<Plant> sortByName = (Plant o1, Plant o2) -> o1.name().compareTo(o2.name());
|
||||||
|
public final Comparator<Plant> getSortById = (Plant o1, Plant o2) -> Long.compare(o1.id(), o2.id());
|
||||||
|
public final Comparator<Plant> sortBySpacing = (Plant o1, Plant o2) -> o1.spacing() - o2.spacing();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Functions to get Plant Attribute as String
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPlantList(List<Plant> plantList) {
|
||||||
|
this.plantList = plantList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Plant> getPlantList() {
|
||||||
|
return getSortedPlantList(sortByName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Plant> getSortedPlantList(Comparator<Plant> comparator) {
|
||||||
|
return plantList.stream().sorted(comparator).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Plant> getFilteredList(String filterString, Function<Plant, String> filterFunction) {
|
||||||
|
return plantList.stream().filter(plant -> filterFunction.apply(plant).contains(filterString)).toList();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue