1 46 47 package org.jfree.data.xy; 48 49 import java.io.Serializable ; 50 import java.util.Arrays ; 51 import java.util.Collections ; 52 import java.util.Date ; 53 import java.util.List ; 54 55 58 public class DefaultWindDataset extends AbstractXYDataset 59 implements WindDataset { 60 61 62 private List seriesKeys; 63 64 65 private List allSeriesData; 66 67 72 public DefaultWindDataset() { 73 this.seriesKeys = new java.util.ArrayList (); 74 this.allSeriesData = new java.util.ArrayList (); 75 } 76 77 84 public DefaultWindDataset(Object [][][] data) { 85 this(seriesNameListFromDataArray(data), data); 86 } 87 88 98 public DefaultWindDataset(String [] seriesNames, Object [][][] data) { 99 this(Arrays.asList(seriesNames), data); 100 } 101 102 126 public DefaultWindDataset(List seriesKeys, Object [][][] data) { 127 if (seriesKeys == null) { 128 throw new IllegalArgumentException ("Null 'seriesKeys' argument."); 129 } 130 if (seriesKeys.size() != data.length) { 131 throw new IllegalArgumentException ("The number of series keys does " 132 + "not match the number of series in the data array."); 133 } 134 this.seriesKeys = seriesKeys; 135 int seriesCount = data.length; 136 this.allSeriesData = new java.util.ArrayList (seriesCount); 137 138 for (int seriesIndex = 0; seriesIndex < seriesCount; seriesIndex++) { 139 List oneSeriesData = new java.util.ArrayList (); 140 int maxItemCount = data[seriesIndex].length; 141 for (int itemIndex = 0; itemIndex < maxItemCount; itemIndex++) { 142 Object xObject = data[seriesIndex][itemIndex][0]; 143 if (xObject != null) { 144 Number xNumber; 145 if (xObject instanceof Number ) { 146 xNumber = (Number ) xObject; 147 } 148 else { 149 if (xObject instanceof Date ) { 150 Date xDate = (Date ) xObject; 151 xNumber = new Long (xDate.getTime()); 152 } 153 else { 154 xNumber = new Integer (0); 155 } 156 } 157 Number windDir = (Number ) data[seriesIndex][itemIndex][1]; 158 Number windForce = (Number ) data[seriesIndex][itemIndex][2]; 159 oneSeriesData.add(new WindDataItem(xNumber, windDir, 160 windForce)); 161 } 162 } 163 Collections.sort(oneSeriesData); 164 this.allSeriesData.add(seriesIndex, oneSeriesData); 165 } 166 167 } 168 169 174 public int getSeriesCount() { 175 return this.allSeriesData.size(); 176 } 177 178 185 public int getItemCount(int series) { 186 if (series < 0 || series >= getSeriesCount()) { 187 throw new IllegalArgumentException ("Invalid series index: " 188 + series); 189 } 190 List oneSeriesData = (List ) this.allSeriesData.get(series); 191 return oneSeriesData.size(); 192 } 193 194 201 public Comparable getSeriesKey(int series) { 202 if (series < 0 || series >= getSeriesCount()) { 203 throw new IllegalArgumentException ("Invalid series index: " 204 + series); 205 } 206 return (Comparable ) this.seriesKeys.get(series); 207 } 208 209 219 public Number getX(int series, int item) { 220 List oneSeriesData = (List ) this.allSeriesData.get(series); 221 WindDataItem windItem = (WindDataItem) oneSeriesData.get(item); 222 return windItem.getX(); 223 } 224 225 235 public Number getY(int series, int item) { 236 return getWindForce(series, item); 237 } 238 239 248 public Number getWindDirection(int series, int item) { 249 List oneSeriesData = (List ) this.allSeriesData.get(series); 250 WindDataItem windItem = (WindDataItem) oneSeriesData.get(item); 251 return windItem.getWindDirection(); 252 } 253 254 263 public Number getWindForce(int series, int item) { 264 List oneSeriesData = (List ) this.allSeriesData.get(series); 265 WindDataItem windItem = (WindDataItem) oneSeriesData.get(item); 266 return windItem.getWindForce(); 267 } 268 269 278 public static List seriesNameListFromDataArray(Object [][] data) { 279 280 int seriesCount = data.length; 281 List seriesNameList = new java.util.ArrayList (seriesCount); 282 for (int i = 0; i < seriesCount; i++) { 283 seriesNameList.add("Series " + (i + 1)); 284 } 285 return seriesNameList; 286 287 } 288 289 304 public boolean equals(Object obj) { 305 if (this == obj) { 306 return true; 307 } 308 if (! (obj instanceof DefaultWindDataset)) { 309 return false; 310 } 311 DefaultWindDataset that = (DefaultWindDataset) obj; 312 if (!this.seriesKeys.equals(that.seriesKeys)) { 313 return false; 314 } 315 if (!this.allSeriesData.equals(that.allSeriesData)) { 316 return false; 317 } 318 return true; 319 } 320 321 } 322 323 328 class WindDataItem implements Comparable , Serializable { 329 330 331 private Number x; 332 333 334 private Number windDir; 335 336 337 private Number windForce; 338 339 346 public WindDataItem(Number x, Number windDir, Number windForce) { 347 this.x = x; 348 this.windDir = windDir; 349 this.windForce = windForce; 350 } 351 352 357 public Number getX() { 358 return this.x; 359 } 360 361 366 public Number getWindDirection() { 367 return this.windDir; 368 } 369 370 375 public Number getWindForce() { 376 return this.windForce; 377 } 378 379 386 public int compareTo(Object object) { 387 if (object instanceof WindDataItem) { 388 WindDataItem item = (WindDataItem) object; 389 if (this.x.doubleValue() > item.x.doubleValue()) { 390 return 1; 391 } 392 else if (this.x.equals(item.x)) { 393 return 0; 394 } 395 else { 396 return -1; 397 } 398 } 399 else { 400 throw new ClassCastException ("WindDataItem.compareTo(error)"); 401 } 402 } 403 404 412 public boolean equals(Object obj) { 413 if (this == obj) { 414 return false; 415 } 416 if (! (obj instanceof WindDataItem)) { 417 return false; 418 } 419 WindDataItem that = (WindDataItem) obj; 420 if (!this.x.equals(that.x)) { 421 return false; 422 } 423 if (!this.windDir.equals(that.windDir)) { 424 return false; 425 } 426 if (!this.windForce.equals(that.windForce)) { 427 return false; 428 } 429 return true; 430 } 431 432 } 433 | Popular Tags |