@SuppressWarnings("unchecked") private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException { initHead(); int size = ois.readInt(); for (int i = 0; i < size; i++) { pushEnd((T) ois.readObject()); } }
/** * Inserts the request with the default priority. * * @param materialRequest * The {@link MaterialRequestObject} that shall be inserted. */ public final void insertRequest(MaterialRequestObject materialRequest) { getQueue(EPriority.DEFAULT, materialRequest.getBuildingType()).pushEnd(materialRequest); materialRequest.requestQueue = this; }
@Override public void moveObjectsOfPositionTo(ShortPoint2D position, AbstractMaterialRequestPriorityQueue newAbstractQueue) { assert newAbstractQueue instanceof SimpleMaterialRequestPriorityQueue : "can't move positions between diffrent types of queues."; SimpleMaterialRequestPriorityQueue newQueue = (SimpleMaterialRequestPriorityQueue) newAbstractQueue; for (int queueIdx = 0; queueIdx < queues.length; queueIdx++) { Iterator<MaterialRequestObject> iter = queues[queueIdx].iterator(); while (iter.hasNext()) { MaterialRequestObject curr = iter.next(); if (curr.getPosition().equals(position)) { iter.remove(); newQueue.queues[queueIdx].pushEnd(curr); curr.requestQueue = newQueue; } } } }
@Override public void moveObjectsOfPositionTo(ShortPoint2D position, AbstractMaterialRequestPriorityQueue newAbstractQueue) { assert newAbstractQueue instanceof MaterialsForBuildingsRequestPriorityQueue : "can't move positions between different types of queues."; MaterialsForBuildingsRequestPriorityQueue newQueue = (MaterialsForBuildingsRequestPriorityQueue) newAbstractQueue; for (int priorityIndex = 0; priorityIndex < queues.length; priorityIndex++) { DoubleLinkedList<MaterialRequestObject>[] priorityQueue = queues[priorityIndex]; for (int queueIdx = 0; queueIdx < numberOfConfigurableBuildings; queueIdx++) { Iterator<MaterialRequestObject> iterator = priorityQueue[queueIdx].iterator(); while (iterator.hasNext()) { MaterialRequestObject curr = iterator.next(); if (curr.getPosition().equals(position)) { iterator.remove(); newQueue.queues[priorityIndex][queueIdx].pushEnd(curr); curr.requestQueue = newQueue; } } } } }
protected final MaterialRequestObject findRequestInQueue(DoubleLinkedList<MaterialRequestObject> queue) { int numberOfElements = queue.size(); for (int handledElements = 0; handledElements < numberOfElements; handledElements++) { MaterialRequestObject request = queue.getFront(); // if the request is done if (request.isFinished()) { request.requestQueue = null; queue.popFront(); // remove the request numberOfElements--; } // if all needed are in delivery, or there can not be any more in delivery else if (!request.canTakeMoreOffers()) { queue.pushEnd(queue.popFront()); // move the request to the end. } // everything fine, take this request else { if (request.isRoundRobinRequest()) { queue.pushEnd(queue.popFront()); // put the request to the end of the queue. } return request; } } return null; }
@Test public void testIteratorLoop() { for (int i = 0; i < TEST_NUMBERS; i++) { list.pushEnd(new DoubleLinkedIntListItem(i)); } int i = 0; for (DoubleLinkedIntListItem curr : list) { assertEquals(i, curr.value); i++; } }
@Test public void testSerialization() throws IOException, ClassNotFoundException { for (int i = 0; i < TEST_NUMBERS; i++) { list.pushEnd(new DoubleLinkedIntListItem(i)); } DoubleLinkedList<DoubleLinkedIntListItem> readList = TestUtils.serializeAndDeserialize(list); assertListEquals(list, readList); }
@Test public void testIteratorRemove() { for (int i = 0; i < TEST_NUMBERS; i++) { list.pushEnd(new DoubleLinkedIntListItem(i)); } int i = 0; Iterator<DoubleLinkedIntListItem> iter = list.iterator(); while (iter.hasNext()) { assertEquals(i, iter.next().value); iter.remove(); i++; } assertEquals(TEST_NUMBERS, i); assertEquals(0, list.size()); assertTrue(list.isEmpty()); assertFalse(list.iterator().hasNext()); }
@Test public void testIteratorRemoveHalf() { for (int i = 0; i < TEST_NUMBERS; i++) { list.pushEnd(new DoubleLinkedIntListItem(i)); } int i = 0; Iterator<DoubleLinkedIntListItem> iter = list.iterator(); while (iter.hasNext()) { DoubleLinkedIntListItem curr = iter.next(); assertEquals(i, curr.value); if (curr.value % 2 == 0) { iter.remove(); } i++; } assertEquals(TEST_NUMBERS / 2, list.size()); assertTrue(list.iterator().hasNext()); i = 1; for (DoubleLinkedIntListItem curr : list) { assertEquals(i, curr.value); i += 2; } }
@Test public void testMerge() { DoubleLinkedList<DoubleLinkedIntListItem> list2 = new DoubleLinkedList<>(); for (int i = 0; i < TEST_NUMBERS; i++) { list.pushEnd(new DoubleLinkedIntListItem(i)); list2.pushEnd(new DoubleLinkedIntListItem(i)); } list2.mergeInto(list); assertEquals(0, list2.size()); assertEquals(TEST_NUMBERS * 2, list.size()); assertEquals(list2.head, ((DoubleLinkedListItem<DoubleLinkedIntListItem>) list2.head).next); assertEquals(list2.head, ((DoubleLinkedListItem<DoubleLinkedIntListItem>) list2.head).prev); int i = 0; for (DoubleLinkedIntListItem curr : list) { assertEquals(i % TEST_NUMBERS, curr.value); i++; } }
@Test public void testPushEndAndPopFrontWithClear() { for (int i = 0; i < TEST_NUMBERS; i++) { list.pushEnd(new DoubleLinkedIntListItem(i)); } assertEquals(TEST_NUMBERS, list.size()); list.clear(); assertEquals(0, list.size()); testPushEndAndPopFront(); }
@Test public void testPushEndAndPopFront() { assertEquals(0, list.size()); for (int i = 0; i < TEST_NUMBERS; i++) { list.pushEnd(new DoubleLinkedIntListItem(i)); assertEquals(i + 1, list.size()); } for (int i = 0; i < TEST_NUMBERS; i++) { assertEquals(10 - i, list.size()); assertEquals(i, list.popFront().value); } assertEquals(0, list.size()); }