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