1 package net.logAnalyzer.analysis; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 import java.util.GregorianCalendar; 6 7 /*** 8 * Used to set the time period of the analysis. 9 * 10 * @author Karim REFEYTON 11 * @version 0.1 12 */ 13 public class LATimePeriod implements Comparable { 14 /*** 15 * 1 second time period. 16 */ 17 public static final LATimePeriod SECOND = new LATimePeriod("SECOND", 18 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), 10); 19 20 /*** 21 * 1 minute time period. 22 */ 23 public static final LATimePeriod MINUTE = new LATimePeriod("MINUTE", 24 new SimpleDateFormat("yyyy-MM-dd HH:mm"), 20); 25 26 /*** 27 * 10 minutes time period. 28 */ 29 public static final LATimePeriod MINUTES10 = new LATimePeriod("10MINUTES", 30 new SimpleDateFormat("yyyy-MM-dd HH:mm"), 21) { 31 /*** 32 * Set the minutes unit to <tt>0</tt>. 33 * 34 * @see net.logAnalyzer.analysis.LATimePeriod#normalizeToString(java.util.Date) 35 */ 36 public String normalizeToString(final Date date) { 37 String dateString = getDateFormat().format(date); 38 dateString = dateString.substring(0, dateString.length() - 1) + "0"; 39 return dateString; 40 } 41 }; 42 43 /*** 44 * 30 minutes time period. 45 */ 46 public static final LATimePeriod MINUTES30 = new LATimePeriod("30MINUTES", 47 new SimpleDateFormat("yyyy-MM-dd HH:mm"), 25) { 48 /*** 49 * Sets the minutes to <tt>00</tt> or <tt>30</tt>. 50 * 51 * @see net.logAnalyzer.analysis.LATimePeriod#normalizeToString(java.util.Date) 52 */ 53 public String normalizeToString(final Date date) { 54 String dateString = getDateFormat().format(date); 55 int minutes = Integer.parseInt(dateString.substring(14)); 56 dateString = dateString.substring(0, dateString.length() - 2) + "0"; 57 if (minutes < 30) { 58 dateString += "00"; 59 } else { 60 dateString += "30"; 61 } 62 return dateString; 63 } 64 }; 65 66 /*** 67 * 1 hour time period. 68 */ 69 public static final LATimePeriod HOUR = new LATimePeriod("HOUR", 70 new SimpleDateFormat("yyyy-MM-dd HH:00"), 30); 71 72 /*** 73 * 1 day time period. 74 */ 75 public static final LATimePeriod DAY = new LATimePeriod("DAY", 76 new SimpleDateFormat("yyyy-MM-dd"), 40); 77 78 /*** 79 * 1 week time period. 80 */ 81 public static final LATimePeriod WEEK = new LATimePeriod("WEEK", 82 new SimpleDateFormat("yyyy-MM-dd"), 47) { 83 /*** 84 * Set the day to the first day of week according to default 85 * localization. 86 * 87 * @see net.logAnalyzer.analysis.LATimePeriod#normalizeToString(java.util.Date) 88 */ 89 public String normalizeToString(final Date date) { 90 GregorianCalendar calendar = new GregorianCalendar(); 91 calendar.setTime(date); 92 calendar.set(GregorianCalendar.DAY_OF_WEEK, calendar 93 .getFirstDayOfWeek()); 94 String dateString = getDateFormat().format(calendar.getTime()); 95 return dateString; 96 } 97 }; 98 99 /*** 100 * 1 month time period. 101 */ 102 public static final LATimePeriod MONTH = new LATimePeriod("MONTH", 103 new SimpleDateFormat("yyyy-MM"), 50); 104 105 /*** 106 * 1 year time period. 107 */ 108 public static final LATimePeriod YEAR = new LATimePeriod("YEAR", 109 new SimpleDateFormat("yyyy"), 60); 110 111 /*** 112 * Name of the period. 113 */ 114 private String periodName; 115 116 /*** 117 * Date format used to parse or format a date for the period. 118 */ 119 private SimpleDateFormat dateFormat; 120 121 /*** 122 * Used to compare two periods. 123 */ 124 private Integer order; 125 126 /*** 127 * Constructs a new period with the defined name, format and order. 128 * 129 * @param period 130 * Name of the period (must be unique between all periods). 131 * @param dateFormat 132 * Date format to parse or format dates from/to strings. 133 * @param order 134 * Order value used to compare two periods. 135 */ 136 private LATimePeriod(String period, SimpleDateFormat dateFormat, int order) { 137 this.periodName = period; 138 this.dateFormat = dateFormat; 139 this.order = new Integer(order); 140 } 141 142 /*** 143 * Return the date format. 144 * 145 * @return Date format. 146 */ 147 public SimpleDateFormat getDateFormat() { 148 return this.dateFormat; 149 } 150 151 /*** 152 * Return the period name. 153 * 154 * @return Period name. 155 */ 156 public String getName() { 157 return this.periodName; 158 } 159 160 /*** 161 * Normalize the specified date with current date format. 162 * 163 * @param date 164 * Date to normalize 165 * @return Normalized date. 166 */ 167 public Date normalize(Date date) { 168 Date dateNormalized = null; 169 try { 170 dateNormalized = this.dateFormat.parse(normalizeToString(date)); 171 } catch (Exception e) { 172 dateNormalized = date; 173 } 174 return dateNormalized; 175 } 176 177 /*** 178 * Format the specified date to String. 179 * 180 * @param date 181 * Date to format 182 * @return Formatted date. 183 */ 184 public String normalizeToString(Date date) { 185 return this.dateFormat.format(date); 186 } 187 188 /*** 189 * Return the period associated to the specified name. 190 * 191 * @param name 192 * Period name 193 * @return Period 194 */ 195 public static LATimePeriod getPeriod(String name) { 196 if ("SECOND".equalsIgnoreCase(name)) { 197 return SECOND; 198 } else if ("MINUTE".equalsIgnoreCase(name)) { 199 return MINUTE; 200 } else if ("10MINUTES".equalsIgnoreCase(name)) { 201 return MINUTES10; 202 } else if ("HOUR".equalsIgnoreCase(name)) { 203 return HOUR; 204 } else if ("DAY".equalsIgnoreCase(name)) { 205 return DAY; 206 } else if ("WEEK".equalsIgnoreCase(name)) { 207 return WEEK; 208 } else if ("MONTH".equalsIgnoreCase(name)) { 209 return MONTH; 210 } else if ("YEAR".equalsIgnoreCase(name)) { 211 return YEAR; 212 } else { 213 return null; 214 } 215 } 216 217 /*** 218 * Compare two periods. Return <tt>0</tt> if the current period is equal 219 * to the specified period; a value lower than <tt>0</tt> if the period is 220 * lower than the specified period; a value greater than <tt>0</tt> if the 221 * period is greater than the specified period. 222 * 223 * @param anotherPeriod 224 * The period to be compared. 225 * @return <tt>0</tt> if this equals the other period; <tt>< 0</tt> 226 * if lower; <tt>> 0</tt> if greater. 227 * @see java.lang.Comparable#compareTo(java.lang.Object) 228 */ 229 public int compareTo(Object anotherPeriod) { 230 return this.order.compareTo(((LATimePeriod) anotherPeriod).order); 231 } 232 }