KickJava   Java API By Example, From Geeks To Geeks.

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


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

23
24 package info.monitorenter.gui.chart.traces;
25
26 import info.monitorenter.gui.chart.ITrace2D;
27 import info.monitorenter.gui.chart.TracePoint2D;
28
29 import java.util.Iterator JavaDoc;
30 import java.util.SortedSet JavaDoc;
31 import java.util.TreeSet JavaDoc;
32
33 /**
34  * Has the behaviour of <code>Trace2DReplacing</code> and an additional
35  * features: <br>
36  * <ul>
37  * <li>All traceoints added whose x- values are not already contained are added
38  * to the internal Tree ordered by growing x-values. Therefore it is guaranteed
39  * that the tracepoints will be sorted in ascending order of x- values at any
40  * time.</li>
41  * </UL>
42  *
43  * Because sorted insertion of a List causes n! index- operations (
44  * <code>get(int i)</code>) additional to the comparisons this class does not
45  * extend <code>Trace2DSimple</code> which uses a List. Instead a
46  * <code>TreeSet </code> is used.
47  *
48  * @author <a HREF='mailto:Achim.Westermann@gmx.de'>Achim Westermann </a>
49  *
50  * @version $Revision: 1.2 $
51  */

52 public class Trace2DSorted extends ATrace2D implements ITrace2D {
53   /** The sorted set of points. */
54   protected SortedSet JavaDoc m_points = new TreeSet JavaDoc();
55
56   /**
57    * Defcon.
58    * <p>
59    */

60   public Trace2DSorted() {
61     // nop
62
}
63
64   /**
65    * In case p has an x- value already contained, the old tracepoint with that
66    * value will be replaced by the new one. Else the new tracepoint will be
67    * added at an index in order to keep the ascending order of tracepoints with
68    * a higher x- value are contained.
69    * <p>
70    *
71    * @param p
72    * the point to add.
73    *
74    * @return true if the given point was successfully added.
75    */

76   protected boolean addPointInternal(final TracePoint2D p) {
77     // remove eventually contained to allow adding of new one
78
this.removePoint(p);
79     return this.m_points.add(p);
80   }
81
82   /**
83    * @see info.monitorenter.gui.chart.ITrace2D#getMaxSize()
84    */

85   public int getMaxSize() {
86     return Integer.MAX_VALUE;
87   }
88
89   /**
90    * @see info.monitorenter.gui.chart.ITrace2D#getSize()
91    */

92   public int getSize() {
93     return this.m_points.size();
94   }
95
96   /**
97    * @see info.monitorenter.gui.chart.ITrace2D#isEmpty()
98    */

99   public boolean isEmpty() {
100     return this.m_points.size() == 0;
101   }
102
103   /**
104    * @see info.monitorenter.gui.chart.ITrace2D#iterator()
105    */

106   public Iterator JavaDoc iterator() {
107     return this.m_points.iterator();
108   }
109
110   /**
111    * @see ATrace2D#addPointInternal(info.monitorenter.gui.chart.TracePoint2D)
112    */

113   protected void removeAllPointsInternal() {
114     this.m_points.clear();
115   }
116
117   /**
118    * @see ATrace2D#removePointInternal(info.monitorenter.gui.chart.TracePoint2D)
119    */

120   protected boolean removePointInternal(final TracePoint2D point) {
121     return this.m_points.remove(point);
122   }
123 }
124
Popular Tags