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.osedu.org/licenses/ECL-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  package org.kuali.coeus.sys.api.model;
17  
18  import java.math.BigDecimal;
19  
20  /**
21   * This class is a wrapper around {@link java.math.BigDecimal}. It exposes the only the
22   * needed functionality of {@link java.math.BigDecimal}, uses a standard
23   * {@link java.math.RoundingMode} of {@link java.math.RoundingMode#HALF_UP}
24   * and uses a standard SCALE of 2.
25   *
26   * This class is, like {@link java.math.BigDecimal}, immutable; even methods which
27   * might be expected to change the value actually just return a new instance
28   * with the new value.
29   */
30  public final class ScaleTwoDecimal extends AbstractDecimal<ScaleTwoDecimal> {
31  
32      private static final long serialVersionUID = 1602860735060812811L;
33  
34      public static final int SCALE = 2;
35  
36      public static final ScaleTwoDecimal ZERO = new ScaleTwoDecimal(0);
37      public static final ScaleTwoDecimal ONE_HUNDRED = new ScaleTwoDecimal(100);
38      /**
39       * This constructor should never be called except during JAXB unmarshalling.
40       */
41      private ScaleTwoDecimal() {
42          super();
43      }
44  
45      public ScaleTwoDecimal(String value) {
46          super(value, SCALE);
47      }
48  
49      public ScaleTwoDecimal(int value) {
50          super(value, SCALE);
51      }
52  
53      public ScaleTwoDecimal(double value) {
54          super(value, SCALE);
55      }
56  
57      public ScaleTwoDecimal(BigDecimal value) {
58          super(value, SCALE);
59      }
60  
61      private ScaleTwoDecimal(String value, int scale) {
62          super(value, scale);
63      }
64  
65      private ScaleTwoDecimal(int value, int scale) {
66          super(value, scale);
67      }
68  
69      private ScaleTwoDecimal(double value, int scale) {
70          super(value, scale);
71      }
72  
73      private ScaleTwoDecimal(BigDecimal value, int scale) {
74          super(value, scale);
75      }
76  
77      @Override
78      protected ScaleTwoDecimal newInstance(int value) {
79          return new ScaleTwoDecimal(value);
80      }
81  
82      @Override
83      protected ScaleTwoDecimal newInstance(BigDecimal value, int scale) {
84          return new ScaleTwoDecimal(value, scale);
85      }
86  
87      @Override
88      protected ScaleTwoDecimal zero() {
89          return ZERO;
90      }
91  
92      @Override
93      protected ScaleTwoDecimal oneHundred() {return ONE_HUNDRED; }
94  
95      /**
96       * return {@link #ZERO} if the object is null
97       * @param value the passed in value or {@link #ZERO}
98       */
99      public static ScaleTwoDecimal returnZeroIfNull(ScaleTwoDecimal value){
100         return value==null ? ScaleTwoDecimal.ZERO : value;
101     }
102 }