Fix removing objects from lists while iterating over them

This commit is contained in:
Sebastian 2023-10-28 18:30:14 +02:00
parent 00a15e91ea
commit 1dd01d0249
2 changed files with 20 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package core.entities.timemanager;
import javax.persistence.*; import javax.persistence.*;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Objects;
@Entity @Entity
@Table(name = "basic_schedules") @Table(name = "basic_schedules")
@ -79,4 +80,17 @@ public class BasicTaskSchedule {
public boolean isActivateAble() { public boolean isActivateAble() {
return startTime == null; return startTime == null;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BasicTaskSchedule that = (BasicTaskSchedule) o;
return scheduleID == that.scheduleID;
}
@Override
public int hashCode() {
return Objects.hash(scheduleID);
}
} }

View File

@ -13,6 +13,7 @@ import org.springframework.stereotype.Service;
import java.time.Duration; import java.time.Duration;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -61,14 +62,15 @@ public class TaskScheduleService {
} }
List<BasicTaskSchedule> basicTaskSchedules = basicTaskScheduleRepository.findAllByUserAndDate(user.get(), LocalDate.now()); List<BasicTaskSchedule> basicTaskSchedules = basicTaskScheduleRepository.findAllByUserAndDate(user.get(), LocalDate.now());
List<BasicTaskSchedule> activatableSchedules = new LinkedList<>();
if(onlyActivateable) { if(onlyActivateable) {
for(int i=0; i<basicTaskSchedules.size(); i++) { for (BasicTaskSchedule basicTaskSchedule : basicTaskSchedules) {
if(!basicTaskSchedules.get(i).isActivateAble()) { if (basicTaskSchedule.isActivateAble()) {
basicTaskSchedules.remove(i); activatableSchedules.add(basicTaskSchedule);
} }
} }
} }
return new ServiceResult<>(basicTaskSchedules); return new ServiceResult<>(activatableSchedules);
} }
public ServiceResult<List<BasicTaskSchedule>> loadSchedules(String username) { public ServiceResult<List<BasicTaskSchedule>> loadSchedules(String username) {