Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

Queue.java

Go to the documentation of this file.
00001 /* Cyclops
00002  * FER - SPVP
00003  * Fakultet elektrotehnike i racunarstva (http://www.fer.hr/)
00004  * Unska 3, 10000 Zagreb, Hrvatska
00005  * (c) 2001 FER, Zagreb.
00006  */
00007 
00008 package hr.fer.zesoi.cyclops;
00009 
00010 import java.util.*;
00011 
00012 
00020 public class Queue
00021 {
00022 
00027     protected int max_capacity;
00028 
00032     protected int number_of_packets;
00033 
00037     private Stack storage_buffer = new Stack();
00038 
00042     public Queue ()
00043     {
00044         max_capacity = 1;
00045         number_of_packets = 0;
00046     }
00047     /* Queue */
00048 
00052     public Queue (int capacity)
00053     {
00054         max_capacity = (capacity < 0)? 0: capacity;
00055         number_of_packets = 0;
00056     }
00057     /* Queue */
00058 
00064     public void setCapacity (int capacity)
00065     {
00066         while (capacity < number_of_packets)
00067             {
00068                 this.get();
00069             }
00070         this.max_capacity = capacity;
00071     }
00072     /* setCapacity */
00073 
00078     public boolean isEmpty ()
00079     {
00080         return storage_buffer.isEmpty();
00081     }
00082     /* isEmpty */
00083 
00090     public boolean put (Object packet)
00091     {
00092         if (number_of_packets <= max_capacity)
00093             {
00094                 storage_buffer.push(packet);
00095                 number_of_packets = number_of_packets + 1;
00096                 return true;
00097             }
00098         else
00099             {
00100                 return false;
00101             }
00102     }
00103     /* put */
00104 
00110     public Object get ()
00111     {
00112         Object packet;
00113 
00114         if (number_of_packets > 0)
00115             {
00116                 number_of_packets = number_of_packets - 1;
00117                 packet = storage_buffer.pop();
00118                 return packet;
00119             }
00120         else
00121             {
00122                 throw new NoSuchElementException("No more packages.");
00123             }
00124     }
00125     /* get */
00126 
00132     public Object peek ()
00133     {
00134         return storage_buffer.peek();
00135     }
00136     /* peek */
00137 
00138 }
00139 /* Queue */

Generated at Thu Jun 28 03:04:21 2001 for Cyclops Network Simulator by doxygen1.2.7 written by Dimitri van Heesch, © 1997-2001