View Javadoc

1   package org.codehaus.mojo.dashboard.report.plugin.utils;
2   
3   /*
4    * Copyright 2007 David Vicente
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.text.SimpleDateFormat;
20  import java.util.Date;
21  import java.util.GregorianCalendar;
22  
23  /**
24   * Used to set the time period of the analysis.
25   * 
26   * @author Karim REFEYTON
27   * @version 0.1
28   */
29  public class TimePeriod implements Comparable
30  {
31  
32      /**
33       * 1 minute time period.
34       */
35      public static final TimePeriod MINUTE = new TimePeriod( "MINUTE", new SimpleDateFormat( "yyyy-MM-dd HH:mm" ), 20 );
36  
37      /**
38       * 1 hour time period.
39       */
40      public static final TimePeriod HOUR = new TimePeriod( "HOUR", new SimpleDateFormat( "yyyy-MM-dd HH:00" ), 30 );
41  
42      /**
43       * 1 day time period.
44       */
45      public static final TimePeriod DAY = new TimePeriod( "DAY", new SimpleDateFormat( "yyyy-MM-dd" ), 40 );
46  
47      /**
48       * 1 week time period.
49       */
50      public static final TimePeriod WEEK = new TimePeriod( "WEEK", new SimpleDateFormat( "yyyy-MM-dd" ), 47 )
51      {
52          /**
53           * Set the day to the first day of week according to default localization.
54           * 
55           * @see net.logAnalyzer.analysis.TimePeriod#normalizeToString(java.util.Date)
56           */
57          public String normalizeToString( final Date date )
58          {
59              GregorianCalendar calendar = new GregorianCalendar();
60              calendar.setTime( date );
61              calendar.set( GregorianCalendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() );
62              String dateString = getDateFormat().format( calendar.getTime() );
63              return dateString;
64          }
65      };
66  
67      /**
68       * 1 month time period.
69       */
70      public static final TimePeriod MONTH = new TimePeriod( "MONTH", new SimpleDateFormat( "yyyy-MM" ), 50 );
71  
72      /**
73       * Name of the period.
74       */
75      private String periodName;
76  
77      /**
78       * Date format used to parse or format a date for the period.
79       */
80      private SimpleDateFormat dateFormat;
81  
82      /**
83       * Used to compare two periods.
84       */
85      private Integer order;
86  
87      /**
88       * Constructs a new period with the defined name, format and order.
89       * 
90       * @param period
91       *            Name of the period (must be unique between all periods).
92       * @param dateFormat
93       *            Date format to parse or format dates from/to strings.
94       * @param order
95       *            Order value used to compare two periods.
96       */
97      private TimePeriod( String period, SimpleDateFormat dateFormat, int order )
98      {
99          this.periodName = period;
100         this.dateFormat = dateFormat;
101         this.order = new Integer( order );
102     }
103 
104     /**
105      * Return the date format.
106      * 
107      * @return Date format.
108      */
109     public SimpleDateFormat getDateFormat()
110     {
111         return this.dateFormat;
112     }
113 
114     /**
115      * Return the period name.
116      * 
117      * @return Period name.
118      */
119     public String getName()
120     {
121         return this.periodName;
122     }
123 
124     /**
125      * Normalize the specified date with current date format.
126      * 
127      * @param date
128      *            Date to normalize
129      * @return Normalized date.
130      */
131     public Date normalize( Date date )
132     {
133         Date dateNormalized = null;
134         try
135         {
136             dateNormalized = this.dateFormat.parse( normalizeToString( date ) );
137         }
138         catch ( Exception e )
139         {
140             dateNormalized = date;
141         }
142         return dateNormalized;
143     }
144 
145     /**
146      * Format the specified date to String.
147      * 
148      * @param date
149      *            Date to format
150      * @return Formatted date.
151      */
152     public String normalizeToString( Date date )
153     {
154         return this.dateFormat.format( date );
155     }
156 
157     /**
158      * Return the period associated to the specified name.
159      * 
160      * @param name
161      *            Period name
162      * @return Period
163      */
164     public static TimePeriod getPeriod( String name )
165     {
166         if ( "MINUTE".equalsIgnoreCase( name ) )
167         {
168             return MINUTE;
169         }
170         else if ( "HOUR".equalsIgnoreCase( name ) )
171         {
172             return HOUR;
173         }
174         else if ( "DAY".equalsIgnoreCase( name ) )
175         {
176             return DAY;
177         }
178         else if ( "WEEK".equalsIgnoreCase( name ) )
179         {
180             return WEEK;
181         }
182         else if ( "MONTH".equalsIgnoreCase( name ) )
183         {
184             return MONTH;
185         }
186         else
187         {
188             return DAY;
189         }
190     }
191 
192     /**
193      * Compare two periods. Return <tt>0</tt> if the current period is equal to the specified period; a value lower
194      * than <tt>0</tt> if the period is lower than the specified period; a value greater than <tt>0</tt> if the
195      * period is greater than the specified period.
196      * 
197      * @param anotherPeriod
198      *            The period to be compared.
199      * @return <tt>0</tt> if this equals the other period; <tt>&lt; 0</tt> if lower; <tt>&gt; 0</tt> if greater.
200      * @see java.lang.Comparable#compareTo(java.lang.Object)
201      */
202     public int compareTo( Object anotherPeriod )
203     {
204         return this.order.compareTo( ( (TimePeriod) anotherPeriod ).order );
205     }
206 }