View Javadoc

1   /*
2   	StatCvs - CVS statistics generation 
3   	Copyright (C) 2002  Lukasz Pekacki <lukasz@pekacki.de>
4   	http://statcvs.sf.net/
5       
6   	This library is free software; you can redistribute it and/or
7   	modify it under the terms of the GNU Lesser General Public
8   	License as published by the Free Software Foundation; either
9   	version 2.1 of the License, or (at your option) any later version.
10  
11  	This library is distributed in the hope that it will be useful,
12  	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  	Lesser General Public License for more details.
15  
16  	You should have received a copy of the GNU Lesser General Public
17  	License along with this library; if not, write to the Free Software
18  	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19      
20  	$RCSfile: HelperTimePoint.java,v $
21  	$Date: 2008/04/02 11:22:14 $
22  */
23  package net.sf.statcvs.reportmodel;
24  
25  import java.util.Date;
26  
27  /**
28   * Helper class for {@link TimeLine}. It encapsulates a data point
29   * in the time line and may have an absolute value or a delta value (change).
30   * 
31   * @author Richard Cyganiak <rcyg@gmx.de>
32   * @version $Id: HelperTimePoint.java,v 1.2 2008/04/02 11:22:14 benoitx Exp $
33   */
34  class HelperTimePoint implements Comparable {
35      private final Date date;
36      private final int value;
37      private final boolean isAbsolute;
38  
39      private HelperTimePoint(final Date date, final int value, final boolean isAbsolute) {
40          this.date = date;
41          this.value = value;
42          this.isAbsolute = isAbsolute;
43      }
44  
45      /**
46       * Creates a new time point having an absolute value.
47       * @param date the time point's date
48       * @param value the time point's value
49       * @return a new time point
50       */
51      public static HelperTimePoint createAbsoluteValueTimePoint(final Date date, final int value) {
52          return new HelperTimePoint(date, value, true);
53      }
54  
55      /**
56       * Creates a new time point representing a change relative to the previous
57       * time point's value.
58       * @param date the time point's date
59       * @param delta the time point's change relative to the previous value
60       * @return a new time point
61       */
62      public static HelperTimePoint createDeltaTimePoint(final Date date, final int delta) {
63          return new HelperTimePoint(date, delta, false);
64      }
65  
66      /**
67       * Creates a new time point representing two time points with the same
68       * time. If one of them is absolute, it will be returned. If both
69       * are relative, a new time point with the sum of the relative
70       * values will be returned.
71       * @param other a <tt>HelperTimePoint</tt>
72       * @return a time point representing both <tt>this</tt> and <tt>other</tt>
73       */
74      public HelperTimePoint join(final HelperTimePoint other) {
75          if (!date.equals(other.getDate())) {
76              throw new IllegalArgumentException("Can only add time points having the same date");
77          }
78          if (isAbsolute) {
79              return this;
80          }
81          if (other.isAbsolute()) {
82              return other;
83          }
84          return HelperTimePoint.createDeltaTimePoint(date, value + other.getValue());
85      }
86  
87      /**
88       * @return Date the data point's time
89       */
90      public Date getDate() {
91          return date;
92      }
93  
94      /**
95       * @return int the data point's value
96       */
97      public int getValue() {
98          return value;
99      }
100 
101     /**
102      * Returns <tt>true</tt> if the time point has an absolute value, and
103      * <tt>false</tt> if it has a delta value relative to the previous
104      * time point. 
105      * @return <tt>true</tt> if absolute value
106      */
107     public boolean isAbsolute() {
108         return isAbsolute;
109     }
110 
111     /**
112      * Compares the time point's date to that of another time point
113      * @param o another time point
114      * @return See {@link java.lang.Comparable#compareTo(java.lang.Object)}
115      */
116     public int compareTo(final Object o) {
117         final HelperTimePoint other = (HelperTimePoint) o;
118         return getDate().compareTo(other.getDate());
119     }
120 }