View Javadoc
1 /* ==================================================================== 2 * The Apache Software License, Version 1.1 3 * 4 * Copyright (c) 2003 Digital Clash LLC. All rights 5 * reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. The end-user documentation included with the redistribution, 20 * if any, must include the following acknowledgment: 21 * "This product includes software developed by the 22 * ChronicJ team (http://www.chronicj.org/)." 23 * Alternately, this acknowledgment may appear in the software itself, 24 * if and wherever such third-party acknowledgments normally appear. 25 * 26 * 4. The names "ChronicJ" and "Digital Clash" not be used to endorse or 27 * promote products derived from this software without prior written 28 * permission. For written permission, please contact 29 * info@digitalclash.com. 30 * 31 * 5. Products derived from this software may not be called "ChronicJ", 32 * "Digital Clash", nor may "ChronicJ" or "Digital Clash" appear in 33 * their name, without prior written permission of Digital Clash LLC. 34 * 35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * ==================================================================== 48 * This product includes software developed by the by the 49 * Apache Software Foundation (http://www.apache.org/). 50 * ==================================================================== 51 */ 52 package org.chronicj.impl; 53 54 import org.chronicj.DateRange; 55 import org.chronicj.TimePoint; 56 57 import java.util.Calendar; 58 import java.util.Date; 59 60 61 /*** 62 * <p> 63 * Implements support for temporal expressions that describe a recurring span 64 * of time each day or across multiple days. In pseudocode, this looks like: 65 * "starting at 11:34am and ending at 3:50pm" or "From 11pm to 2am". 66 * Expression syntax is specified using constructors. 67 * </p> 68 * 69 * <p> 70 * This class is based directly on patterns described in a paper by Martin 71 * Fowler which can be found <a 72 * href="http://martinfowler.com/apsupp/recurring.pdf">here</a>. 73 * </p> 74 * 75 * @author <a href="mlipper@US-ABP.com">Matthew Lipper</a> 76 * 77 * @see <a href="http://martinfowler.com/apsupp/recurring.pdf">Recurring Events 78 * for Calendars</a> by Martin Fowler 79 */ 80 public class RangeEachDayTE extends AbstractTemporalExpression { 81 private static final int CURRENT_DAY = 1; 82 private static final int NEXT_DAY = 2; 83 private final Calendar calendar; 84 private final DateRange range; 85 private final boolean spansMidnight; 86 87 /*** 88 * Creates a new RangeEachDayTE object. If startHour less than or equal to 89 * the endHour, then end hour is assumed to belong to the next day. As such, 90 * this class can match a maximim range of 24 hours. 91 * 92 * @param startHour int 0 to 23 93 * @param startMinute int 0 to 59 94 * @param endHour int 0 to 23 95 * @param endMinute int 0 to 59 96 */ 97 public RangeEachDayTE(int startHour, int startMinute, int endHour, 98 int endMinute) { 99 calendar = Calendar.getInstance(); 100 resetCalendar(startHour, startMinute, CURRENT_DAY); 101 102 Date startTime = calendar.getTime(); 103 System.out.println(DateUtil.format(startTime)); 104 105 if (spansMidnight(startHour, endHour)) { 106 //Treat it as the next day 107 resetCalendar(endHour, endMinute, NEXT_DAY); 108 spansMidnight = true; 109 } else { 110 //Same day 111 resetCalendar(endHour, endMinute, CURRENT_DAY); 112 spansMidnight = false; 113 } 114 115 Date endTime = calendar.getTime(); 116 117 System.out.println(DateUtil.format(endTime)); 118 119 range = new DateRange(startTime, endTime); 120 121 System.out.println(range); 122 } 123 124 /* 125 * @see org.chronicj.TemporalExpression#includes(org.chronicj.TimePoint) 126 */ 127 public boolean includes(TimePoint aTimePoint) { 128 if (spansMidnight && (aTimePoint.getHourOfDay() < 12)) { 129 //Treat it as the next day 130 resetCalendar(aTimePoint.getHourOfDay(), aTimePoint.getMinute(), 131 NEXT_DAY); 132 } else { 133 //Same day 134 resetCalendar(aTimePoint.getHourOfDay(), aTimePoint.getMinute(), 135 CURRENT_DAY); 136 } 137 138 return range.includes(new TimePoint(calendar.getTime())); 139 } 140 141 /* (non-Javadoc) 142 * @see java.lang.Object#toString() 143 */ 144 public String toString() { 145 return range.toString(); 146 } 147 148 private void resetCalendar(int hourOfDay, int minute, int dayOfMonth) { 149 calendar.clear(); 150 calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); 151 calendar.set(Calendar.MINUTE, minute); 152 calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); 153 } 154 155 private boolean spansMidnight(int startHour, int endHour) { 156 if (endHour <= startHour) { 157 return true; 158 } 159 160 return false; 161 } 162 }

This page was automatically generated by Maven