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 ratio.
63 *
64 * @param pRatio the Ratio
65 */
66 public OceanusRate(final OceanusRatio pRatio) {
67 super(pRatio.unscaledValue(), pRatio.scale());
68 adjustToScale(NUM_DECIMALS);
69 super.subtractValue(RATE_ONEHUNDREDPERCENT);
70 }
71
72 /**
73 * Constructor for rate from a decimal string.
74 *
75 * @param pSource The source decimal string
76 * @throws IllegalArgumentException on invalidly formatted argument
77 */
78 public OceanusRate(final String pSource) {
79 /* Parse the string and correct the scale */
80 OceanusDecimalParser.parseDecimalValue(pSource, this);
81 adjustToScale(NUM_DECIMALS);
82 }
83
84 /**
85 * Create the rate from a byte array.
86 *
87 * @param pBuffer the buffer
88 */
89 public OceanusRate(final byte[] pBuffer) {
90 super(pBuffer);
91 }
92
93 /**
94 * Construct a new Rate by setting the value explicitly.
95 *
96 * @param pValue the unscaled value of a whole percentage (e.g. 2 = 2%)
97 * @return the new Rate
98 */
99 public static OceanusRate getWholePercentage(final long pValue) {
100 final OceanusRate myRate = new OceanusRate();
101 myRate.setValue(adjustDecimals(pValue, NUM_DECIMALS
102 - OceanusDecimalParser.ADJUST_PERCENT), NUM_DECIMALS);
103 return myRate;
104 }
105
106 /**
107 * Construct a new Rate by setting the value explicitly.
108 *
109 * @param pValue the unscaled value of a whole percentage (e.g. 25 = 2.5%)
110 * @return the new Rate
111 */
112 public static OceanusRate getWholePermille(final long pValue) {
113 final OceanusRate myRate = new OceanusRate();
114 myRate.setValue(adjustDecimals(pValue, NUM_DECIMALS
115 - OceanusDecimalParser.ADJUST_PERMILLE), NUM_DECIMALS);
116 return myRate;
117 }
118
119 /**
120 * Construct a new Rate by setting the value explicitly.
121 *
122 * @param pValue the unscaled value of a whole percentage (e.g. 25 = 0.25%)
123 * @return the new Rate
124 */
125 public static OceanusRate getTenthPermille(final long pValue) {
126 final OceanusRate myRate = new OceanusRate();
127 myRate.setValue(adjustDecimals(pValue, NUM_DECIMALS
128 - OceanusDecimalParser.ADJUST_PERMILLE - 1), NUM_DECIMALS);
129 return myRate;
130 }
131
132 /**
133 * Obtain remaining rate of this rate (i.e. 100% - this rate).
134 *
135 * @return the remaining rate
136 */
137 public OceanusRate getRemainingRate() {
138 /* Create a copy of this rate and reverse it */
139 final OceanusRate myRate = new OceanusRate(this);
140 myRate.reverseRate();
141 return myRate;
142 }
143
144 /**
145 * Obtain remaining rate of this rate (i.e. 100% - this rate).
146 */
147 private void reverseRate() {
148 /* Negate the value and add 100% */
149 negate();
150 super.addValue(RATE_ONEHUNDREDPERCENT);
151 }
152
153 /**
154 * Obtain inverse ratio of this rate (i.e. 100%/this rate).
155 *
156 * @return the inverse ratio
157 */
158 public OceanusRatio getInverseRatio() {
159 return new OceanusRatio(RATE_ONEHUNDREDPERCENT, this);
160 }
161
162 @Override
163 public void addValue(final OceanusDecimal pValue) {
164 throw new UnsupportedOperationException();
165 }
166
167 @Override
168 public void subtractValue(final OceanusDecimal pValue) {
169 throw new UnsupportedOperationException();
170 }
171 }