View Javadoc

1   /*
2    * Copyright 2004-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springmodules.validation.util.condition.date.jodatime;
18  
19  import java.util.Calendar;
20  import java.util.Date;
21  
22  import org.joda.time.DateTime;
23  import org.joda.time.DateTimeZone;
24  import org.joda.time.ReadableInstant;
25  
26  /**
27   * An {@link AbstractInstantCondition} implementation that checks whether the checked instant comes chronologically
28   * before another instant.
29   *
30   * @author Uri Boness
31   */
32  public class IsBeforeInstantCondition extends AbstractInstantCondition {
33  
34      private ReadableInstant later;
35  
36      /**
37       * Constructs a new IsBeforeDateCondition with a given instant to be checked against.
38       *
39       * @param later The instant that this instantCondition will checkCalendar against.
40       */
41      public IsBeforeInstantCondition(ReadableInstant later) {
42          this.later = later;
43      }
44  
45      /**
46       * Constructs a new IsBeforeDateCondition with a given date to be checked against.
47       *
48       * @param later The date that this instantCondition will checkCalendar against.
49       */
50      public IsBeforeInstantCondition(Date later) {
51          this.later = new DateTime(later.getTime());
52      }
53  
54      /**
55       * Constructs a new IsBeforeDateCondition with a given calendar to be checked against.
56       *
57       * @param later The calendar that this instantCondition will checkCalendar against.
58       */
59      public IsBeforeInstantCondition(Calendar later) {
60          this.later = new DateTime(later.getTimeInMillis(), DateTimeZone.forTimeZone(later.getTimeZone()));
61      }
62  
63      /**
64       * Checks whether the given instant comes chronologically before the instant associated with this instantCondition.
65       *
66       * @param instant The instance to be checked.
67       * @return <code>true</code> if the given instant comes before the instant associated with this instantCondition,
68       *         <code>false</code> otherwise.
69       */
70      protected boolean checkInstant(ReadableInstant instant) {
71          return later.isAfter(instant);
72      }
73  
74      //=============================================== Setter/Getter ====================================================
75  
76      /**
77       * Returns the instant associated with this condition.
78       *
79       * @return The instant associated with this condition.
80       */
81      public ReadableInstant getLater() {
82          return later;
83      }
84  
85  }