1 22 package info.monitorenter.gui.chart.traces; 23 24 import info.monitorenter.gui.chart.Chart2D; 25 import info.monitorenter.gui.chart.ITrace2D; 26 import info.monitorenter.gui.chart.TracePoint2D; 27 import info.monitorenter.util.collections.IRingBuffer; 28 import info.monitorenter.util.collections.RingBufferArrayFast; 29 30 import java.util.Iterator ; 31 32 33 53 public class Trace2DLtd extends ATrace2D implements ITrace2D { 54 55 62 public static void main(final String [] args) { 63 try { 64 final Trace2DLtd test = new Trace2DLtd(10); 65 for (int i = 0; i < 10; i++) { 67 test.addPoint(i, i); 68 } 69 Iterator it = test.iterator(); 71 while (it.hasNext()) { 72 } 74 test.setMaxSize(15); 79 it = test.iterator(); 81 while (it.hasNext()) { 82 } 84 88 for (int i = 10; i < 15; i++) { 90 test.addPoint(i, i); 91 } 92 it = test.iterator(); 94 while (it.hasNext()) { 95 } 97 101 } catch (Throwable f) { 102 f.printStackTrace(); 103 } 104 } 105 106 110 protected IRingBuffer m_buffer; 111 112 116 public Trace2DLtd() { 117 this(100); 118 } 119 120 128 public Trace2DLtd(final int maxsize) { 129 this(maxsize, Trace2DLtd.class.getName() + "-" + getInstanceCount()); 130 } 131 132 143 public Trace2DLtd(final int maxsize, final String name) { 144 this.m_buffer = new RingBufferArrayFast(maxsize); 145 this.setName(name); 146 } 147 148 155 public Trace2DLtd(final String name) { 156 this(100, name); 157 } 158 159 162 public boolean addPointInternal(final TracePoint2D p) { 163 164 TracePoint2D removed = (TracePoint2D) this.m_buffer.add(p); 165 double tmpx, tmpy; 166 if (removed != null) { 167 tmpx = removed.getX(); 168 tmpy = removed.getY(); 169 if (tmpx >= this.m_maxX) { 171 tmpx = this.m_maxX; 172 this.maxXSearch(); 173 this.firePropertyChange(PROPERTY_MAX_X, new Double (tmpx), new Double (this.m_maxX)); 174 } else if (tmpx <= m_minX) { 175 tmpx = this.m_minX; 176 this.minXSearch(); 177 this.firePropertyChange(PROPERTY_MIN_X, new Double (tmpx), new Double (this.m_minX)); 178 } 179 if (tmpy >= this.m_maxY) { 180 tmpy = this.m_maxY; 181 this.maxYSearch(); 182 this.firePropertyChange(PROPERTY_MAX_Y, new Double (tmpy), new Double (this.m_maxY)); 183 } else if (tmpy <= this.m_minY) { 184 tmpy = this.m_minY; 185 this.minYSearch(); 186 this.firePropertyChange(PROPERTY_MIN_Y, new Double (tmpy), new Double (this.m_minY)); 187 } 188 this.firePointAdded(p); 190 return false; 191 } else { 192 return true; 195 } 196 } 197 198 201 public int getMaxSize() { 202 return this.m_buffer.getBufferSize(); 203 } 204 205 213 public int getSize() { 214 return this.m_buffer.size(); 215 } 216 217 220 public boolean isEmpty() { 221 return this.m_buffer.isEmpty(); 222 } 223 224 227 public Iterator iterator() { 228 if (Chart2D.DEBUG_THREADING) { 229 System.out.println("Trace2DLtd.iterator, 0 locks"); 230 } 231 232 synchronized (this.m_renderer) { 233 if (Chart2D.DEBUG_THREADING) { 234 System.out.println("Trace2DLtd.iterator, 1 lock"); 235 } 236 synchronized (this) { 237 if (Chart2D.DEBUG_THREADING) { 238 System.out.println("Trace2DLtd.iterator, 2 locks"); 239 } 240 return this.m_buffer.iteratorL2F(); 241 } 242 } 243 } 244 245 248 public void removeAllPointsInternal() { 249 this.m_buffer.clear(); 250 } 251 252 267 protected boolean removePointInternal(final TracePoint2D point) { 268 return false; 269 } 270 271 285 public final void setMaxSize(final int amount) { 286 if (Chart2D.DEBUG_THREADING) { 287 System.out.println("Trace2DLtd.setMaxSize, 0 locks"); 288 } 289 290 synchronized (this.m_renderer) { 291 if (Chart2D.DEBUG_THREADING) { 292 System.out.println("Trace2DLtd.setMaxSize, 1 lock"); 293 } 294 synchronized (this) { 295 if (Chart2D.DEBUG_THREADING) { 296 System.out.println("Trace2DLtd.setMaxSize, 2 locks"); 297 } 298 this.m_buffer.setBufferSize(amount); 299 300 double xmin = this.m_minX; 301 this.minXSearch(); 302 if (this.m_minX != xmin) { 303 this.firePropertyChange(PROPERTY_MIN_X, new Double (xmin), new Double (this.m_minX)); 304 } 305 306 double xmax = this.m_maxX; 307 this.maxXSearch(); 308 if (this.m_maxX != xmax) { 309 this.firePropertyChange(PROPERTY_MAX_X, new Double (xmax), new Double (this.m_maxX)); 310 } 311 312 double ymax = this.m_maxY; 313 this.maxYSearch(); 314 if (this.m_maxY != ymax) { 315 this.firePropertyChange(PROPERTY_MAX_Y, new Double (ymax), new Double (this.m_maxY)); 316 } 317 318 double ymin = this.m_minY; 319 this.minYSearch(); 320 if (this.m_minY != ymin) { 321 this.firePropertyChange(PROPERTY_MIN_Y, new Double (ymin), new Double (this.m_minY)); 322 } 323 } 324 } 325 } 326 } 327 | Popular Tags |