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  /**
19   * Created by sona on 3/4/14.
20   */
21  /*
22   * Copyright 2006-2014 The Kuali Foundation
23   *
24   * Licensed under the Educational Community License, Version 2.0 (the "License");
25   * you may not use this file except in compliance with the License.
26   * You may obtain a copy of the License at
27   *
28   * http://www.opensource.org/licenses/ecl2.php
29   *
30   * Unless required by applicable law or agreed to in writing, software
31   * distributed under the License is distributed on an "AS IS" BASIS,
32   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33   * See the License for the specific language governing permissions and
34   * limitations under the License.
35   */
36  
37  import org.apache.commons.lang.builder.EqualsBuilder;
38  import org.apache.commons.lang.builder.HashCodeBuilder;
39  import org.apache.commons.lang.builder.ToStringBuilder;
40  import org.kuali.rice.core.api.util.type.KualiDecimal;
41  import org.kuali.rice.core.api.util.type.KualiPercent;
42  
43  import javax.xml.bind.annotation.XmlAccessType;
44  import javax.xml.bind.annotation.XmlAccessorType;
45  import javax.xml.bind.annotation.XmlRootElement;
46  import javax.xml.bind.annotation.XmlType;
47  import javax.xml.bind.annotation.XmlValue;
48  import java.math.BigDecimal;
49  
50  
51  /**
52   * A CriteriaValue which stores {@link org.kuali.rice.core.api.util.type.KualiPercent} information in the form of a
53   * {@link java.math.BigDecimal} value.
54   *
55   * @author Kuali Rice Team (rice.collab@kuali.org)
56   *
57   */
58  @XmlRootElement(name = CriteriaKualiPercentValue.Constants.ROOT_ELEMENT_NAME)
59  @XmlAccessorType(XmlAccessType.NONE)
60  @XmlType(name = CriteriaKualiPercentValue.Constants.TYPE_NAME)
61  public final class CriteriaKualiPercentValue implements CriteriaValue<KualiPercent> {
62  
63      @XmlValue
64      private final BigDecimal value;
65  
66      CriteriaKualiPercentValue() {
67          this.value = null;
68      }
69  
70      CriteriaKualiPercentValue(KualiPercent value) {
71          validateValue(value);
72          this.value = safeInstance(value.bigDecimalValue());
73      }
74  
75      CriteriaKualiPercentValue(double value) {
76          validateValue(value);
77          this.value = new KualiPercent(value).bigDecimalValue();
78      }
79  
80      CriteriaKualiPercentValue(int value) {
81          validateValue(value);
82          this.value = new KualiPercent(value).bigDecimalValue();
83      }
84  
85  
86      CriteriaKualiPercentValue(String value) {
87          validateValue(value);
88          this.value = new KualiPercent(value).bigDecimalValue();
89      }
90  
91  
92      CriteriaKualiPercentValue(BigDecimal value) {
93          validateValue(value);
94          this.value = new KualiPercent(value).bigDecimalValue();
95      }
96  
97  
98      private static void validateValue(Object value) {
99          if (value == null) {
100             throw new IllegalArgumentException("Value cannot be null.");
101         }
102     }
103 
104     /**
105      * Since KualiPercent is not technically immutable we defensively copy when needed.
106      *
107      * see Effective Java 2nd ed. page 79 for details.
108      *
109      * @param val the KualiPercent to check
110      * @return the safe BigDecimal
111      */
112     private static BigDecimal safeInstance(BigDecimal val) {
113         return new BigDecimal(val.toPlainString());
114     }
115 
116     @Override
117     public KualiPercent getValue() {
118         return new KualiPercent(this.value);
119     }
120 
121     @Override
122     public int hashCode() {
123         return HashCodeBuilder.reflectionHashCode(this);
124     }
125 
126     @Override
127     public boolean equals(Object obj) {
128         return EqualsBuilder.reflectionEquals(obj, this);
129     }
130 
131     @Override
132     public String toString() {
133         return ToStringBuilder.reflectionToString(this);
134     }
135 
136     /**
137      * Defines some internal constants used on this class.
138      */
139     static class Constants {
140         final static String ROOT_ELEMENT_NAME = "kualiPercentValue";
141         final static String TYPE_NAME = "CriteriaKualiPercentValueType";
142     }
143 
144 }
145