1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.statistic;
22
23 import java.util.Calendar;
24 import java.util.Date;
25
26 import org.jfree.data.time.RegularTimePeriod;
27
28 /***
29 * TODO Add docs
30 *
31 * Created on 14.12.2004
32 * Contributing Author(s):
33 *
34 * Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
35 * Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
36 *
37 * @author root
38 * @version $Revision: 1.1 $ ($Author: vlads $) $Date: 2004/12/14 06:48:15 $
39 */
40 public class MillisecondsPeriod extends RegularTimePeriod {
41
42 /*** The millisecond. */
43 private long millis;
44
45 private long timeFrame = 1;
46
47 /***
48 * Constructs a millisecond based on the current system time.
49 */
50 public MillisecondsPeriod(long period) {
51 this(new Date(), period);
52 }
53
54 /***
55 * Constructs a millisecond.
56 *
57 * @param millisecond the millisecond (same encoding as java.util.Date).
58 */
59 public MillisecondsPeriod(final long millisecond, long period) {
60 this.millis = millisecond;
61 this.timeFrame = period;
62 }
63
64 /***
65 * Constructs a millisecond.
66 *
67 * @param time the time.
68 */
69 public MillisecondsPeriod(final Date time, long period) {
70 this.millis = time.getTime();
71 this.timeFrame = period;
72 }
73
74
75
76
77 public RegularTimePeriod previous() {
78 RegularTimePeriod result = null;
79
80 final long t = this.millis;
81 if (t != Long.MIN_VALUE) {
82 result = new MillisecondsPeriod(t - timeFrame);
83 }
84
85 return result;
86 }
87
88
89
90
91 public RegularTimePeriod next() {
92 RegularTimePeriod result = null;
93
94 final long t = this.millis;
95 if (t != Long.MAX_VALUE) {
96 result = new MillisecondsPeriod(t + timeFrame);
97 }
98
99 return result;
100 }
101
102
103 public boolean equals(final Object object) {
104 if (object instanceof MillisecondsPeriod) {
105 final MillisecondsPeriod m = (MillisecondsPeriod) object;
106 return (this.millis == m.millis);
107 }
108 else {
109 return false;
110 }
111
112 }
113
114
115
116
117
118 public long getSerialIndex() {
119 return this.millis;
120 }
121
122
123
124
125 public long getFirstMillisecond(Calendar calendar) {
126 return this.millis;
127 }
128
129
130
131
132 public long getLastMillisecond(Calendar calendar) {
133 return this.millis + this.timeFrame;
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148 public int compareTo(Object o1) {
149 final int result;
150 final long difference;
151
152
153
154 if (o1 instanceof MillisecondsPeriod) {
155 final MillisecondsPeriod t1 = (MillisecondsPeriod) o1;
156 difference = this.millis - t1.millis;
157 if (difference > 0) {
158 result = 1;
159 }
160 else {
161 if (difference < 0) {
162 result = -1;
163 }
164 else {
165 result = 0;
166 }
167 }
168 }
169
170
171
172 else if (o1 instanceof RegularTimePeriod) {
173
174 result = 0;
175 }
176
177
178
179 else {
180
181 result = 1;
182 }
183
184 return result;
185 }
186
187 }