1 23 package info.monitorenter.util.collections; 24 25 import info.monitorenter.util.StringUtil; 26 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.NoSuchElementException ; 31 32 33 57 public class RingBufferArray extends RingBufferArrayFast { 58 59 62 private static final long serialVersionUID = 3977861774055585593L; 63 64 69 protected List m_pendingremove = new LinkedList (); 70 71 78 public RingBufferArray(final int aSize) { 79 super(aSize); 80 } 81 82 85 public boolean isEmpty() { 86 return super.isEmpty() && (this.m_pendingremove.size() == 0); 87 } 88 89 99 abstract class ARingBufferIterator extends RingBufferArrayFast.ARingBufferIterator { 100 101 105 protected int m_pendpos; 106 107 110 public boolean hasNext() { 111 return super.hasNext() || this.m_pendpos >= 0; 112 } 113 114 117 protected void incPos() { 118 120 } 121 122 125 public Object next() { 126 Object ret = null; 127 if (this.m_pendpos >= 0) { 129 ret = RingBufferArray.this.m_pendingremove.get(this.m_pendpos); 130 this.m_pendpos--; 131 if (ret == null) { 132 System.out 133 .println("RingBufferArray.iteratorF2L returns null: head:" 134 + RingBufferArray.this.m_headpointer + " tail: " 135 + RingBufferArray.this.m_tailpointer); 136 } 137 return ret; 138 } 139 if (!this.hasNext()) { 140 throw new NoSuchElementException (); 141 } 142 ret = RingBufferArray.this.m_buffer[this.m_pos]; 143 this.m_count++; 144 this.incPos(); 145 return ret; 146 } 147 148 } 149 150 153 public java.util.Iterator iteratorF2L() { 154 return new ARingBufferIterator() { 155 { 156 this.m_pos = (m_headpointer == 0) ? m_size : m_headpointer - 1; 157 this.m_pendpos = RingBufferArray.this.m_pendingremove.size() - 1; 158 } 159 160 protected void incPos() { 161 if (this.m_pos == 0) { 162 this.m_pos = m_size; 163 } else { 164 this.m_pos--; 165 } 166 167 } 168 }; 169 } 170 171 174 public java.util.Iterator iteratorL2F() { 175 return new ARingBufferIterator() { 176 { 177 this.m_pos = m_tailpointer; 178 this.m_pendpos = 0; 179 } 180 181 182 protected void incPos() { 183 if (this.m_pos == RingBufferArray.this.m_size) { 184 this.m_pos = 0; 185 } else { 186 this.m_pos++; 187 } 188 } 189 }; 190 } 191 192 195 public Object remove() { 196 if (this.m_pendingremove.size() > 0) { 197 if (DEBUG) { 198 System.out.println("Removing pending element!!!"); 199 } 200 return this.m_pendingremove.remove(0); 201 } 202 return super.remove(); 203 } 204 205 208 public Object [] removeAll() { 209 Object [] ret = new Object [this.size() + this.m_pendingremove.size()]; 210 int stop = this.m_pendingremove.size(); 211 int i; 212 for (i = 0; i < stop; i++) { 213 ret[i] = this.m_pendingremove.remove(0); 214 } 215 for (; i < ret.length; i++) { 216 ret[i] = this.remove(); 217 } 218 return ret; 219 } 220 221 237 public void setBufferSize(final int newSize) { 238 List newpending = null; 239 if (this.size() > newSize) { 240 newpending = new LinkedList (); 241 int stop = this.size(); 242 for (int i = newSize; i < stop; i++) { 243 Object add = this.remove(); 244 newpending.add(add); 245 } 246 } 247 Object [] newbuffer = new Object [newSize]; 248 int i = 0; 249 if (DEBUG) { 250 System.out.println("setBufferSize(" + newSize + "): isEmpty(): " + this.isEmpty() + " tail: " 251 + this.m_tailpointer + " head: " + this.m_headpointer); 252 } 253 while (!isEmpty()) { 254 newbuffer[i] = remove(); 255 i++; 256 } 257 this.m_tailpointer = 0; 258 if (newSize == i) { 259 this.m_headpointer = 0; 260 } else { 261 this.m_headpointer = i; 262 } 263 this.m_buffer = newbuffer; 264 this.m_size = newSize - 1; 265 if (newpending != null) { 266 this.m_pendingremove = newpending; 267 } 268 } 269 270 273 public int size() { 274 return super.size() + this.m_pendingremove.size(); 275 } 276 277 287 public String toString() { 288 if (this.isEmpty()) { 289 if (DEBUG) { 290 System.out.println("toString(): isEmpty: true"); 291 } 292 return "[]"; 293 } 294 Object [] actualcontent = new Object [this.size()]; 295 int tmp = this.m_tailpointer; 296 int stop = this.m_pendingremove.size(); 297 Iterator it = this.m_pendingremove.iterator(); 298 int i = 0; 299 for (; i < stop; i++) { 300 actualcontent[i] = it.next(); 301 } 302 for (; i < actualcontent.length; i++) { 303 actualcontent[i] = this.m_buffer[tmp]; 304 if (tmp == this.m_size) { 305 tmp = 0; 306 } else { 307 tmp++; 308 } 309 if (tmp == this.m_headpointer && this.m_empty) { 310 break; 311 } 312 } 313 return StringUtil.arrayToString(actualcontent); 314 } 315 316 } 317 | Popular Tags |