View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.core.api.criteria;
17  
18  import org.apache.commons.lang.builder.EqualsBuilder;
19  import org.apache.commons.lang.builder.HashCodeBuilder;
20  import org.apache.commons.lang.builder.ToStringBuilder;
21  import org.joda.time.DateTime;
22  import org.kuali.rice.core.api.util.jaxb.DateTimeAdapter;
23  
24  import javax.xml.bind.annotation.XmlAccessType;
25  import javax.xml.bind.annotation.XmlAccessorType;
26  import javax.xml.bind.annotation.XmlRootElement;
27  import javax.xml.bind.annotation.XmlType;
28  import javax.xml.bind.annotation.XmlValue;
29  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
30  import java.util.Calendar;
31  import java.util.Date;
32  
33  /**
34   * A CriteriaValue which stores date and time information in the form of a {@link Calendar} value.
35   * 
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   * 
38   */
39  @XmlRootElement(name = CriteriaDateTimeValue.Constants.ROOT_ELEMENT_NAME)
40  @XmlAccessorType(XmlAccessType.NONE)
41  @XmlType(name = CriteriaDateTimeValue.Constants.TYPE_NAME)
42  public final class CriteriaDateTimeValue implements CriteriaValue<DateTime> {
43  
44      @XmlValue
45      @XmlJavaTypeAdapter(DateTimeAdapter.class)
46      private final DateTime value;
47  
48      CriteriaDateTimeValue() {
49          this.value = null;
50      }
51  
52      CriteriaDateTimeValue(DateTime value) {
53          validateValue(value);
54          this.value = value;
55      }
56      
57      CriteriaDateTimeValue(Calendar value) {
58          validateValue(value);
59          this.value = new DateTime(value.getTimeInMillis());
60      }
61  
62      CriteriaDateTimeValue(Date value) {
63          validateValue(value);
64          this.value = new DateTime(value.getTime());
65      }
66  
67      private static void validateValue(Object value) {
68          if (value == null) {
69              throw new IllegalArgumentException("Value cannot be null.");
70          }
71      }
72  
73      @Override
74      public DateTime getValue() {
75          //defensive copy outgoing value - keeps things immutable
76          return value;
77      }
78  
79      @Override
80      public int hashCode() {
81          return HashCodeBuilder.reflectionHashCode(this);
82      }
83  
84      @Override
85      public boolean equals(Object obj) {
86          return EqualsBuilder.reflectionEquals(obj, this);
87      }
88  
89      @Override
90      public String toString() {
91          return ToStringBuilder.reflectionToString(this);
92      }
93  
94      /**
95       * Defines some internal constants used on this class.
96       */
97      static class Constants {
98          final static String ROOT_ELEMENT_NAME = "dateTimeValue";
99          final static String TYPE_NAME = "CriteriaDateTimeValueType";
100     }
101 
102 }