KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > chart > traces > Trace2DLtd


1 /*
2  * Trace2DLtd, a RingBuffer- based fast implementation of a ITrace2D.
3  * Copyright (C) 2002 Achim Westermann, Achim.Westermann@gmx.de
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * If you modify or optimize the code in a useful way please let me know.
20  * Achim.Westermann@gmx.de
21  */

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 JavaDoc;
31
32
33 /**
34  * Additional to the Trace2DSimple the Trace2DLimited adds the following
35  * functionality:
36  * <p>
37  * <ul>
38  * <li>The amount of internal tracepoints is limited to the maxsize, passed to
39  * the constructor.</li>
40  * <li>If a new tracepoint is inserted and the maxsize has been reached, the
41  * tracepoint residing for the longest time in this trace is thrown away.</li>
42  * </UL>
43  * Take this implementation to display frequently changing data (nonstatic, time -
44  * dependant values). You will avoid a huge growing amount of tracepoints that
45  * would increase the time for scaling and painting until system hangs or
46  * java.lang.OutOfMemoryError is thrown.
47  * <p>
48  *
49  * @author <a HREF='mailto:Achim.Westermann@gmx.de'>Achim Westermann </a>
50  *
51  * @version $Revision: 1.2 $
52  */

53 public class Trace2DLtd extends ATrace2D implements ITrace2D {
54
55   /**
56    * Prooves that synchronizing from outside is necessary for multithreaded use.
57    * <p>
58    *
59    * @param args
60    * ignored
61    */

62   public static void main(final String JavaDoc[] args) {
63     try {
64       final Trace2DLtd test = new Trace2DLtd(10);
65       // System.out.println("adding 0 to 9 to size limit of 10: ");
66
for (int i = 0; i < 10; i++) {
67         test.addPoint(i, i);
68       }
69       // System.out.println("Iterator- test: ");
70
Iterator JavaDoc it = test.iterator();
71       while (it.hasNext()) {
72         // System.out.println("Iterator: " + it.next());
73
}
74       // System.out.println("buffer.getYoungest(): " +
75
// test.buffer.getYoungest());
76
// System.out.println("buffer.getOldest(): " + test.buffer.getOldest());
77
// System.out.println("\nsetting size to 15.");
78
test.setMaxSize(15);
79       // System.out.println("Iterator- test: ");
80
it = test.iterator();
81       while (it.hasNext()) {
82         // System.out.println("Iterator: " + it.next());
83
}
84       // System.out.println("buffer.getYoungest(): " +
85
// test.buffer.getYoungest());
86
// System.out.println("buffer.getOldest(): " + test.buffer.getOldest());
87

88       // System.out.println("\nadding 10 to 14 to size limit of 14: ");
89
for (int i = 10; i < 15; i++) {
90         test.addPoint(i, i);
91       }
92       // System.out.println("Iterator- test: ");
93
it = test.iterator();
94       while (it.hasNext()) {
95         // System.out.println("Iterator: " + it.next());
96
}
97       // System.out.println("buffer.getYoungest(): " +
98
// test.buffer.getYoungest());
99
// System.out.println("buffer.getOldest(): " + test.buffer.getOldest());
100

101     } catch (Throwable JavaDoc f) {
102       f.printStackTrace();
103     }
104   }
105
106   /**
107    * Internal fast fifo buffer implentation based upon indexed access to an
108    * array.
109    */

110   protected IRingBuffer m_buffer;
111
112   /**
113    * Constructs an instance with a default buffersize of 100.
114    * <p>
115    */

116   public Trace2DLtd() {
117     this(100);
118   }
119
120   /**
121    * Constructs an instance with a buffersize of maxsize and a default name.
122    * <p>
123    *
124    * @param maxsize
125    * the buffer size for the maximum amount of points that will be
126    * shown.
127    */

128   public Trace2DLtd(final int maxsize) {
129     this(maxsize, Trace2DLtd.class.getName() + "-" + getInstanceCount());
130   }
131
132   /**
133    * Constructs an instance with a buffersize of maxsize and a default name.
134    * <p>
135    *
136    * @param maxsize
137    * the buffer size for the maximum amount of points that will be
138    * shown.
139    *
140    * @param name
141    * the name that will be displayed for this trace.
142    */

143   public Trace2DLtd(final int maxsize, final String JavaDoc name) {
144     this.m_buffer = new RingBufferArrayFast(maxsize);
145     this.setName(name);
146   }
147
148   /**
149    * Creates an instance with a default buffersize of 100 and the given name.
150    * <p>
151    *
152    * @param name
153    * the name that will be displayed for the trace.
154    */

155   public Trace2DLtd(final String JavaDoc name) {
156     this(100, name);
157   }
158
159   /**
160    * @see ATrace2D#addPointInternal(info.monitorenter.gui.chart.TracePoint2D)
161    */

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       // System.out.println("Trace2DLtd.addPoint() removed point!");
170
if (tmpx >= this.m_maxX) {
171         tmpx = this.m_maxX;
172         this.maxXSearch();
173         this.firePropertyChange(PROPERTY_MAX_X, new Double JavaDoc(tmpx), new Double JavaDoc(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 JavaDoc(tmpx), new Double JavaDoc(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 JavaDoc(tmpy), new Double JavaDoc(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 JavaDoc(tmpy), new Double JavaDoc(this.m_minY));
187       }
188       // scale the new point, check for new bounds!
189
this.firePointAdded(p);
190       return false;
191     } else {
192       // no point was removed
193
// use bound checks of calling addPoint
194
return true;
195     }
196   }
197
198   /**
199    * @see info.monitorenter.gui.chart.ITrace2D#getMaxSize()
200    */

201   public int getMaxSize() {
202     return this.m_buffer.getBufferSize();
203   }
204
205   /**
206    * Returns the acutal amount of points in this trace.
207    * <p>
208    *
209    * @return the acutal amount of points in this trace.
210    *
211    * @see info.monitorenter.gui.chart.ITrace2D#getSize()
212    */

213   public int getSize() {
214     return this.m_buffer.size();
215   }
216
217   /**
218    * @see info.monitorenter.gui.chart.ITrace2D#isEmpty()
219    */

220   public boolean isEmpty() {
221     return this.m_buffer.isEmpty();
222   }
223
224   /**
225    * @see info.monitorenter.gui.chart.ITrace2D#iterator()
226    */

227   public Iterator JavaDoc 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   /**
246    * @see info.monitorenter.gui.chart.ITrace2D#removeAllPoints()
247    */

248   public void removeAllPointsInternal() {
249     this.m_buffer.clear();
250   }
251
252   /**
253    * <p>
254    * Returns false always because internally a ringbuffer is used which does not
255    * allow removing of values because that would break the contract of a
256    * ringbuffer.
257    * </p>
258    *
259    * @param point
260    * the point to remove.
261    *
262    * @return false always because internally a ringbuffer is used which does not
263    * allow removing of values because that would break the contract of a
264    * ringbuffer.
265    *
266    */

267   protected boolean removePointInternal(final TracePoint2D point) {
268     return false;
269   }
270
271   /**
272    * Sets the maximum amount of points that may be displayed.
273    * <p>
274    *
275    * Don't use this too often as decreases in size may cause expensive array
276    * copy operations and new searches on all points for bound changes.
277    * <p>
278    *
279    * TODO: Only search for bounds if size is smaller than before, debug and
280    * test.
281    *
282    * @param amount
283    * the new maximum amount of points to show.
284    */

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 JavaDoc(xmin), new Double JavaDoc(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 JavaDoc(xmax), new Double JavaDoc(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 JavaDoc(ymax), new Double JavaDoc(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 JavaDoc(ymin), new Double JavaDoc(this.m_minY));
322         }
323       }
324     }
325   }
326 }
327
Popular Tags