adapted class PlantListModel to PlantDatabase

#12
This commit is contained in:
schrom01 2022-10-24 18:55:26 +02:00
parent f9149c48fd
commit e5b5e1b88a
4 changed files with 57 additions and 23 deletions

View File

@ -56,9 +56,9 @@ public class JsonPlantDatabase implements PlantDatabase {
* @see PlantDatabase#getPlantById(long) * @see PlantDatabase#getPlantById(long)
*/ */
@Override @Override
public Optional<Plant> getPlantById(long id) throws HardinessZoneNotSetException, IOException { public Optional<Plant> getPlantById(HardinessZone zone, long id) throws HardinessZoneNotSetException, IOException {
if (plantMap.isEmpty()) { if (plantMap.isEmpty()) {
loadPlantList(currentZone); loadPlantList(zone);
} }
return Optional.ofNullable(plantMap.get(id)); return Optional.ofNullable(plantMap.get(id));
} }

View File

@ -30,5 +30,5 @@ public interface PlantDatabase {
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified * @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;
} }

View File

@ -1,53 +1,87 @@
package ch.zhaw.gartenverwaltung.plantList; 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 ch.zhaw.gartenverwaltung.types.Plant;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
public class PlantListModel { public class PlantListModel {
private List<Plant> plantList; private final PlantDatabase plantDatabase;
private HardinessZone currentZone;
/** /**
* Comparators to create sorted Plant List * Comparators to create sorted Plant List
*/ */
public final Comparator<Plant> sortByName = (Plant o1, Plant o2) -> o1.name().compareTo(o2.name()); 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> 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() {
public PlantListModel(List<Plant> plantList) { plantDatabase = new JsonPlantDatabase();
setPlantList(plantList);
} }
public void setPlantList(List<Plant> plantList) { public void setCurrentZone(HardinessZone currentZone) {
this.plantList = plantList; this.currentZone = currentZone;
}
public HardinessZone getCurrentZone() {
return currentZone;
} }
/** /**
* Method to get actual Plant List in alphabetic Order * Method to get actual Plant List in alphabetic Order
* @return actual Plant List in alphabetic Order * @return actual Plant List in alphabetic Order
*/ */
public List<Plant> getPlantList() { public List<Plant> getPlantList(HardinessZone zone) throws HardinessZoneNotSetException, IOException {
return getSortedPlantList(sortByName); 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 * Method to get Filtered plant list
* @param comparator comparator Object which is used to sort list * @param predicate predicate to filter the list
* @return actual Plant List in custom order * @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) { public List<Plant> getFilteredPlantList(HardinessZone zone, Predicate<Plant> predicate) throws HardinessZoneNotSetException, IOException {
return plantList.stream().sorted(comparator).toList(); setCurrentZone(zone);
return getPlantList(zone).stream().filter(predicate).toList();
} }
/** /**
* Method to get filtered Plant List with custom filter * Method to get Filtered plant list by id by exact match
* @param predicate predicate for filter * @param zone selected hardiness zone
* @return filtered Plant List * @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) { public List<Plant> getFilteredPlantListById(HardinessZone zone, Long id) throws HardinessZoneNotSetException, IOException {
return plantList.stream().filter(predicate).toList(); setCurrentZone(zone);
List<Plant> plantList = new ArrayList<>();
plantDatabase.getPlantById(zone, id).ifPresent(plantList::add);
return plantList;
} }
} }

View File

@ -251,4 +251,4 @@
} }
] ]
} }
] ]