View Javadoc
1   /*
2    * Oceanus: Java Utilities
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.oceanus.decimal;
18  
19  /**
20   * Represents a Rate object.
21   */
22  public class OceanusRate
23          extends OceanusDecimal {
24      /**
25       * Standard number of decimals for Rate.
26       */
27      protected static final int NUM_DECIMALS = 4;
28  
29      /**
30       * One hundred percent.
31       */
32      public static final OceanusRate RATE_ONEHUNDREDPERCENT = getWholePercentage(100);
33  
34      /**
35       * Construct a new Rate.
36       */
37      protected OceanusRate() {
38      }
39  
40      /**
41       * Construct a new Rate by copying another rate.
42       *
43       * @param pRate the Rate to copy
44       */
45      public OceanusRate(final OceanusRate pRate) {
46          super(pRate.unscaledValue(), pRate.scale());
47      }
48  
49      /**
50       * Construct a new Ratio by the ratio between two decimals.
51       *
52       * @param pFirst  the first decimal
53       * @param pSecond the second decimal
54       */
55      public OceanusRate(final OceanusDecimal pFirst,
56                         final OceanusDecimal pSecond) {
57          recordScale(NUM_DECIMALS);
58          calculateQuotient(pFirst, pSecond);
59      }
60  
61      /**
62       * Construct a new Rate from a decimal.
63       *
64       * @param pRatio the Ratio
65       */
66      public OceanusRate(final OceanusDecimal pRatio) {
67          super(pRatio.unscaledValue(), pRatio.scale());
68          adjustToScale(NUM_DECIMALS);
69          super.subtractValue(RATE_ONEHUNDREDPERCENT);
70      }
71  
72      /**
73       * Create the rate from a byte array.
74       *
75       * @param pBuffer the buffer
76       */
77      public OceanusRate(final byte[] pBuffer) {
78          super(pBuffer);
79      }
80  
81      /**
82       * Construct a new Rate by setting the value explicitly.
83       *
84       * @param pValue the unscaled value of a whole percentage (e.g. 2 = 2%)
85       * @return the new Rate
86       */
87      public static OceanusRate getWholePercentage(final long pValue) {
88          final OceanusRate myRate = new OceanusRate();
89          myRate.setValue(adjustDecimals(pValue, NUM_DECIMALS
90                  - OceanusDecimalConstants.ADJUST_PERCENT), NUM_DECIMALS);
91          return myRate;
92      }
93  
94      /**
95       * Construct a new Rate by setting the value explicitly.
96       *
97       * @param pValue the unscaled value of a whole percentage (e.g. 25 = 2.5%)
98       * @return the new Rate
99       */
100     public static OceanusRate getWholePermille(final long pValue) {
101         final OceanusRate myRate = new OceanusRate();
102         myRate.setValue(adjustDecimals(pValue, NUM_DECIMALS
103                 - OceanusDecimalConstants.ADJUST_PERMILLE), NUM_DECIMALS);
104         return myRate;
105     }
106 
107     /**
108      * Construct a new Rate by setting the value explicitly.
109      *
110      * @param pValue the unscaled value of a whole percentage (e.g. 25 = 0.25%)
111      * @return the new Rate
112      */
113     public static OceanusRate getTenthPermille(final long pValue) {
114         final OceanusRate myRate = new OceanusRate();
115         myRate.setValue(adjustDecimals(pValue, NUM_DECIMALS
116                 - OceanusDecimalConstants.ADJUST_PERMILLE - 1), NUM_DECIMALS);
117         return myRate;
118     }
119 
120     /**
121      * Obtain remaining rate of this rate (i.e. 100% - this rate).
122      *
123      * @return the remaining rate
124      */
125     public OceanusRate getRemainingRate() {
126         /* Create a copy of this rate and reverse it */
127         final OceanusRate myRate = new OceanusRate(this);
128         myRate.reverseRate();
129         return myRate;
130     }
131 
132     /**
133      * Obtain remaining rate of this rate (i.e. 100% - this rate).
134      */
135     private void reverseRate() {
136         /* Negate the value and add 100% */
137         negate();
138         super.addValue(RATE_ONEHUNDREDPERCENT);
139     }
140 
141     /**
142      * Obtain inverse ratio of this rate (i.e. 100%/this rate).
143      *
144      * @return the inverse ratio
145      */
146     public OceanusRatio getInverseRatio() {
147         return new OceanusRatio(RATE_ONEHUNDREDPERCENT, this);
148     }
149 
150     @Override
151     public void addValue(final OceanusDecimal pValue) {
152         throw new UnsupportedOperationException();
153     }
154 
155     @Override
156     public void subtractValue(final OceanusDecimal pValue) {
157         throw new UnsupportedOperationException();
158     }
159 }