001 package org.apache.jcs.auxiliary.disk.jdbc.mysql.util;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import java.util.Calendar;
023 import java.util.Date;
024 import java.util.StringTokenizer;
025
026 /**
027 * Parses the very simple schedule format.
028 * <p>
029 * @author Aaron Smuts
030 */
031 public class ScheduleParser
032 {
033 /**
034 * For each date time that is separated by a comma in the
035 * OptimizationSchedule, create a date and add it to an array of dates.
036 * <p>
037 * @param schedule
038 * @return Date[]
039 * @throws ScheduleFormatException
040 */
041 public static Date[] createDatesForSchedule( String schedule )
042 throws ScheduleFormatException
043 {
044 if ( schedule == null )
045 {
046 throw new ScheduleFormatException( "Cannot create schedules for a null String." );
047 }
048
049 StringTokenizer toker = new StringTokenizer( schedule, "," );
050 Date[] dates = new Date[toker.countTokens()];
051 int cnt = 0;
052 while ( toker.hasMoreTokens() )
053 {
054 String time = toker.nextToken();
055 dates[cnt] = getDateForSchedule( time );
056 cnt++;
057 }
058 return dates;
059 }
060
061 /**
062 * For a single string it creates a date that is the next time this hh:mm:ss
063 * combo will be seen.
064 * <p>
065 * @param startTime
066 * @return Date
067 * @throws ScheduleFormatException
068 */
069 public static Date getDateForSchedule( String startTime )
070 throws ScheduleFormatException
071 {
072 if ( startTime == null )
073 {
074 throw new ScheduleFormatException( "Cannot create date for a null String." );
075 }
076
077 int firstColon = startTime.indexOf( ":" );
078 int lastColon = startTime.lastIndexOf( ":" );
079 int len = startTime.length();
080 if ( firstColon == -1 || lastColon == -1 || firstColon == lastColon || lastColon == len )
081 {
082 String message = "StartTime [" + startTime + "] is deformed. Unable to schedule optimizaiton.";
083 throw new ScheduleFormatException( message );
084 }
085
086 Calendar cal = Calendar.getInstance();
087 try
088 {
089 int hour = Integer.parseInt( startTime.substring( 0, firstColon ) );
090 cal.set( Calendar.HOUR_OF_DAY, hour );
091 int minute = Integer.parseInt( startTime.substring( firstColon + 1, lastColon ) );
092 cal.set( Calendar.MINUTE, minute );
093 int second = Integer.parseInt( startTime.substring( lastColon + 1, len ) );
094 cal.set( Calendar.SECOND, second );
095 }
096 catch ( NumberFormatException e )
097 {
098 String message = "Problem parsing start time [" + startTime + "]. It should be in HH:MM:SS format.";
099 throw new ScheduleFormatException( message );
100 }
101
102 // if the date is less than now, add a day.
103 Date now = new Date();
104 if ( cal.getTime().before( now ) )
105 {
106 cal.add( Calendar.DAY_OF_MONTH, 1 );
107 }
108
109 return cal.getTime();
110 }
111 }