View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 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   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
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      /* (non-Javadoc)
75       * @see org.jfree.data.time.RegularTimePeriod#previous()
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      /* (non-Javadoc)
89       * @see org.jfree.data.time.RegularTimePeriod#next()
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     /* (non-Javadoc)
116      * @see org.jfree.data.time.RegularTimePeriod#getSerialIndex()
117      */
118     public long getSerialIndex() {
119         return this.millis;
120     }
121 
122     /* (non-Javadoc)
123      * @see org.jfree.data.time.RegularTimePeriod#getFirstMillisecond(java.util.Calendar)
124      */
125     public long getFirstMillisecond(Calendar calendar) {
126         return this.millis;
127     }
128 
129     /* (non-Javadoc)
130      * @see org.jfree.data.time.RegularTimePeriod#getLastMillisecond(java.util.Calendar)
131      */
132     public long getLastMillisecond(Calendar calendar) {
133         return this.millis + this.timeFrame;
134     }
135 
136     /* (non-Javadoc)
137      * @see java.lang.Comparable#compareTo(java.lang.Object)
138      *
139      * Returns an integer indicating the order of this Millisecond object
140      * relative to the specified
141      * object: negative == before, zero == same, positive == after.
142      *
143      * @param o1    the object to compare.
144      *
145      * @return negative == before, zero == same, positive == after.
146      */
147 
148     public int compareTo(Object o1) {
149         final int result;
150         final long difference;
151 
152         // CASE 1 : Comparing to another Second object
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         // CASE 2 : Comparing to another TimePeriod object
171         // -----------------------------------------------
172         else if (o1 instanceof RegularTimePeriod) {
173             // more difficult case - evaluate later...
174             result = 0;
175         }
176 
177         // CASE 3 : Comparing to a non-TimePeriod object
178         // ---------------------------------------------
179         else {
180             // consider time periods to be ordered after general objects
181             result = 1;
182         }
183 
184         return result;
185     }
186 
187 }