| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.jcs.auxiliary.disk.jdbc.mysql.util.ScheduleParser |
|
|
| 1 | package org.apache.jcs.auxiliary.disk.jdbc.mysql.util; |
|
| 2 | ||
| 3 | /* |
|
| 4 | * Licensed to the Apache Software Foundation (ASF) under one |
|
| 5 | * or more contributor license agreements. See the NOTICE file |
|
| 6 | * distributed with this work for additional information |
|
| 7 | * regarding copyright ownership. The ASF licenses this file |
|
| 8 | * to you under the Apache License, Version 2.0 (the |
|
| 9 | * "License"); you may not use this file except in compliance |
|
| 10 | * with the License. You may obtain a copy of the License at |
|
| 11 | * |
|
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 13 | * |
|
| 14 | * Unless required by applicable law or agreed to in writing, |
|
| 15 | * software distributed under the License is distributed on an |
|
| 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
| 17 | * KIND, either express or implied. See the License for the |
|
| 18 | * specific language governing permissions and limitations |
|
| 19 | * under the License. |
|
| 20 | */ |
|
| 21 | ||
| 22 | import java.util.Calendar; |
|
| 23 | import java.util.Date; |
|
| 24 | import java.util.StringTokenizer; |
|
| 25 | ||
| 26 | /** |
|
| 27 | * Parses the very simple schedule format. |
|
| 28 | * <p> |
|
| 29 | * @author Aaron Smuts |
|
| 30 | */ |
|
| 31 | 0 | public class ScheduleParser |
| 32 | { |
|
| 33 | /** |
|
| 34 | * For each date time that is separated by a comma in the |
|
| 35 | * OptimizationSchedule, create a date and add it to an array of dates. |
|
| 36 | * <p> |
|
| 37 | * @param schedule |
|
| 38 | * @return Date[] |
|
| 39 | * @throws ScheduleFormatException |
|
| 40 | */ |
|
| 41 | public static Date[] createDatesForSchedule( String schedule ) |
|
| 42 | throws ScheduleFormatException |
|
| 43 | { |
|
| 44 | 42 | if ( schedule == null ) |
| 45 | { |
|
| 46 | 7 | throw new ScheduleFormatException( "Cannot create schedules for a null String." ); |
| 47 | } |
|
| 48 | ||
| 49 | 35 | StringTokenizer toker = new StringTokenizer( schedule, "," ); |
| 50 | 35 | Date[] dates = new Date[toker.countTokens()]; |
| 51 | 35 | int cnt = 0; |
| 52 | 105 | while ( toker.hasMoreTokens() ) |
| 53 | { |
|
| 54 | 84 | String time = toker.nextToken(); |
| 55 | 84 | dates[cnt] = getDateForSchedule( time ); |
| 56 | 70 | cnt++; |
| 57 | 70 | } |
| 58 | 21 | return dates; |
| 59 | } |
|
| 60 | ||
| 61 | /** |
|
| 62 | * For a single string it creates a date that is the next time this hh:mm:ss |
|
| 63 | * combo will be seen. |
|
| 64 | * <p> |
|
| 65 | * @param startTime |
|
| 66 | * @return |
|
| 67 | * @throws ScheduleFormatException |
|
| 68 | */ |
|
| 69 | public static Date getDateForSchedule( String startTime ) |
|
| 70 | throws ScheduleFormatException |
|
| 71 | { |
|
| 72 | 91 | if ( startTime == null ) |
| 73 | { |
|
| 74 | 7 | throw new ScheduleFormatException( "Cannot create date for a null String." ); |
| 75 | } |
|
| 76 | ||
| 77 | 84 | int firstColon = startTime.indexOf( ":" ); |
| 78 | 84 | int lastColon = startTime.lastIndexOf( ":" ); |
| 79 | 84 | int len = startTime.length(); |
| 80 | 84 | if ( firstColon == -1 || lastColon == -1 || firstColon == lastColon || lastColon == len ) |
| 81 | { |
|
| 82 | 7 | String message = "StartTime [" + startTime + "] is deformed. Unable to schedule optimizaiton."; |
| 83 | 7 | throw new ScheduleFormatException( message ); |
| 84 | } |
|
| 85 | ||
| 86 | 77 | Calendar cal = Calendar.getInstance(); |
| 87 | try |
|
| 88 | { |
|
| 89 | 77 | int hour = Integer.parseInt( startTime.substring( 0, firstColon ) ); |
| 90 | 70 | cal.set( Calendar.HOUR_OF_DAY, hour ); |
| 91 | 70 | int minute = Integer.parseInt( startTime.substring( firstColon + 1, lastColon ) ); |
| 92 | 70 | cal.set( Calendar.MINUTE, minute ); |
| 93 | 70 | int second = Integer.parseInt( startTime.substring( lastColon + 1, len ) ); |
| 94 | 70 | cal.set( Calendar.SECOND, second ); |
| 95 | } |
|
| 96 | 7 | catch ( NumberFormatException e ) |
| 97 | { |
|
| 98 | 7 | String message = "Problem parsing start time [" + startTime + "]. It should be in HH:MM:SS format."; |
| 99 | 7 | throw new ScheduleFormatException( message ); |
| 100 | 70 | } |
| 101 | ||
| 102 | // if the date is less than now, add a day. |
|
| 103 | 70 | Date now = new Date(); |
| 104 | 70 | if ( cal.getTime().before( now ) ) |
| 105 | { |
|
| 106 | 46 | cal.add( Calendar.DAY_OF_MONTH, 1 ); |
| 107 | } |
|
| 108 | ||
| 109 | 70 | return cal.getTime(); |
| 110 | } |
|
| 111 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |