adapted class PlantListModel to PlantDatabase
This commit is contained in:
parent
f9149c48fd
commit
d1c5c58468
|
@ -56,9 +56,9 @@ public class JsonPlantDatabase implements PlantDatabase {
|
|||
* @see PlantDatabase#getPlantById(long)
|
||||
*/
|
||||
@Override
|
||||
public Optional<Plant> getPlantById(long id) throws HardinessZoneNotSetException, IOException {
|
||||
public Optional<Plant> getPlantById(HardinessZone zone, long id) throws HardinessZoneNotSetException, IOException {
|
||||
if (plantMap.isEmpty()) {
|
||||
loadPlantList(currentZone);
|
||||
loadPlantList(zone);
|
||||
}
|
||||
return Optional.ofNullable(plantMap.get(id));
|
||||
}
|
||||
|
|
|
@ -30,5 +30,5 @@ public interface PlantDatabase {
|
|||
* @throws IOException If the database cannot be accessed
|
||||
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
|
||||
*/
|
||||
Optional<Plant> getPlantById(long id) throws IOException, HardinessZoneNotSetException;
|
||||
Optional<Plant> getPlantById(HardinessZone zone, long id) throws IOException, HardinessZoneNotSetException;
|
||||
}
|
||||
|
|
|
@ -1,53 +1,87 @@
|
|||
package ch.zhaw.gartenverwaltung.plantList;
|
||||
|
||||
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
|
||||
import ch.zhaw.gartenverwaltung.io.JsonPlantDatabase;
|
||||
import ch.zhaw.gartenverwaltung.io.PlantDatabase;
|
||||
import ch.zhaw.gartenverwaltung.types.HardinessZone;
|
||||
import ch.zhaw.gartenverwaltung.types.Plant;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class PlantListModel {
|
||||
private List<Plant> plantList;
|
||||
private final PlantDatabase plantDatabase;
|
||||
private HardinessZone currentZone;
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
|
||||
public PlantListModel(List<Plant> plantList) {
|
||||
setPlantList(plantList);
|
||||
public PlantListModel() {
|
||||
plantDatabase = new JsonPlantDatabase();
|
||||
}
|
||||
|
||||
public void setPlantList(List<Plant> plantList) {
|
||||
this.plantList = plantList;
|
||||
public void setCurrentZone(HardinessZone currentZone) {
|
||||
this.currentZone = currentZone;
|
||||
}
|
||||
|
||||
public HardinessZone getCurrentZone() {
|
||||
return currentZone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get actual Plant List in alphabetic Order
|
||||
* @return actual Plant List in alphabetic Order
|
||||
*/
|
||||
public List<Plant> getPlantList() {
|
||||
return getSortedPlantList(sortByName);
|
||||
public List<Plant> getPlantList(HardinessZone zone) throws HardinessZoneNotSetException, IOException {
|
||||
setCurrentZone(zone);
|
||||
return getSortedPlantList(zone, sortByName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Method to get the actual Plant list in custom Order
|
||||
* @param zone selected hardiness zone
|
||||
* @param comparator comparator to sort the list
|
||||
* @return sorted list with plants in the given hardiness zone
|
||||
* @throws IOException If the database cannot be accessed
|
||||
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
|
||||
*/
|
||||
public List<Plant> getSortedPlantList(HardinessZone zone, Comparator<Plant> comparator) throws HardinessZoneNotSetException, IOException {
|
||||
setCurrentZone(zone);
|
||||
return plantDatabase.getPlantList(zone).stream().sorted(comparator).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* Method to get Filtered plant list
|
||||
* @param predicate predicate to filter the list
|
||||
* @param zone selected hardiness zone
|
||||
* @return filterd list with plants in the hardinness zone
|
||||
* @throws IOException If the database cannot be accessed
|
||||
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
|
||||
*/
|
||||
public List<Plant> getSortedPlantList(Comparator<Plant> comparator) {
|
||||
return plantList.stream().sorted(comparator).toList();
|
||||
public List<Plant> getFilteredPlantList(HardinessZone zone, Predicate<Plant> predicate) throws HardinessZoneNotSetException, IOException {
|
||||
setCurrentZone(zone);
|
||||
return getPlantList(zone).stream().filter(predicate).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get filtered Plant List with custom filter
|
||||
* @param predicate predicate for filter
|
||||
* @return filtered Plant List
|
||||
* Method to get Filtered plant list by id by exact match
|
||||
* @param zone selected hardiness zone
|
||||
* @param id id of plant
|
||||
* @return if id doesn't exist: empty List, else list with 1 plant entry.
|
||||
* @throws IOException If the database cannot be accessed
|
||||
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
|
||||
*/
|
||||
public List<Plant> getFilteredPlantList(Predicate<Plant> predicate) {
|
||||
return plantList.stream().filter(predicate).toList();
|
||||
public List<Plant> getFilteredPlantListById(HardinessZone zone, Long id) throws HardinessZoneNotSetException, IOException {
|
||||
setCurrentZone(zone);
|
||||
List<Plant> plantList = new ArrayList<>();
|
||||
plantDatabase.getPlantById(zone, id).ifPresent(plantList::add);
|
||||
return plantList;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -251,4 +251,4 @@
|
|||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue