View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.custom.schedule.model;
21  
22  import java.util.Date;
23  
24  /**
25   * <p>
26   * This class represents an interval of up to half an hour.
27   * The interval will always round up to the next half hour
28   * e.g. a start time of 14:15 will generate an interval with
29   * end time of 14:30.
30   * </p>
31   * @since 1.1.7
32   * @author Peter Mahoney
33   * @version $Revision: 371736 $
34   */
35  public class HalfHourInterval extends Interval {
36  
37      public static final long HALF_HOUR = 1000 * 60 * 30;
38  
39      public HalfHourInterval(Date startTime, Date maxEnd)
40      {
41          super(null, startTime, new Date(Math.min(startTime.getTime() + HALF_HOUR, maxEnd.getTime())));
42      }
43  
44      private HalfHourInterval(String label, Date startTime, Date endTime)
45      {
46          super(label, startTime, endTime); 
47      }
48      
49      /**
50       * Create a new half hour interval following on from the supplied interval.
51       * The interval will be anything up to half an hour, depending on when the
52       * end of the previous interval was. If an interval cannot be fitted between
53       * the end of the last interval and the maximum end time, null will be returned.
54       * 
55       * @param interval The previous interval
56       * @param maxEnd The maximum end time of the new interval
57       * @return The next half hour interval
58       */
59      public static Interval next(Interval interval, Date maxEnd) {
60          Date startTime = interval.getEndTime();
61          
62          if (startTime.before(maxEnd))
63          {
64              Date endTime = new Date(Math.min(startTime.getTime() - (startTime.getTime() % HALF_HOUR) + HALF_HOUR, maxEnd.getTime()));
65              
66              return new HalfHourInterval(null, startTime, endTime);
67          }
68          else
69          {
70              
71              return null;
72          }
73      }
74  }