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 LIFO (last in first out) order. The statistic data of the queue will be |
7 | * stored in a <code>QueueBased</code> object. The <code>QueueListLifo</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 QueueListLifo<E extends Entity> extends QueueListStandard<E> implements |
35 | java.beans.PropertyChangeListener { |
36 | |
37 | |
38 | /** |
39 | * Constructs an empty <code>QueueListStandardFifo</code> with no reference to its |
40 | * client QueueBased. This no-arg constructor is necessary to instantiate an |
41 | * object of this class by calling the |
42 | * <code>java.lang.Class.newInstance()</code> method. The reference to the |
43 | * QueueBased object making use of this queue-functionality must be provided |
44 | * later by calling the setQueueBased() method. The initial length is always |
45 | * zero. |
46 | */ |
47 | public QueueListLifo() { |
48 | |
49 | // set the abbreviation for this kind of queueing discipline |
50 | this.abbreviation = "LIFO"; |
51 | } |
52 | |
53 | /** |
54 | * Adds a new Entity to the QueueListLifo. Entities are inserted according |
55 | * to their priority in descending order. The highest priority Entity will |
56 | * always be first in the queue. Entities with same priority are inserted in |
57 | * LiFo order. |
58 | * |
59 | * @param e |
60 | * Entity : The Entity to add to the QueueListStandardLifo |
61 | */ |
62 | @Override |
63 | public void insert(E e) |
64 | { |
65 | |
66 | if (e == null) { // check for null reference |
67 | sendWarning( |
68 | "Can not insert entity. Command ignored.", |
69 | "Class: QueueListStandardFifo Method: insert(Entity e).", |
70 | "The Entity reference given as parameter is a null reference.", |
71 | "Be sure to only use valid references."); |
72 | return; |
73 | } |
74 | |
75 | if (contains(e)) { // entity must not be contained twice in queue |
76 | sendWarning("Can not insert entity. Command ignored.", |
77 | "Class: QueueListStandardFifo Method: insert(Entity e).", |
78 | "The Entity given as parameter is already enqueued.", |
79 | "Make sure the entity is not enqueued here by calling " |
80 | + "method 'contains(Entity e)'."); |
81 | return; |
82 | } |
83 | |
84 | // if there are already entities in queue |
85 | if (this.size() > 0) |
86 | { |
87 | // continuously asks the succ (LIFO) entities if they have a higher priority; |
88 | // finally inserts at correct position |
89 | |
90 | E swap = first(); // swap references the first element of the list |
91 | while (Entity.isSmaller(e, swap)) // LIFO |
92 | { |
93 | swap = succ(swap); // swap reference to successor of itself |
94 | } |
95 | |
96 | if (swap == null) |
97 | { |
98 | queuelist.addLast(e); |
99 | statisticalInsert(e); // update statistics |
100 | e.addQueueBased(this.clientQ); // sets entity's queue as this queued |
101 | } |
102 | else |
103 | { |
104 | this.insertBefore(e, swap); //inserts the entity at the correct position |
105 | // with help of the java.util.LinkedList |
106 | } |
107 | |
108 | } |
109 | else |
110 | { |
111 | queuelist.add(e); |
112 | e.addQueueBased(this.clientQ); // sets entity's queue as this queued |
113 | |
114 | statisticalInsert(e); // update statistics |
115 | } |
116 | } |
117 | } |