| 1 | package desmoj.core.simulator; |
| 2 | |
| 3 | /** |
| 4 | * Contains the implementation with the java.util.LinkedList to represent queueing |
| 5 | * functionality. The entities are queued first according to their priority and |
| 6 | * second in FIFO (first in first out) order. The statistic data of the queue will be |
| 7 | * stored in a <code>QueueBased</code> object. The <code>QueueListStandardFifo</code> |
| 8 | * has a reference to its <code>QueueBased</code> object. This class needs a |
| 9 | * reference to a subclass of QueueBased to update the queue statistics. |
| 10 | * It is used in many kinds of queue implementations i.e. in classes |
| 11 | * <code>Queue</code> and <code>ProcessQueue</code>. |
| 12 | * |
| 13 | * @see QueueList |
| 14 | * @see QueueBased |
| 15 | * @see Queue |
| 16 | * @see ProcessQueue |
| 17 | * |
| 18 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
| 19 | * @author Justin Neumann |
| 20 | * @author based on ideas from Soenke Claassen, Tim Lechler, Johannes Goebel |
| 21 | * |
| 22 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 23 | * you may not use this file except in compliance with the License. You |
| 24 | * may obtain a copy of the License at |
| 25 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 26 | * |
| 27 | * Unless required by applicable law or agreed to in writing, software |
| 28 | * distributed under the License is distributed on an "AS IS" |
| 29 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 30 | * or implied. See the License for the specific language governing |
| 31 | * permissions and limitations under the License. |
| 32 | * |
| 33 | */ |
| 34 | public class QueueListFifo<E extends Entity> extends QueueListStandard<E> implements |
| 35 | java.beans.PropertyChangeListener |
| 36 | |
| 37 | { |
| 38 | |
| 39 | /** |
| 40 | * Constructs an empty <code>QueueListStandardFifo</code> with no reference to its |
| 41 | * client QueueBased. This no-arg constructor is necessary to instantiate an |
| 42 | * object of this class by calling the |
| 43 | * <code>java.lang.Class.newInstance()</code> method. The reference to the |
| 44 | * QueueBased object making use of this queue-functionality must be provided |
| 45 | * later by calling the setQueueBased() method. The initial length is always |
| 46 | * zero. |
| 47 | */ |
| 48 | public QueueListFifo() |
| 49 | { |
| 50 | super(); |
| 51 | |
| 52 | // set the abbreviation for this kind of queueing discipline |
| 53 | this.abbreviation = "FIFO"; |
| 54 | |
| 55 | // we don't know the client queue yet. |
| 56 | // Must be provided later by calling the setQueueBased() method |
| 57 | clientQ = null; |
| 58 | |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Adds a new Entity to the QueueListStandardFifo. Entities are inserted according |
| 63 | * to their priority in descending order. The highest priority Entity will |
| 64 | * always be first in the queue. Entities with same priority are inserted in |
| 65 | * FiFo order. |
| 66 | * |
| 67 | * @param e |
| 68 | * Entity : The Entity to add to the QueueListStandardFifo |
| 69 | */ |
| 70 | public void insert(E e) |
| 71 | { |
| 72 | |
| 73 | if (e == null) { // check for null reference |
| 74 | sendWarning( |
| 75 | "Can not insert entity. Command ignored.", |
| 76 | "Class: QueueListStandardFifo Method: insert(Entity e).", |
| 77 | "The Entity reference given as parameter is a null reference.", |
| 78 | "Be sure to only use valid references."); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | if (contains(e)) { // entity must not be contained twice in queue |
| 83 | sendWarning("Can not insert entity. Command ignored.", |
| 84 | "Class: QueueListStandardFifo Method: insert(Entity e).", |
| 85 | "The Entity given as parameter is already enqueued.", |
| 86 | "Make sure the entity is not enqueued here by calling " |
| 87 | + "method 'contains(Entity e)'."); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | // if there are already entities in queue |
| 92 | if (!this.isEmpty()) |
| 93 | { |
| 94 | // continuously asks the predecessor (FIFO) entities if they have a lower priority; |
| 95 | // finally inserts at correct position |
| 96 | |
| 97 | E swap = last(); // swap here references the last element (FIFO) |
| 98 | while (Entity.isSmaller(swap, e)) // FIFO |
| 99 | { |
| 100 | swap = pred(swap); // swap reference to successor of itself |
| 101 | } |
| 102 | |
| 103 | if (swap == null) |
| 104 | { |
| 105 | queuelist.addFirst(e); |
| 106 | statisticalInsert(e); // update statistics |
| 107 | e.addQueueBased(this.clientQ); // sets entity's queue as this queued |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | this.insertAfter(e, swap); //inserts the entity at the correct position |
| 112 | // with help of the java.util.LinkedList |
| 113 | } |
| 114 | |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | queuelist.add(e); |
| 119 | e.addQueueBased(this.clientQ); // sets entity's queue as this queued |
| 120 | |
| 121 | statisticalInsert(e); // update statistics |
| 122 | } |
| 123 | } |
| 124 | } |