Advertisement
andresnogales

Queue.java

Oct 27th, 2021
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. //Grupo 2.3
  2.  
  3. import java.lang.reflect.Array;
  4. import java.util.Iterator;
  5.  
  6. public class Queue<ELEMENT> {
  7.    
  8.     private final static Integer defaulDimension = 10;
  9.    
  10.     private int dimension;
  11.     protected Class<?> elementClass;
  12.     protected SimpleLinkedList<ELEMENT> list;
  13.     private int count;
  14.        
  15.     public Queue() {
  16.         list = new SimpleLinkedList<ELEMENT>();
  17.         this.count = 0;
  18.         dimension = defaulDimension;
  19.     }
  20.    
  21.     @SuppressWarnings("unchecked")
  22.     public Queue(int dimension,  ELEMENT... dummy) {
  23.         if (dummy.length > 0) {
  24.             throw new IllegalArgumentException("No se debe facilitar valores para dummy");
  25.         }
  26.         elementClass = dummy.getClass().getComponentType();
  27.         list = new SimpleLinkedList<ELEMENT>();
  28.         this.count = 0;
  29.         this.dimension = dimension;
  30.     }
  31.        
  32.     public boolean add(ELEMENT element) {
  33.          
  34.         if (list.size() >= dimension) {
  35.             throw new IllegalStateException("Cola llena ...");
  36.         }
  37.  
  38.         list.addLast(element);
  39.         ++count;
  40.  
  41.         return true;
  42.     }
  43.    
  44.     public ELEMENT element() {
  45.          
  46.         if (list.size() <= 0) {
  47.             throw new IllegalStateException("Cola vacía ...");
  48.         }
  49.  
  50.         return list.getFirst();
  51.     }
  52.    
  53.     public boolean offer(ELEMENT element) {
  54.          
  55.         if (list.size() >= dimension) {
  56.             return false;
  57.         }
  58.  
  59.         list.addLast(element);
  60.         ++count;
  61.  
  62.         return true;
  63.     }
  64.    
  65.     public ELEMENT peek() {
  66.         if (this.size() <= 0) {
  67.             return null;
  68.         }
  69.  
  70.         return list.getFirst();
  71.     }
  72.  
  73.     public ELEMENT pool() {
  74.         if (this.size() <= 0) {
  75.             return null;
  76.         }
  77.  
  78.         ELEMENT result = list.removeFirst();
  79.         --count;
  80.  
  81.         return result;
  82.     }
  83.    
  84.     public ELEMENT remove() {
  85.         if (this.size() <= 0) {
  86.             throw new IllegalStateException("Cola vacía");
  87.         }
  88.  
  89.         ELEMENT result = list.removeFirst();
  90.         --count;
  91.  
  92.         return result;
  93.     }
  94.    
  95.     public boolean isEmpty() {
  96.         return list.size() <= 0;
  97.     }
  98.  
  99.     public int size() {
  100.         return list.size();
  101.     }
  102.  
  103.     public Object[] toArray() {
  104.         ELEMENT [] result = (ELEMENT []) Array.newInstance(this.elementClass, this.size());
  105.         Iterator<ELEMENT> iteratorList = list.iterator();        
  106.         int i = 0;
  107.        
  108.         while(iteratorList.hasNext()) {
  109.             result[i] = iteratorList.next();
  110.             ++i;
  111.         }        
  112.         return result;
  113.     }
  114.    
  115.     public int getDimension() {
  116.         return dimension;
  117.     }
  118.  
  119.     public void setDimension(int dimension) {
  120.         this.dimension = dimension;
  121.     }
  122.  
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement