View Javadoc

1   package de.jaret.examples.timebars.linechart.model;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.TreeMap;
6   
7   import de.jaret.util.date.IntervalImpl;
8   import de.jaret.util.date.JaretDate;
9   
10  /***
11   * Interval holding data points. Begin and end will be adjusted by the first and last data point.
12   * 
13   * @author kliem
14   * @version $Id: LineChartInterval.java 856 2009-04-02 18:54:40Z kliem $
15   */
16  public class LineChartInterval extends IntervalImpl {
17      // raw points
18      private List<DataPoint> _dataPoints;
19  
20      // navigable map for quicker lookup
21      private TreeMap<Long, DataPoint> _dataMap = new TreeMap<Long, DataPoint>();
22      
23      public LineChartInterval(List<DataPoint> points) {
24          _dataPoints = points;
25          if (_dataPoints.size() < 2) {
26              throw new RuntimeException("supply at least two points");
27          }
28          
29          for (DataPoint dataPoint : points) {
30              _dataMap.put(dataPoint.getTime().getDate().getTime(), dataPoint);
31          }
32          
33      }
34  
35      public List<DataPoint> getDataPoints() {
36          return _dataPoints;
37      }
38  
39      /***
40       * {@inheritDoc} Always return the time of the first data point.
41       */
42      public JaretDate getBegin() {
43          return _dataPoints.get(0).getTime();
44      }
45  
46      /***
47       * {@inheritDoc} Always return the time of the last data point.
48       */
49      public JaretDate getEnd() {
50          return _dataPoints.get(_dataPoints.size() - 1).getTime();
51      }
52  
53      public List<DataPoint> getDataPoints(JaretDate startDate, JaretDate endDate) {
54          List<DataPoint> result = new ArrayList<DataPoint>();
55          
56  // 1.6        result.addAll(_dataMap.subMap(startDate.getDate().getTime(), true, endDate.getDate().getTime(), true).values());
57          result.addAll(_dataMap.subMap(startDate.getDate().getTime(), endDate.getDate().getTime()).values());
58          
59          return result;
60      }
61  
62  }