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  import java.util.Currency;
20  
21  /**
22   * Represents a Price object.
23   */
24  public class OceanusPrice
25          extends OceanusMoney {
26      /**
27       * Additional number of decimals for Price.
28       */
29      static final int XTRA_DECIMALS = 2;
30  
31      /**
32       * Constructor for price of value zero in the default currency.
33       */
34      public OceanusPrice() {
35          this(DEFAULT_CURRENCY);
36      }
37  
38      /**
39       * Constructor for money of value zero in specified currency.
40       *
41       * @param pCurrency the currency
42       */
43      public OceanusPrice(final Currency pCurrency) {
44          super(pCurrency);
45          recordScale(pCurrency.getDefaultFractionDigits()
46                  + XTRA_DECIMALS);
47      }
48  
49      /**
50       * Construct a new Price by copying another price.
51       *
52       * @param pPrice the Price to copy
53       */
54      public OceanusPrice(final OceanusPrice pPrice) {
55          super(pPrice.getCurrency());
56          setValue(pPrice.unscaledValue(), pPrice.scale());
57      }
58  
59      /**
60       * Create the price from a byte array.
61       *
62       * @param pBuffer the buffer
63       */
64      public OceanusPrice(final byte[] pBuffer) {
65          super(pBuffer);
66      }
67  
68      /**
69       * Factory method for generating whole monetary units for a currency (e.g. £)
70       *
71       * @param pUnits    the number of whole monetary units
72       * @param pCurrency the currency
73       * @return the allocated price
74       */
75      public static OceanusPrice getWholeUnits(final long pUnits,
76                                               final Currency pCurrency) {
77          /* Allocate the money */
78          final OceanusPrice myResult = new OceanusPrice(pCurrency);
79          final int myScale = myResult.scale();
80          myResult.setValue(adjustDecimals(pUnits, myScale), myScale);
81          return myResult;
82      }
83  
84      /**
85       * Factory method for generating whole monetary units (e.g. £)
86       *
87       * @param pUnits the number of whole monetary units
88       * @return the allocated price
89       */
90      public static OceanusPrice getWholeUnits(final long pUnits) {
91          /* Allocate the price */
92          final OceanusPrice myResult = new OceanusPrice();
93          final int myScale = myResult.scale();
94          myResult.setValue(adjustDecimals(pUnits, myScale), myScale);
95          return myResult;
96      }
97  
98      @Override
99      public OceanusPrice changeCurrency(final Currency pCurrency) {
100         /* Convert currency with an exchange rate of one */
101         return convertCurrency(pCurrency, OceanusRatio.ONE);
102     }
103 
104     @Override
105     public OceanusPrice convertCurrency(final Currency pCurrency,
106                                         final OceanusRatio pRate) {
107         /* If this is the same currency then no conversion */
108         if (getCurrency().equals(pCurrency)) {
109             return new OceanusPrice(this);
110         }
111 
112         /* Create the new Price */
113         final OceanusPrice myResult = new OceanusPrice(pCurrency);
114         myResult.calculateProduct(this, pRate);
115         return myResult;
116     }
117 
118     /**
119      * Subtract a monetary price from the value.
120      *
121      * @param pValue The money to subtract from this one.
122      */
123     public void subtractPrice(final OceanusPrice pValue) {
124         /* Currency must be identical */
125         if (!getCurrency().equals(pValue.getCurrency())) {
126             throw new IllegalArgumentException(ERROR_DIFFER);
127         }
128 
129         /* Subtract the value */
130         super.subtractValue(pValue);
131     }
132 
133     /**
134      * calculate the value of specified units at this price.
135      *
136      * @param pUnits the number of units
137      * @return the calculated value
138      */
139     public OceanusMoney unitsAtPrice(final OceanusUnits pUnits) {
140         /* Calculate value of units */
141         final OceanusPrice myPrice = new OceanusPrice(this);
142         myPrice.calculateProduct(myPrice, pUnits);
143         return new OceanusMoney(myPrice);
144     }
145 }