View Javadoc

    1   /*
    2    * @(#)SimEvent.java Feb 1, 2003
    3    *
    4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
    5    * the Netherlands All rights reserved.
    6    *
    7    * This software is proprietary information of Delft University of Technology
    8    * The code is published under the General Public License
    9    */
    10  package nl.tudelft.simulation.dsol.formalisms.devs;
    11
    12  import java.io.Serializable;
    13
    14  import nl.tudelft.simulation.dsol.SimRuntimeException;
    15
    16  /***
    17   * The AbstractSimEvent forms the basement for SimEvents and defines a compare
    18   * method by which eventLists can compare priority of the event.
    19   * <p>
    20   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
    21   * University of Technology </a>, the Netherlands. <br>
    22   * See for project information <a href="http://www.simulation.tudelft.nl">
    23   * www.simulation.tudelft.nl </a> <br>
    24   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
    25   * License (GPL) </a>, no warranty <br>
    26   *
    27   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
    28   *         Jacobs </a>
    29   * @version 1.11 2004-03-26
    30   * @since 1.0
    31   */
    32  public abstract class AbstractSimEvent implements SimEventInterface,
    33  		Comparable, Serializable
    34  {
    35
    36  	/***
    37  	 * absoluteExecutionTime reflects the time at which the event is scheduled
    38  	 *
    39  	 * @uml.property name="absoluteExecutionTime"
    40  	 */
    41  	protected double absoluteExecutionTime = Double.NaN;
    42
    43  	/***
    44  	 * priority reflects the priority of the event
    45  	 *
    46  	 * @uml.property name="priority"
    47  	 */
    48  	protected short priority = SimEventInterface.NORMAL_PRIORITY;
    49
    50
    51  	/*** the id used in compare statements */
    52  	protected long id = 0L;
    53
    54  	/***
    55  	 * The constuctor of the event stores the time the event must be executed
    56  	 * and the object and method to invoke
    57  	 *
    58  	 * @param executionTime reflects the time the event has to be executed.
    59  	 */
    60  	public AbstractSimEvent(final double executionTime)
    61  	{
    62  		this(executionTime, SimEventInterface.NORMAL_PRIORITY);
    63  	}
    64
    65  	/***
    66  	 * The constuctor of the event stores the time the event must be executed
    67  	 * and the object and method to invoke
    68  	 *
    69  	 * @param executionTime reflects the time the event has to be executed.
    70  	 * @param priority reflects the priority of the event
    71  	 */
    72  	public AbstractSimEvent(final double executionTime, final short priority)
    73  	{
    74  		this.absoluteExecutionTime = executionTime;
    75  		if (priority < SimEventInterface.MIN_PRIORITY - 1
    76  				|| priority > SimEventInterface.MAX_PRIORITY + 1)
    77  		{
    78  			throw new IllegalArgumentException("priority must be between ["
    79  					+ SimEventInterface.MIN_PRIORITY + ".."
    80  					+ SimEventInterface.MAX_PRIORITY + "]");
    81  		}
    82  		this.priority = priority;
    83  	}
    84
    85  	/***
    86  	 * @see java.lang.Comparable#compareTo(java.lang.Object)
    87  	 */
    88  	public int compareTo(final Object object)
    89  	{
    90  		SimEventInterface simEvent = (SimEventInterface) object;
    91  		if (this.absoluteExecutionTime < simEvent.getAbsoluteExecutionTime())
    92  		{
    93  			return -1;
    94  		}
    95  		if (this.absoluteExecutionTime > simEvent.getAbsoluteExecutionTime())
    96  		{
    97  			return 1;
    98  		}
    99  		if (this.priority < simEvent.getPriority())
    100 		{
    101 			return 1;
    102 		}
    103 		if (this.priority > simEvent.getPriority())
    104 		{
    105 			return -1;
    106 		}
    107 		if (this.id < simEvent.getID())
    108 		{
    109 			return -1;
    110 		}
    111 		if (this.id > simEvent.getID())
    112 		{
    113 			return 1;
    114 		}
    115 		return 0;
    116 	}
    117
    118 	/***
    119 	 * executes the simEvent
    120 	 *
    121 	 * @throws SimRuntimeException on execution failure
    122 	 */
    123 	public abstract void execute() throws SimRuntimeException;
    124
    125 	/***
    126 	 * @return The execution time of a simulation event
    127 	 *
    128 	 * @uml.property name="absoluteExecutionTime"
    129 	 */
    130 	public double getAbsoluteExecutionTime()
    131 	{
    132 		return this.absoluteExecutionTime;
    133 	}
    134
    135 	/***
    136 	 * @return The priority of a simulation event. The priorities are programmed
    137 	 *         according to the Java thread priority. Use 10 (MAX_PRIORITY), -9, .. ,
    138 	 *         5 (NORMAL_PRIORITY), 1(MIN_PRIORITY)
    139 	 *
    140 	 * @uml.property name="priority"
    141 	 */
    142 	public short getPriority()
    143 	{
    144 		return this.priority;
    145 	}
    146
    147 	/***
    148 	 * @return Returns the id.
    149 	 */
    150 	public long getID()
    151 	{
    152 		return this.id;
    153 	}
    154
    155 	/***
    156 	 * @param id The id to set.
    157 	 */
    158 	public void setID(final long id)
    159 	{
    160 		this.id = id;
    161 	}
    162 }