View Javadoc
1   /**
2    * Copyright 2005-2014 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.kuali.rice.core.api.util.type.KualiDecimal;
22  import org.kuali.rice.core.api.util.type.KualiPercent;
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 java.math.BigDecimal;
30  
31  /**
32   * A CriteriaValue which stores {@link org.kuali.rice.core.api.util.type.KualiDecimal} information in the form of a
33   * {@link java.math.BigDecimal} value.
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   *
37   */
38  @XmlRootElement(name = CriteriaKualiDecimalValue.Constants.ROOT_ELEMENT_NAME)
39  @XmlAccessorType(XmlAccessType.NONE)
40  @XmlType(name = CriteriaKualiDecimalValue.Constants.TYPE_NAME)
41  public final class CriteriaKualiDecimalValue implements CriteriaValue<KualiDecimal> {
42  
43      @XmlValue
44      private final BigDecimal value;
45  
46      CriteriaKualiDecimalValue() {
47          this.value = null;
48      }
49  
50      CriteriaKualiDecimalValue(KualiDecimal value) {
51      	validateValue(value);
52          this.value = safeInstance(value);
53          this.value.setScale(KualiDecimal.SCALE);
54      }
55  
56      CriteriaKualiDecimalValue(double value) {
57          validateValue(value);
58          this.value = new KualiDecimal(value).bigDecimalValue();
59      }
60  
61      CriteriaKualiDecimalValue(int value) {
62          validateValue(value);
63          this.value = new KualiDecimal(value).bigDecimalValue();
64      }
65  
66      CriteriaKualiDecimalValue(String value) {
67          validateValue(value);
68          this.value = new KualiDecimal(value).bigDecimalValue();
69      }
70  
71  
72      CriteriaKualiDecimalValue(BigDecimal value) {
73          validateValue(value);
74          this.value = new KualiDecimal(value).bigDecimalValue();
75      }
76  
77      private static void validateValue(Object value) {
78      	if (value == null) {
79      		throw new IllegalArgumentException("Value cannot be null.");
80      	}
81      }
82  
83      /**
84       * Since KualiDecimal is not technically immutable we defensively copy when needed.
85       *
86       * see Effective Java 2nd ed. page 79 for details.
87       *
88       * @param val the KualiDecimal to check
89       * @return the safe BigDecimal
90       */
91      private static BigDecimal safeInstance(KualiDecimal val) {
92          return new BigDecimal(val.bigDecimalValue().toPlainString());
93      }
94      
95      @Override
96      public KualiDecimal getValue() {
97          return new KualiDecimal(this.value);
98      }
99      
100     @Override
101     public int hashCode() {
102         return HashCodeBuilder.reflectionHashCode(this);
103     }
104 
105     @Override
106     public boolean equals(Object obj) {
107         return EqualsBuilder.reflectionEquals(obj, this);
108     }
109 
110     @Override
111     public String toString() {
112         return ToStringBuilder.reflectionToString(this);
113     }
114     
115     /**
116      * Defines some internal constants used on this class.
117      */
118     static class Constants {
119         final static String ROOT_ELEMENT_NAME = "kualiDecimalValue";
120         final static String TYPE_NAME = "CriteriaKualiDecimalValueType";
121     }
122     
123 }