/** Some checks for the getDataItem(int) method. */ @Test public void testGetDataItem1() { TimeSeries series = new TimeSeries("S", Year.class); // can't get anything yet...just an exception boolean pass = false; try { /*TimeSeriesDataItem item =*/ series.getDataItem(0); } catch (IndexOutOfBoundsException e) { pass = true; } assertTrue(pass); series.add(new Year(2006), 100.0); TimeSeriesDataItem item = series.getDataItem(0); assertEquals(new Year(2006), item.getPeriod()); pass = false; try { /*item = */ series.getDataItem(-1); } catch (IndexOutOfBoundsException e) { pass = true; } assertTrue(pass); pass = false; try { /*item = */ series.getDataItem(1); } catch (IndexOutOfBoundsException e) { pass = true; } assertTrue(pass); }
/** * Create a TimeSeriesDataItem, add it to a TimeSeries. Now, modifying the original * TimeSeriesDataItem should NOT affect the TimeSeries. */ @Test public void testAdd_TimeSeriesDataItem() { TimeSeriesDataItem item = new TimeSeriesDataItem(new Year(2009), 1.0); TimeSeries series = new TimeSeries("S1"); series.add(item); assertTrue(item.equals(series.getDataItem(0))); item.setValue(new Double(99.9)); assertFalse(item.equals(series.getDataItem(0))); }
public void chartProgress(ChartProgressEvent chartprogressevent) { if (chartprogressevent.getType() != 2) return; if (chartPanel != null) { JFreeChart jfreechart = chartPanel.getChart(); if (jfreechart != null) { XYPlot xyplot = (XYPlot) jfreechart.getPlot(); XYDataset xydataset = xyplot.getDataset(); @SuppressWarnings("rawtypes") Comparable comparable = xydataset.getSeriesKey(0); double d = xyplot.getDomainCrosshairValue(); model.setValueAt(comparable, 0, 0); long l = (long) d; model.setValueAt(new Long(l), 0, 1); int i = series.getIndex(new Minute(new Date(l))); if (i >= 0) { TimeSeriesDataItem timeseriesdataitem = series.getDataItem(Math.min(199, Math.max(0, i))); TimeSeriesDataItem timeseriesdataitem1 = series.getDataItem(Math.max(0, i - 1)); TimeSeriesDataItem timeseriesdataitem2 = series.getDataItem(Math.min(199, i + 1)); long l1 = timeseriesdataitem.getPeriod().getMiddleMillisecond(); double d1 = timeseriesdataitem.getValue().doubleValue(); long l2 = timeseriesdataitem1.getPeriod().getMiddleMillisecond(); double d2 = timeseriesdataitem1.getValue().doubleValue(); long l3 = timeseriesdataitem2.getPeriod().getMiddleMillisecond(); double d3 = timeseriesdataitem2.getValue().doubleValue(); model.setValueAt(new Long(l1), 0, 1); model.setValueAt(new Double(d1), 0, 2); model.setValueAt(new Long(l2), 0, 3); model.setValueAt(new Double(d2), 0, 4); model.setValueAt(new Long(l3), 0, 5); model.setValueAt(new Double(d3), 0, 6); } } } }
/** * Test the setMaximumItemCount() method to ensure that it removes items from the series if * necessary. */ @Test public void testSetMaximumItemCount() { TimeSeries s1 = new TimeSeries("S1", Year.class); s1.add(new Year(2000), 13.75); s1.add(new Year(2001), 11.90); s1.add(new Year(2002), null); s1.add(new Year(2005), 19.32); s1.add(new Year(2007), 16.89); assertTrue(s1.getItemCount() == 5); s1.setMaximumItemCount(3); assertTrue(s1.getItemCount() == 3); TimeSeriesDataItem item = s1.getDataItem(0); assertTrue(item.getPeriod().equals(new Year(2002))); assertEquals(16.89, s1.getMinY(), EPSILON); assertEquals(19.32, s1.getMaxY(), EPSILON); }
/** Test the equals() method. */ @Test public void testEquals() { TimeSeriesDataItem item1 = new TimeSeriesDataItem(new Day(23, 9, 2001), 99.7); TimeSeriesDataItem item2 = new TimeSeriesDataItem(new Day(23, 9, 2001), 99.7); assertTrue(item1.equals(item2)); assertTrue(item2.equals(item1)); item1.setValue(new Integer(5)); assertFalse(item1.equals(item2)); item2.setValue(new Integer(5)); assertTrue(item1.equals(item2)); }
/** * Returns an integer indicating the order of this data pair object relative to another object. * * <p>For the order we consider only the timing: negative == before, zero == same, positive == * after. * * @param o1 The object being compared to. * @return An integer indicating the order of the data item object relative to another object. */ public int compareTo(Object o1) { int result; // CASE 1 : Comparing to another TimeSeriesDataItem object // ------------------------------------------------------- if (o1 instanceof TimeSeriesDataItem) { TimeSeriesDataItem datapair = (TimeSeriesDataItem) o1; result = getPeriod().compareTo(datapair.getPeriod()); } // CASE 2 : Comparing to a general object // --------------------------------------------- else { // consider time periods to be ordered after general objects result = 1; } return result; }
/** Some more checks for the addOrUpdate() method. */ @Test public void testAddOrUpdate4() { TimeSeries ts = new TimeSeries("S"); TimeSeriesDataItem overwritten = ts.addOrUpdate(new Year(2009), 20.09); assertNull(overwritten); overwritten = ts.addOrUpdate(new Year(2009), 1.0); assertEquals(new Double(20.09), overwritten.getValue()); assertEquals(new Double(1.0), ts.getValue(new Year(2009))); // changing the overwritten record shouldn't affect the series overwritten.setValue(null); assertEquals(new Double(1.0), ts.getValue(new Year(2009))); TimeSeriesDataItem item = new TimeSeriesDataItem(new Year(2010), 20.10); overwritten = ts.addOrUpdate(item); assertNull(overwritten); assertEquals(new Double(20.10), ts.getValue(new Year(2010))); // changing the item that was added should not change the series item.setValue(null); assertEquals(new Double(20.10), ts.getValue(new Year(2010))); }
/** * Handles a chart progress event. * * @param event the event. */ public void chartProgress(ChartProgressEvent event) { if (event.getType() != ChartProgressEvent.DRAWING_FINISHED) { return; } if (this.chartPanel != null) { JFreeChart c = this.chartPanel.getChart(); if (c != null) { XYPlot plot = c.getXYPlot(); XYDataset dataset = plot.getDataset(); Comparable seriesKey = dataset.getSeriesKey(0); double xx = plot.getDomainCrosshairValue(); // update the table... this.model.setValueAt(seriesKey, 0, 0); long millis = (long) xx; this.model.setValueAt(new Long(millis), 0, 1); int itemIndex = this.series.getIndex(new Minute(new Date(millis))); if (itemIndex >= 0) { TimeSeriesDataItem item = this.series.getDataItem(Math.min(199, Math.max(0, itemIndex))); TimeSeriesDataItem prevItem = this.series.getDataItem(Math.max(0, itemIndex - 1)); TimeSeriesDataItem nextItem = this.series.getDataItem(Math.min(199, itemIndex + 1)); long x = item.getPeriod().getMiddleMillisecond(); double y = item.getValue().doubleValue(); long prevX = prevItem.getPeriod().getMiddleMillisecond(); double prevY = prevItem.getValue().doubleValue(); long nextX = nextItem.getPeriod().getMiddleMillisecond(); double nextY = nextItem.getValue().doubleValue(); this.model.setValueAt(new Long(x), 0, 1); this.model.setValueAt(new Double(y), 0, 2); this.model.setValueAt(new Long(prevX), 0, 3); this.model.setValueAt(new Double(prevY), 0, 4); this.model.setValueAt(new Long(nextX), 0, 5); this.model.setValueAt(new Double(nextY), 0, 6); } } } }
/** * Tests the correct output of a DataSet to a TimeSeries by outputting it, then iterating through * TimeSeries object checking the correct values were stored/output. */ public void testOutput() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, InstantiationException { // Constants used to determine size of test int NUMBER_OF_TIME_PERIODS = 10; String TIME_VARIABLE = "t"; // Set up array for expected results double expectedValue[] = new double[NUMBER_OF_TIME_PERIODS]; // We'll set up periods starting from today RegularTimePeriod period = new Day(); // Create a test DataSet for output // - note that only one independent variable (the time variable) // will be output. This is expected. DataSet dataSet = new DataSet(); dataSet.setTimeVariable(TIME_VARIABLE); for (int d = 0; d < NUMBER_OF_TIME_PERIODS; d++) { double value = (double) d; DataPoint obs = new Observation(value); obs.setIndependentValue(TIME_VARIABLE, period.getMiddleMillisecond()); dataSet.add(obs); period = period.next(); expectedValue[d] = value; } assertEquals( "Checking only one independent variable exists in dataSet", 1, dataSet.getIndependentVariables().length); assertEquals( "Checking dataSet has correct number of entries", NUMBER_OF_TIME_PERIODS, dataSet.size()); // Create TimeSeriesOutputter and use it to output dataSet TimeSeries timeSeries = new TimeSeries("test"); TimeSeriesOutputter outputter = new TimeSeriesOutputter(timeSeries, period.getClass()); outputter.output(dataSet); assertEquals( "Checking number of items in time series", NUMBER_OF_TIME_PERIODS, timeSeries.getItemCount()); // Reset period to start checking from today onwards period = new Day(); for (int d = 0; d < NUMBER_OF_TIME_PERIODS; d++) { TimeSeriesDataItem dataItem = timeSeries.getDataItem(d); period = dataItem.getPeriod(); assertNotNull("Checking time period", period); long timeValue = period.getMiddleMillisecond(); assertTrue( "Checking time periods match", (double) timeValue >= period.getFirstMillisecond() && (double) timeValue <= period.getLastMillisecond()); assertEquals( "Checking values for period " + dataItem.getPeriod() + " match", expectedValue[d], dataItem.getValue().doubleValue(), TOLERANCE); period = period.next(); } }
/** * Test that an instance is equal to itself. * * <p>SourceForge Bug ID: 558850. */ @Test public void testEqualsSelf() { TimeSeriesDataItem item = new TimeSeriesDataItem(new Day(23, 9, 2001), 99.7); assertTrue(item.equals(item)); }