1 55 56 package org.apache.bsf.debug.clientImpl; 57 58 59 public class CircularBuffer { 60 61 Object m_elements[]; 62 int m_pos,m_end; 63 64 public CircularBuffer() { 65 m_elements = new Object [256]; 66 } 67 68 82 private void grow() { 83 int p,q; 84 Object tmp[] = m_elements; 85 m_elements = new Object [2*m_elements.length]; 86 if (m_pos<=m_end) { 87 for (p=m_pos;p<m_end;p++) 88 m_elements[p] = tmp[p]; 89 } else { 90 for (p=0;p<m_end;p++) 91 m_elements[p] = tmp[p]; 92 93 for (p=m_pos;p<tmp.length;p++) 94 m_elements[p+tmp.length] = tmp[p]; 95 96 m_pos += tmp.length; 97 } 98 } 99 100 public synchronized void push(Object elem) { 101 102 if (m_end==m_elements.length) { 103 if (m_pos==0) { 106 grow(); 108 } else { 109 m_end = 0; 111 } 112 m_elements[m_end++] = elem; 113 return; 114 } 115 if (m_pos<=m_end) { 116 m_elements[m_end++] = elem; 120 return; 121 } 122 if (m_end==m_pos-1) { 124 grow(); 126 } 127 m_elements[m_end++] = elem; 128 } 129 public boolean isEmpty() { 131 return (m_pos==m_end); 132 } 133 public Object pop() { 135 Object elem; 136 137 if (m_pos==m_end) return null; 138 elem = m_elements[m_pos++]; 139 if (m_pos==m_elements.length && 140 m_pos!=m_end) 141 m_pos=0; 142 return elem; 143 } 144 145 } 146 147 | Popular Tags |