1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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.3 2009/08/20 17:44:05 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
121 public boolean equals(final Object rhs) {
122 if (rhs == null) {
123 return false;
124 }
125 if (!(rhs instanceof HelperTimePoint)) {
126 return false;
127 }
128 final HelperTimePoint that = (HelperTimePoint) rhs;
129
130 final boolean eq = getDate().equals(that.getDate());
131
132 return eq;
133 }
134
135 public int hashCode() {
136 return getDate().hashCode();
137 }
138
139 }