Skip to contentPackage: Money
Money
Coverage
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * blueHour: open source accounting
5: * http://tidalwave.it/projects/bluehour
6: *
7: * Copyright (C) 2013 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/bluehour-src
22: * git clone https://github.com/tidalwave-it/bluehour-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.accounting.model.types;
27:
28: import javax.annotation.Nonnegative;
29: import javax.annotation.Nonnull;
30: import javax.annotation.concurrent.Immutable;
31: import java.text.DecimalFormat;
32: import java.text.DecimalFormatSymbols;
33: import java.text.ParseException;
34: import java.math.BigDecimal;
35: import lombok.AccessLevel;
36: import lombok.EqualsAndHashCode;
37: import lombok.Getter;
38: import lombok.RequiredArgsConstructor;
39:
40: /***************************************************************************************************************************************************************
41: *
42: * This class models an amount of money.
43: *
44: * @author Fabrizio Giudici
45: *
46: **************************************************************************************************************************************************************/
47: @Immutable @RequiredArgsConstructor(access = AccessLevel.PRIVATE) @Getter @EqualsAndHashCode
48: public class Money implements Comparable<Money>
49: {
50: public static final Money ZERO = Money.of(BigDecimal.ZERO, "EUR");
51:
52: @Nonnull
53: private final BigDecimal amount;
54:
55: @Nonnull
56: private final String currency;
57:
58: private Money (final long amount, @Nonnull final String currency)
59: {
60: this(BigDecimal.valueOf(amount), currency);
61: }
62:
63: @Nonnull
64: public static Money of (final BigDecimal amount, @Nonnull final String currency)
65: {
66: return new Money(amount, currency);
67: }
68:
69: @Nonnull
70: public static Money of (final long amount, @Nonnull final String currency)
71: {
72: return new Money(amount, currency);
73: }
74:
75: @Nonnull
76: public static Money parse (@Nonnull final String string)
77: throws ParseException
78: {
79: final var parts = string.split(" ");
80: return of((BigDecimal)getFormat().parse(parts[0]), parts[1]);
81: }
82:
83: @Nonnull
84: public Money add (@Nonnull final Money other)
85: {
86: checkCurrencies(other);
87: return of(amount.add(other.amount), currency);
88: }
89:
90: @Nonnull
91: public Money subtract (@Nonnull final Money other)
92: {
93: checkCurrencies(other);
94: return of(amount.subtract(other.amount), currency);
95: }
96:
97: @Nonnegative
98: public double divided (@Nonnull final Money other)
99: {
100: checkCurrencies(other);
101: // Can fail with ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
102: // return amount.divide(other.amount).doubleValue();
103: return amount.doubleValue() / other.amount.doubleValue();
104: }
105:
106: @Nonnull
107: public static DecimalFormat getFormat()
108: {
109: final var symbols = new DecimalFormatSymbols();
110: symbols.setDecimalSeparator('.');
111: final var pattern = "###0.00";
112: final var decimalFormat = new DecimalFormat(pattern, symbols);
113: decimalFormat.setParseBigDecimal(true);
114:
115: return decimalFormat;
116: }
117:
118: @Override
119: public int compareTo (@Nonnull final Money other)
120: {
121: checkCurrencies(other);
122: return this.amount.compareTo(other.amount);
123: }
124:
125: public boolean isEqualTo (@Nonnull final Money other)
126: {
127:• return compareTo(other) == 0;
128: }
129:
130: public boolean greaterThan (@Nonnull final Money other)
131: {
132:• return compareTo(other) > 0;
133: }
134:
135: public boolean lowerThan (@Nonnull final Money other)
136: {
137:• return compareTo(other) < 0;
138: }
139:
140: @Override @Nonnull
141: public String toString()
142: {
143: return String.format("%s %s", getFormat().format(amount), currency);
144: }
145:
146: private void checkCurrencies (@Nonnull final Money other)
147: {
148:• if (!this.currency.equals(other.currency))
149: {
150: throw new IllegalArgumentException(String.format("Currency mismatch: %s vs %s", this.currency, other.currency));
151: }
152: }
153: }