KickJava   Java API By Example, From Geeks To Geeks.

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


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

24
25 package info.monitorenter.gui.chart.traces;
26
27 import info.monitorenter.gui.chart.TracePoint2D;
28
29 /**
30  * Additional to the <code>Trace2DLtdReplacing</code> all tracepoints will be
31  * sorted by their x- value.
32  * <p>
33  * Performance is slower compared to the class named above. Internally a
34  * <code>TreeSet </code> is used (instead of <code>RingBufferArrayFast</code>)
35  * to keep the comparable <code>TracePoint2D</code>- instances sorted.
36  * Internally all tracepoints are <code>TracePoint2D</code> -instances.
37  * <p>
38  *
39  * @author <a HREF='mailto:Achim.Westermann@gmx.de'>Achim Westermann </a>
40  *
41  * @version $Revision: 1.2 $
42  */

43 public class Trace2DLtdSorted extends Trace2DSorted {
44
45   /** The maximum amount of points that will be shown. */
46   protected int m_maxsize;
47
48   /**
49    * Constructs an instance with a default buffer size of 100.
50    * <p>
51    */

52   public Trace2DLtdSorted() {
53
54     this(100);
55   }
56
57   /**
58    * Constructs an instance with a buffer size of maxsize.
59    *
60    * @param maxsize
61    * the maximum amount of points to show.
62    */

63   public Trace2DLtdSorted(final int maxsize) {
64     this.m_maxsize = maxsize;
65   }
66
67   /**
68    * In case point has an x- value already contained, the old tracepoint with
69    * that value will be replaced by the new one. Else the new tracepoint will be
70    * added at an index in order to keep the ascending order of tracepoints with
71    * a higher x- value are contained.
72    * <p>
73    * If points takes additional space (it's x- value is not already contained)
74    * and maxsize is reached, the first element (with lowest x- value) will be
75    * removed.
76    * <p>
77    *
78    * @param point
79    * the point to add.
80    *
81    * @return true if the point was successfully removed.
82    */

83   protected boolean addPointInternal(final TracePoint2D point) {
84
85     boolean rem = this.removePoint(point);
86     this.m_points.add(point);
87     if (!rem) {
88       if (this.m_points.size() > this.m_maxsize) {
89         TracePoint2D remove = (TracePoint2D) this.m_points.last();
90         this.removePoint(remove);
91       }
92     }
93     return true;
94   }
95
96   /**
97    * @see info.monitorenter.gui.chart.ITrace2D#getMaxSize()
98    */

99   public final int getMaxSize() {
100
101     return this.m_maxsize;
102   }
103
104   /**
105    * Sets the maximum amount of points that will be shown.
106    * <p>
107    *
108    * @param amount
109    * the maximum amount of points that will be shown.
110    */

111   public final void setMaxSize(final int amount) {
112
113     synchronized (this) {
114       this.m_maxsize = amount;
115     }
116   }
117 }
118
Popular Tags