View Javadoc
1   /**
2    * Copyright 2005-2016 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  
22  import javax.xml.bind.annotation.XmlAccessType;
23  import javax.xml.bind.annotation.XmlAccessorType;
24  import javax.xml.bind.annotation.XmlRootElement;
25  import javax.xml.bind.annotation.XmlType;
26  import javax.xml.bind.annotation.XmlValue;
27  import java.math.BigDecimal;
28  
29  /**
30   * A CriteriaValue which stores date and time information in the form of a
31   * {@link BigDecimal} value.
32   * 
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   *
35   */
36  @XmlRootElement(name = CriteriaDecimalValue.Constants.ROOT_ELEMENT_NAME)
37  @XmlAccessorType(XmlAccessType.NONE)
38  @XmlType(name = CriteriaDecimalValue.Constants.TYPE_NAME)
39  public final class CriteriaDecimalValue implements CriteriaValue<BigDecimal> {
40  
41      @XmlValue
42      private final BigDecimal value;
43      
44      CriteriaDecimalValue() {
45          this.value = null;
46      }
47      
48      CriteriaDecimalValue(BigDecimal value) {
49      	validateValue(value);
50          this.value = safeInstance(value);
51      }
52      
53      CriteriaDecimalValue(Number value) {
54      	validateValue(value);
55      	this.value = BigDecimal.valueOf(value.doubleValue());
56      }
57      
58      private static void validateValue(Object value) {
59      	if (value == null) {
60      		throw new IllegalArgumentException("Value cannot be null.");
61      	}
62      }
63  
64      /**
65       * Since BigDecimal is not technically immutable we defensively copy when needed.
66       *
67       * <p>
68       * See Effective Java 2nd ed. page 79 for details.
69       * </p>
70       *
71       * @param val the big decimal to check
72       * @return the safe BigDecimal
73       */
74      private static BigDecimal safeInstance(BigDecimal val) {
75          if (val.getClass() != BigDecimal.class) {
76              return new BigDecimal(val.toPlainString());
77          }
78          return val;
79      }
80      
81      @Override
82      public BigDecimal getValue() {
83          return value;
84      }
85      
86      @Override
87      public int hashCode() {
88          return HashCodeBuilder.reflectionHashCode(this);
89      }
90  
91      @Override
92      public boolean equals(Object obj) {
93          return EqualsBuilder.reflectionEquals(obj, this);
94      }
95  
96      @Override
97      public String toString() {
98          return ToStringBuilder.reflectionToString(this);
99      }
100     
101     /**
102      * Defines some internal constants used on this class.
103      */
104     static class Constants {
105         final static String ROOT_ELEMENT_NAME = "decimalValue";
106         final static String TYPE_NAME = "CriteriaDecimalValueType";
107     }
108     
109 }