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 Units object.
21   */
22  public class OceanusUnits
23          extends OceanusDecimal {
24      /**
25       * Standard number of decimals for Units.
26       */
27      protected static final int NUM_DECIMALS = 4;
28  
29      /**
30       * Construct a new Units.
31       */
32      public OceanusUnits() {
33          recordScale(NUM_DECIMALS);
34      }
35  
36      /**
37       * Construct a new Units by copying another units.
38       *
39       * @param pUnits the Units to copy
40       */
41      public OceanusUnits(final OceanusUnits pUnits) {
42          super(pUnits.unscaledValue(), pUnits.scale());
43      }
44  
45      /**
46       * Constructor for units from a decimal string.
47       *
48       * @param pSource The source decimal string
49       * @throws IllegalArgumentException on invalidly formatted argument
50       */
51      public OceanusUnits(final String pSource) {
52          /* Use default constructor */
53          this();
54  
55          /* Parse the string and correct the scale */
56          OceanusDecimalParser.parseDecimalValue(pSource, this);
57          adjustToScale(NUM_DECIMALS);
58      }
59  
60      /**
61       * Create the units from a byte array.
62       *
63       * @param pBuffer the buffer
64       */
65      public OceanusUnits(final byte[] pBuffer) {
66          super(pBuffer);
67      }
68  
69      /**
70       * Construct a new Units by setting the value explicitly.
71       *
72       * @param pValue the unscaled value
73       * @return the new Rate
74       */
75      public static OceanusUnits getWholeUnits(final long pValue) {
76          final OceanusUnits myUnits = new OceanusUnits();
77          myUnits.setValue(adjustDecimals(pValue, NUM_DECIMALS), NUM_DECIMALS);
78          return myUnits;
79      }
80  
81      /**
82       * Add units to the value.
83       *
84       * @param pValue The units to add to this one.
85       */
86      public void addUnits(final OceanusUnits pValue) {
87          /* Add the value */
88          super.addValue(pValue);
89      }
90  
91      /**
92       * Subtract units from the value.
93       *
94       * @param pValue The units to subtract from this one.
95       */
96      public void subtractUnits(final OceanusUnits pValue) {
97          /* Subtract the value */
98          super.subtractValue(pValue);
99      }
100 
101     @Override
102     public void addValue(final OceanusDecimal pValue) {
103         throw new UnsupportedOperationException();
104     }
105 
106     @Override
107     public void subtractValue(final OceanusDecimal pValue) {
108         throw new UnsupportedOperationException();
109     }
110 
111     /**
112      * calculate the value of these units at a given price.
113      *
114      * @param pPrice the per unit price
115      * @return the calculated value
116      */
117     public OceanusMoney valueAtPrice(final OceanusPrice pPrice) {
118         /* Calculate value of units */
119         return new OceanusMoney(this, pPrice);
120     }
121 }