Fixed bug with infinite looped iteration in QueuedLinkedList
This commit is contained in:
parent
022fcd2abf
commit
efd53e6954
@ -2,14 +2,24 @@ package ru.dragonestia.picker.repository.impl.collection;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class QueuedLinkedList<ITEM extends QueuedLinkedList.Item> {
|
public class QueuedLinkedList<ITEM extends QueuedLinkedList.Item> {
|
||||||
|
|
||||||
|
private final Function<ITEM, Boolean> selectorValidator;
|
||||||
private Node<ITEM> first;
|
private Node<ITEM> first;
|
||||||
private Node<ITEM> last;
|
private Node<ITEM> last;
|
||||||
private Node<ITEM> cursor;
|
private Node<ITEM> cursor;
|
||||||
private final Map<String, Node<ITEM>> itemMap = new HashMap<>();
|
private final Map<String, Node<ITEM>> itemMap = new HashMap<>();
|
||||||
|
|
||||||
|
public QueuedLinkedList() {
|
||||||
|
this(item -> true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public QueuedLinkedList(Function<ITEM, Boolean> selectorValidator) {
|
||||||
|
this.selectorValidator = selectorValidator;
|
||||||
|
}
|
||||||
|
|
||||||
public void add(ITEM item) {
|
public void add(ITEM item) {
|
||||||
if (itemMap.containsKey(item.getId())) return;
|
if (itemMap.containsKey(item.getId())) return;
|
||||||
|
|
||||||
@ -77,11 +87,28 @@ public class QueuedLinkedList<ITEM extends QueuedLinkedList.Item> {
|
|||||||
|
|
||||||
if (cursor == null) cursor = first;
|
if (cursor == null) cursor = first;
|
||||||
|
|
||||||
|
int rounds = 0;
|
||||||
Node<ITEM> item = cursor;
|
Node<ITEM> item = cursor;
|
||||||
|
while(rounds < 1) {
|
||||||
while (item != null && item.removed) {
|
while (item != null && item.removed) {
|
||||||
item = item.next;
|
item = item.next;
|
||||||
}
|
}
|
||||||
if (item == null) item = first;
|
|
||||||
|
if (item == null) {
|
||||||
|
item = first;
|
||||||
|
rounds++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (selectorValidator.apply(item.object)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
item = item.next;
|
||||||
|
}
|
||||||
|
if (rounds > 0) {
|
||||||
|
throw new RuntimeException("Cannot get need object because no one fulfills all the conditions");
|
||||||
|
}
|
||||||
|
|
||||||
cursor = item.next;
|
cursor = item.next;
|
||||||
return item.object;
|
return item.object;
|
||||||
|
|||||||
@ -7,11 +7,13 @@ import ru.dragonestia.picker.repository.UserRepository;
|
|||||||
import ru.dragonestia.picker.repository.impl.collection.QueuedLinkedList;
|
import ru.dragonestia.picker.repository.impl.collection.QueuedLinkedList;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class RoundRobinPicker implements RoomPicker {
|
public class RoundRobinPicker implements RoomPicker {
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private final QueuedLinkedList<RoomWrapper> list = new QueuedLinkedList<>();
|
private final AtomicInteger addition = new AtomicInteger(0);
|
||||||
|
private final QueuedLinkedList<RoomWrapper> list = new QueuedLinkedList<>(wrapper -> wrapper.canAddUnits(addition.get()));
|
||||||
|
|
||||||
public RoundRobinPicker(UserRepository userRepository) {
|
public RoundRobinPicker(UserRepository userRepository) {
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
@ -38,7 +40,8 @@ public class RoundRobinPicker implements RoomPicker {
|
|||||||
|
|
||||||
synchronized (list) {
|
synchronized (list) {
|
||||||
try {
|
try {
|
||||||
while (!(wrapper = list.pick()).canAddUnits(amount));
|
addition.set(amount);
|
||||||
|
wrapper = list.pick();
|
||||||
} catch (RuntimeException ex) {
|
} catch (RuntimeException ex) {
|
||||||
throw new RuntimeException("There are no rooms available");
|
throw new RuntimeException("There are no rooms available");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user