/
XYVector.java
200 lines (166 loc) · 5.13 KB
/
XYVector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* genvlin project.
* This stands under Public domain
*/
package de.genvlin.core.data;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.Serializable;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* This class is a wrapper around two Vectors. They can used as x-y-pairs
* in a plot. Where plot could be a <tt>XYVectorPool</tt>.
*/
class XYVector extends AbstractCollection
implements BoundedXYCollectionInterface, XYVectorInterface, Serializable {
private VectorInterface x;
private VectorInterface y;
XYVector(VectorInterface xVector, VectorInterface yVector, ID id) {
super(id);
x = xVector;
y = yVector;
}
public void add(Number xNum, Number yNum) {
x.add(xNum);
y.add(yNum);
}
/**
* This method returns the maximal value in x data array
*/
public Number getMaxX() {
if(size()== 0)
return Double.POSITIVE_INFINITY;
/*//return ((Number)Collections.max(xyVector.getX())).doubleValue();
double ret = Double.NEGATIVE_INFINITY;
for(int i=0; i < getX().size(); i++) {
n = getX(i);
if(n!=null && ret < n.doubleValue())
ret = n.doubleValue();
}*/
return x.getMax();
}
/**
* This method returns the minimal value in x data array
*/
public Number getMinX() {
if(size() == 0)
return Double.NEGATIVE_INFINITY;
/*double ret = Double.POSITIVE_INFINITY;
for(int i=0; i < getX().size(); i++) {
n = getX(i);
if(n!=null && ret > n.doubleValue())
ret = n.doubleValue();
}
return ret;*/
return x.getMin();
}
/**
* This method returns the maximal value in y data array
*/
public Number getMaxY() {
if(size() == 0) {
return Double.POSITIVE_INFINITY;
}
return y.getMax();
/*double ret = Double.NEGATIVE_INFINITY;
for(int i=0; i < getY().size(); i++) {
n = getY(i);
if(n!=null && ret < n.doubleValue())
ret = n.doubleValue();
}
return ret;*/
}
/**
* This method returns the minimal value in y data array
*/
public Number getMinY() {
if(size() == 0) {
return Double.NEGATIVE_INFINITY;
}
/*
double ret = Double.POSITIVE_INFINITY;
for(int i=0; i < getY().size(); i++) {
n = getY(i);
if(n!=null && ret > n.doubleValue())
ret = n.doubleValue();
}
return ret;*/
return y.getMin();
}
public Point2D.Double get(int i) {
return new Point2D.Double(getX(i).doubleValue(),
getY(i).doubleValue());
}
public int size() {
return Math.min(x.size(), y.size());
}
public void clear(){
x.clear();
y.clear();
}
public boolean remove(int index) {
if(index < size() && index >= 0) {
x.remove(index);
y.remove(index);
return true;
}
return false;
}
public Iterator<Point.Double> iterator() {
return new XYVectorIterator();
}
public Number getX(int index) {
return x.get(index);
}
public Number getY(int index) {
return y.get(index);
}
public double getXDouble(int index) {
if(x.get(index) == null) return Double.NaN;
else return x.get(index).doubleValue();
}
public double getYDouble(int index) {
if(y.get(index) == null) return Double.NaN;
else return y.get(index).doubleValue();
}
public void addVectorListener(CollectionListener vl) {
x.addVectorListener(vl);
y.addVectorListener(vl);
}
public void removeVectorListener(CollectionListener vl) {
x.removeVectorListener(vl);
y.removeVectorListener(vl);
}
/** The Iterator on Point.Double.
*/
class XYVectorIterator implements Iterator<Point.Double> {
/** points to current element (will returned by next()) */
int cursor = 0;
/** Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove.
*/
int lastRet = -1;
public boolean hasNext() {
return cursor != size();
}
public Point.Double next() {
try {
Point.Double next = get(cursor);
lastRet = cursor++;
return next;
} catch(IndexOutOfBoundsException e) {
throw new NoSuchElementException();
}
}
public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
XYVector.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
}
}
}