impl: ldp remove ap using predicate

This commit is contained in:
Václav Přibík
2025-10-23 21:20:33 +02:00
parent ff10df17c3
commit 457804d251
5 changed files with 67 additions and 22 deletions

View File

@@ -28,8 +28,7 @@ public class APFactory implements AbstractAPFactory {
@Override
public LocalDayPlan createLocalDayPlan(LocalDay day, Instant start, Instant end) {
// TODO Return an instance of your class that implements LocalDayPlan
return null;
return new LocalDayPlanImpl(day, start, end);
}
@Override

View File

@@ -12,6 +12,13 @@ public class AppointmentImpl implements Appointment {
private AppointmentRequest request;
public AppointmentImpl(Instant start, AppointmentRequest request) {
this.start = start;
this.request = request;
this.stop = start.plus(request.duration());
}
@Override
public Instant start() {
return start;

View File

@@ -3,6 +3,7 @@ package appointmentplanner;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalTime;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
@@ -15,11 +16,12 @@ import appointmentplanner.api.LocalDayPlan;
import appointmentplanner.api.TimePreference;
import appointmentplanner.api.TimeSlot;
import appointmentplanner.customlist.CustomLinkedListImpl;
import appointmentplanner.customlist.CustomListToJavaBinding;
import appointmentplanner.customlist.api.CustomLinkedList;
public class LocalDayPlanImpl implements LocalDayPlan {
private CustomLinkedList<AppointmentImpl> timeline = new CustomLinkedListImpl<>();
private CustomLinkedList<Appointment> timeline = new CustomLinkedListImpl<>();
public LocalDayPlanImpl(LocalDay day, Instant start, Instant end) {
this.day = day;
@@ -55,8 +57,7 @@ public class LocalDayPlanImpl implements LocalDayPlan {
@Override
public Optional<Appointment> addAppointment(AppointmentData appointmentData, LocalTime startTime) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'addAppointment'");
throw new UnsupportedOperationException();
}
@Override
@@ -67,20 +68,26 @@ public class LocalDayPlanImpl implements LocalDayPlan {
@Override
public AppointmentRequest removeAppointment(Appointment appointment) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'removeAppointment'");
Appointment removedItem = timeline.remove(appointment);
return removedItem == null ? null : removedItem.request();
}
@Override
public List<AppointmentRequest> removeAppointments(Predicate<Appointment> filter) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'removeAppointments'");
CustomLinkedList<AppointmentRequest> removedRequests = new CustomLinkedListImpl<>();
for (Appointment appointment : timeline.removeFound(filter)) {
removedRequests.add(appointment.request());
}
return new CustomListToJavaBinding<>(removedRequests);
}
@Override
public List<Appointment> appointments() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'appointments'");
return new CustomListToJavaBinding<Appointment>(timeline);
}
@Override
@@ -119,20 +126,17 @@ public class LocalDayPlanImpl implements LocalDayPlan {
@Override
public List<Appointment> findAppointments(Predicate<Appointment> filter) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'findAppointments'");
return new CustomListToJavaBinding<>(timeline.find(filter));
}
@Override
public boolean contains(Appointment appointment) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'contains'");
return timeline.contains(appointment);
}
@Override
public int nrOfAppointments() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'nrOfAppointments'");
return timeline.size();
}
@Override

View File

@@ -1,6 +1,7 @@
package appointmentplanner.customlist;
import java.util.Iterator;
import java.util.function.Predicate;
import appointmentplanner.customlist.api.CustomLinkedList;
@@ -84,31 +85,32 @@ public class CustomLinkedListImpl<T> implements CustomLinkedList<T> {
}
@Override
public void remove(T item) {
public T remove(T item) {
if (head == null) {
return;
return null;
}
if (head.getItem().equals(item)) {
head = head.getNext();
return;
return item;
}
CustomLinkedListNode<T> beforeNode = traverseFind(head, item, ItemPosition.BEFORE);
if (beforeNode == null) {
return;
return item;
}
CustomLinkedListNode<T> nodeToRemove = beforeNode.getNext();
if (beforeNode.getNext() == null) {
beforeNode.setNext(null);
return;
return item;
}
beforeNode.setNext(nodeToRemove.getNext());
return item;
}
@Override
@@ -172,4 +174,34 @@ public class CustomLinkedListImpl<T> implements CustomLinkedList<T> {
return recursiveSizeCalc(this.head, 1);
}
private CustomLinkedList<T> traverseFilter(Predicate<T> filter, CustomLinkedListNode<T> node,
CustomLinkedList<T> found) {
if (node == null) {
return found;
}
T item = node.getItem();
if (filter.test(item)) {
found.add(item);
}
return traverseFilter(filter, node.getNext(), found);
}
@Override
public CustomLinkedList<T> find(Predicate<T> filter) {
return traverseFilter(filter, head, new CustomLinkedListImpl<>());
}
@Override
public CustomLinkedList<T> removeFound(Predicate<T> filter) {
CustomLinkedList<T> items = find(filter);
for (T item : items) {
remove(item);
}
return items;
}
}

View File

@@ -17,6 +17,9 @@ public class CustomLinkedListIterator<T> implements Iterator<T> {
@Override
public boolean hasNext() {
if (lastNode == null) {
return false;
}
return lastNode.getNext() != null;
}