00001
00002
00003
00004
00005
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
00048
00052 public Queue (int capacity)
00053 {
00054 max_capacity = (capacity < 0)? 0: capacity;
00055 number_of_packets = 0;
00056 }
00057
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
00073
00078 public boolean isEmpty ()
00079 {
00080 return storage_buffer.isEmpty();
00081 }
00082
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
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
00126
00132 public Object peek ()
00133 {
00134 return storage_buffer.peek();
00135 }
00136
00137
00138 }
00139