001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -----------------------
028 * DefaultWindDataset.java
029 * -----------------------
030 * (C) Copyright 2001-2016, by Achilleus Mantzios and Contributors.
031 *
032 * Original Author:  Achilleus Mantzios;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *
035 * Changes
036 * -------
037 * 06-Feb-2002 : Version 1, based on code contributed by Achilleus
038 *               Mantzios (DG);
039 * 05-May-2004 : Now extends AbstractXYDataset (DG);
040 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
041 *               getYValue() (DG);
042 * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
043 * 22-Apr-2008 : Implemented PublicCloneable (DG);
044 * 03-Jul-2013 : Use ParamChecks (DG);
045 *
046 */
047
048package org.jfree.data.xy;
049
050import java.io.Serializable;
051import java.util.Arrays;
052import java.util.Collections;
053import java.util.Date;
054import java.util.List;
055import org.jfree.chart.util.Args;
056import org.jfree.chart.util.PublicCloneable;
057
058/**
059 * A default implementation of the {@link WindDataset} interface.
060 */
061public class DefaultWindDataset extends AbstractXYDataset
062        implements WindDataset, PublicCloneable {
063
064    /** The keys for the series. */
065    private List seriesKeys;
066
067    /** Storage for the series data. */
068    private List allSeriesData;
069
070    /**
071     * Constructs a new, empty, dataset.  Since there are currently no methods
072     * to add data to an existing dataset, you should probably use a different
073     * constructor.
074     */
075    public DefaultWindDataset() {
076        this.seriesKeys = new java.util.ArrayList();
077        this.allSeriesData = new java.util.ArrayList();
078    }
079
080    /**
081     * Constructs a dataset based on the specified data array.
082     *
083     * @param data  the data ({@code null} not permitted).
084     *
085     * @throws NullPointerException if {@code data} is {@code null}.
086     */
087    public DefaultWindDataset(Object[][][] data) {
088        this(seriesNameListFromDataArray(data), data);
089    }
090
091    /**
092     * Constructs a dataset based on the specified data array.
093     *
094     * @param seriesNames  the names of the series ({@code null} not
095     *     permitted).
096     * @param data  the wind data.
097     *
098     * @throws NullPointerException if {@code seriesNames} is {@code null}.
099     */
100    public DefaultWindDataset(String[] seriesNames, Object[][][] data) {
101        this(Arrays.asList(seriesNames), data);
102    }
103
104    /**
105     * Constructs a dataset based on the specified data array.  The array
106     * can contain multiple series, each series can contain multiple items,
107     * and each item is as follows:
108     * <ul>
109     * <li>{@code data[series][item][0]} - the date (either a
110     *   {@code Date} or a {@code Number} that is the milliseconds
111     *   since 1-Jan-1970);</li>
112     * <li>{@code data[series][item][1]} - the wind direction (1 - 12,
113     *   like the numbers on a clock face);</li>
114     * <li>{@code data[series][item][2]} - the wind force (1 - 12 on the
115     *   Beaufort scale)</li>
116     * </ul>
117     *
118     * @param seriesKeys  the names of the series ({@code null} not
119     *     permitted).
120     * @param data  the wind dataset ({@code null} not permitted).
121     *
122     * @throws IllegalArgumentException if {@code seriesKeys} is
123     *     {@code null}.
124     * @throws IllegalArgumentException if the number of series keys does not
125     *     match the number of series in the array.
126     * @throws NullPointerException if {@code data} is {@code null}.
127     */
128    public DefaultWindDataset(List seriesKeys, Object[][][] data) {
129        Args.nullNotPermitted(seriesKeys, "seriesKeys");
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 = xDate.getTime();
152                        }
153                        else {
154                            xNumber = 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    /**
170     * Returns the number of series in the dataset.
171     *
172     * @return The series count.
173     */
174    @Override
175    public int getSeriesCount() {
176        return this.allSeriesData.size();
177    }
178
179    /**
180     * Returns the number of items in a series.
181     *
182     * @param series  the series (zero-based index).
183     *
184     * @return The item count.
185     */
186    @Override
187    public int getItemCount(int series) {
188        if (series < 0 || series >= getSeriesCount()) {
189            throw new IllegalArgumentException("Invalid series index: "
190                    + series);
191        }
192        List oneSeriesData = (List) this.allSeriesData.get(series);
193        return oneSeriesData.size();
194    }
195
196    /**
197     * Returns the key for a series.
198     *
199     * @param series  the series (zero-based index).
200     *
201     * @return The series key.
202     */
203    @Override
204    public Comparable getSeriesKey(int series) {
205        if (series < 0 || series >= getSeriesCount()) {
206            throw new IllegalArgumentException("Invalid series index: "
207                    + series);
208        }
209        return (Comparable) this.seriesKeys.get(series);
210    }
211
212    /**
213     * Returns the x-value for one item within a series.  This should represent
214     * a point in time, encoded as milliseconds in the same way as
215     * java.util.Date.
216     *
217     * @param series  the series (zero-based index).
218     * @param item  the item (zero-based index).
219     *
220     * @return The x-value for the item within the series.
221     */
222    @Override
223    public Number getX(int series, int item) {
224        List oneSeriesData = (List) this.allSeriesData.get(series);
225        WindDataItem windItem = (WindDataItem) oneSeriesData.get(item);
226        return windItem.getX();
227    }
228
229    /**
230     * Returns the y-value for one item within a series.  This maps to the
231     * {@link #getWindForce(int, int)} method and is implemented because
232     * {@code WindDataset} is an extension of {@link XYDataset}.
233     *
234     * @param series  the series (zero-based index).
235     * @param item  the item (zero-based index).
236     *
237     * @return The y-value for the item within the series.
238     */
239    @Override
240    public Number getY(int series, int item) {
241        return getWindForce(series, item);
242    }
243
244    /**
245     * Returns the wind direction for one item within a series.  This is a
246     * number between 0 and 12, like the numbers on an upside-down clock face.
247     *
248     * @param series  the series (zero-based index).
249     * @param item  the item (zero-based index).
250     *
251     * @return The wind direction for the item within the series.
252     */
253    @Override
254    public Number getWindDirection(int series, int item) {
255        List oneSeriesData = (List) this.allSeriesData.get(series);
256        WindDataItem windItem = (WindDataItem) oneSeriesData.get(item);
257        return windItem.getWindDirection();
258    }
259
260    /**
261     * Returns the wind force for one item within a series.  This is a number
262     * between 0 and 12, as defined by the Beaufort scale.
263     *
264     * @param series  the series (zero-based index).
265     * @param item  the item (zero-based index).
266     *
267     * @return The wind force for the item within the series.
268     */
269    @Override
270    public Number getWindForce(int series, int item) {
271        List oneSeriesData = (List) this.allSeriesData.get(series);
272        WindDataItem windItem = (WindDataItem) oneSeriesData.get(item);
273        return windItem.getWindForce();
274    }
275
276    /**
277     * Utility method for automatically generating series names.
278     *
279     * @param data  the wind data ({@code null} not permitted).
280     *
281     * @return An array of <i>Series N</i> with N = { 1 .. data.length }.
282     *
283     * @throws NullPointerException if {@code data} is {@code null}.
284     */
285    public static List seriesNameListFromDataArray(Object[][] data) {
286        int seriesCount = data.length;
287        List seriesNameList = new java.util.ArrayList(seriesCount);
288        for (int i = 0; i < seriesCount; i++) {
289            seriesNameList.add("Series " + (i + 1));
290        }
291        return seriesNameList;
292    }
293
294    /**
295     * Checks this {@code WindDataset} for equality with an arbitrary
296     * object.  This method returns {@code true} if and only if:
297     * <ul>
298     *   <li>{@code obj} is not {@code null};</li>
299     *   <li>{@code obj} is an instance of {@code DefaultWindDataset};</li>
300     *   <li>both datasets have the same number of series containing identical
301     *       values.</li>
302     * </ul>
303     *
304     * @param obj  the object ({@code null} permitted).
305     *
306     * @return A boolean.
307     */
308    @Override
309    public boolean equals(Object obj) {
310        if (this == obj) {
311            return true;
312        }
313        if (!(obj instanceof DefaultWindDataset)) {
314            return false;
315        }
316        DefaultWindDataset that = (DefaultWindDataset) obj;
317        if (!this.seriesKeys.equals(that.seriesKeys)) {
318            return false;
319        }
320        if (!this.allSeriesData.equals(that.allSeriesData)) {
321            return false;
322        }
323        return true;
324    }
325
326}
327
328/**
329 * A wind data item.
330 */
331class WindDataItem implements Comparable, Serializable {
332
333    /** The x-value. */
334    private Number x;
335
336    /** The wind direction. */
337    private Number windDir;
338
339    /** The wind force. */
340    private Number windForce;
341
342    /**
343     * Creates a new wind data item.
344     *
345     * @param x  the x-value.
346     * @param windDir  the direction.
347     * @param windForce  the force.
348     */
349    public WindDataItem(Number x, Number windDir, Number windForce) {
350        this.x = x;
351        this.windDir = windDir;
352        this.windForce = windForce;
353    }
354
355    /**
356     * Returns the x-value.
357     *
358     * @return The x-value.
359     */
360    public Number getX() {
361        return this.x;
362    }
363
364    /**
365     * Returns the wind direction.
366     *
367     * @return The wind direction.
368     */
369    public Number getWindDirection() {
370        return this.windDir;
371    }
372
373    /**
374     * Returns the wind force.
375     *
376     * @return The wind force.
377     */
378    public Number getWindForce() {
379        return this.windForce;
380    }
381
382    /**
383     * Compares this item to another object.
384     *
385     * @param object  the other object.
386     *
387     * @return An int that indicates the relative comparison.
388     */
389    @Override
390    public int compareTo(Object object) {
391        if (object instanceof WindDataItem) {
392            WindDataItem item = (WindDataItem) object;
393            if (this.x.doubleValue() > item.x.doubleValue()) {
394                return 1;
395            }
396            else if (this.x.equals(item.x)) {
397                return 0;
398            }
399            else {
400                return -1;
401            }
402        }
403        else {
404            throw new ClassCastException("WindDataItem.compareTo(error)");
405        }
406    }
407
408    /**
409     * Tests this {@code WindDataItem} for equality with an arbitrary
410     * object.
411     *
412     * @param obj  the object ({@code null} permitted).
413     *
414     * @return A boolean.
415     */
416    @Override
417    public boolean equals(Object obj) {
418        if (this == obj) {
419            return false;
420        }
421        if (!(obj instanceof WindDataItem)) {
422            return false;
423        }
424        WindDataItem that = (WindDataItem) obj;
425        if (!this.x.equals(that.x)) {
426            return false;
427        }
428        if (!this.windDir.equals(that.windDir)) {
429            return false;
430        }
431        if (!this.windForce.equals(that.windForce)) {
432            return false;
433        }
434        return true;
435    }
436
437}