001package components.queue; 002 003import java.util.Iterator; 004import java.util.LinkedList; 005import java.util.List; 006import java.util.NoSuchElementException; 007 008/** 009 * {@code Queue} represented as a {@link java.util.List java.util.List} with 010 * implementations of primary methods. 011 * 012 * @param <T> 013 * type of {@code Queue} entries 014 * @correspondence <pre> 015 * this = [value of $this.rep based on List's "proper sequence"] 016 * </pre> 017 */ 018public class Queue1L<T> extends QueueSecondary<T> { 019 020 /* 021 * Private members -------------------------------------------------------- 022 */ 023 024 /** 025 * Representation of {@code this}. 026 */ 027 private List<T> rep; 028 029 /** 030 * Creator of initial representation. 031 */ 032 private void createNewRep() { 033 this.rep = new LinkedList<T>(); 034 } 035 036 /* 037 * Constructors ----------------------------------------------------------- 038 */ 039 040 /** 041 * No-argument constructor. 042 */ 043 public Queue1L() { 044 this.createNewRep(); 045 } 046 047 /* 048 * Standard methods ------------------------------------------------------- 049 */ 050 051 @SuppressWarnings("unchecked") 052 @Override 053 public final Queue<T> newInstance() { 054 try { 055 return this.getClass().getConstructor().newInstance(); 056 } catch (ReflectiveOperationException e) { 057 throw new AssertionError( 058 "Cannot construct object of type " + this.getClass()); 059 } 060 } 061 062 @Override 063 public final void clear() { 064 this.createNewRep(); 065 } 066 067 @Override 068 public final void transferFrom(Queue<T> source) { 069 assert source != null : "Violation of: source is not null"; 070 assert source != this : "Violation of: source is not this"; 071 assert source instanceof Queue1L<?> 072 : "Violation of: source is of dynamic type Queue1L<?>"; 073 /* 074 * This cast cannot fail since the assert above would have stopped 075 * execution in that case: source must be of dynamic type Queue1L<?>, 076 * and the ? must be T or the call would not have compiled. 077 */ 078 Queue1L<T> localSource = (Queue1L<T>) source; 079 this.rep = localSource.rep; 080 localSource.createNewRep(); 081 } 082 083 /* 084 * Kernel methods --------------------------------------------------------- 085 */ 086 087 @Override 088 public final void enqueue(T x) { 089 assert x != null : "Violation of: x is not null"; 090 this.rep.add(x); 091 } 092 093 @Override 094 public final T dequeue() { 095 assert this.length() > 0 : "Violation of: this /= <>"; 096 return this.rep.remove(0); 097 } 098 099 @Override 100 public final int length() { 101 return this.rep.size(); 102 } 103 104 @Override 105 public final Iterator<T> iterator() { 106 return new Queue1LIterator(); 107 } 108 109 /** 110 * Implementation of {@code Iterator} interface for {@code Queue1L}. 111 */ 112 private final class Queue1LIterator implements Iterator<T> { 113 114 /** 115 * Representation iterator. 116 */ 117 private final Iterator<T> iterator; 118 119 /** 120 * No-argument constructor. 121 */ 122 private Queue1LIterator() { 123 this.iterator = Queue1L.this.rep.iterator(); 124 } 125 126 @Override 127 public boolean hasNext() { 128 return this.iterator.hasNext(); 129 } 130 131 @Override 132 public T next() { 133 assert this.hasNext() : "Violation of: ~this.unseen /= <>"; 134 if (!this.hasNext()) { 135 /* 136 * Exception is supposed to be thrown in this case, but with 137 * assertion-checking enabled it cannot happen because of assert 138 * above. 139 */ 140 throw new NoSuchElementException(); 141 } 142 return this.iterator.next(); 143 } 144 145 @Override 146 public void remove() { 147 throw new UnsupportedOperationException("remove operation not supported"); 148 } 149 150 } 151 152 /* 153 * Other methods (overridden for performance reasons) --------------------- 154 */ 155 156 @Override 157 public final T replaceFront(T x) { 158 assert x != null : "Violation of: x is not null"; 159 assert this.length() > 0 : "Violation of: this /= <>"; 160 return this.rep.set(0, x); 161 } 162 163}