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 @Override
public LocalDayPlan createLocalDayPlan(LocalDay day, Instant start, Instant end) { public LocalDayPlan createLocalDayPlan(LocalDay day, Instant start, Instant end) {
// TODO Return an instance of your class that implements LocalDayPlan return new LocalDayPlanImpl(day, start, end);
return null;
} }
@Override @Override

View File

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

View File

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

View File

@@ -1,6 +1,7 @@
package appointmentplanner.customlist; package appointmentplanner.customlist;
import java.util.Iterator; import java.util.Iterator;
import java.util.function.Predicate;
import appointmentplanner.customlist.api.CustomLinkedList; import appointmentplanner.customlist.api.CustomLinkedList;
@@ -84,31 +85,32 @@ public class CustomLinkedListImpl<T> implements CustomLinkedList<T> {
} }
@Override @Override
public void remove(T item) { public T remove(T item) {
if (head == null) { if (head == null) {
return; return null;
} }
if (head.getItem().equals(item)) { if (head.getItem().equals(item)) {
head = head.getNext(); head = head.getNext();
return; return item;
} }
CustomLinkedListNode<T> beforeNode = traverseFind(head, item, ItemPosition.BEFORE); CustomLinkedListNode<T> beforeNode = traverseFind(head, item, ItemPosition.BEFORE);
if (beforeNode == null) { if (beforeNode == null) {
return; return item;
} }
CustomLinkedListNode<T> nodeToRemove = beforeNode.getNext(); CustomLinkedListNode<T> nodeToRemove = beforeNode.getNext();
if (beforeNode.getNext() == null) { if (beforeNode.getNext() == null) {
beforeNode.setNext(null); beforeNode.setNext(null);
return; return item;
} }
beforeNode.setNext(nodeToRemove.getNext()); beforeNode.setNext(nodeToRemove.getNext());
return item;
} }
@Override @Override
@@ -172,4 +174,34 @@ public class CustomLinkedListImpl<T> implements CustomLinkedList<T> {
return recursiveSizeCalc(this.head, 1); 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 @Override
public boolean hasNext() { public boolean hasNext() {
if (lastNode == null) {
return false;
}
return lastNode.getNext() != null; return lastNode.getNext() != null;
} }