Money.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * blueHour: open source accounting
  5.  * http://tidalwave.it/projects/bluehour
  6.  *
  7.  * Copyright (C) 2013 - 2025 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. // import javax.annotation.Nonnegative;
  28. import jakarta.annotation.Nonnull;
  29. // import javax.annotation.concurrent.Immutable;
  30. import java.text.DecimalFormat;
  31. import java.text.DecimalFormatSymbols;
  32. import java.text.ParseException;
  33. import java.math.BigDecimal;
  34. import lombok.AccessLevel;
  35. import lombok.EqualsAndHashCode;
  36. import lombok.Getter;
  37. import lombok.RequiredArgsConstructor;

  38. /***************************************************************************************************************************************************************
  39.  *
  40.  * This class models an amount of money.
  41.  *
  42.  * @author  Fabrizio Giudici
  43.  *
  44.  **************************************************************************************************************************************************************/
  45. /* @Immutable */ @RequiredArgsConstructor(access = AccessLevel.PRIVATE) @Getter @EqualsAndHashCode
  46. public class Money implements Comparable<Money>
  47.   {
  48.     public static final Money ZERO = of(BigDecimal.ZERO, "EUR");

  49.     @Nonnull
  50.     private final BigDecimal amount;

  51.     @Nonnull
  52.     private final String currency;

  53.     private Money (final long amount, @Nonnull final String currency)
  54.       {
  55.         this(BigDecimal.valueOf(amount), currency);
  56.       }

  57.     @Nonnull
  58.     public static Money of (final BigDecimal amount, @Nonnull final String currency)
  59.       {
  60.         return new Money(amount, currency);
  61.       }

  62.     @Nonnull
  63.     public static Money of (final long amount, @Nonnull final String currency)
  64.       {
  65.         return new Money(amount, currency);
  66.       }

  67.     @Nonnull
  68.     public static Money parse (@Nonnull final String string)
  69.       throws ParseException
  70.       {
  71.         final var parts = string.split(" ");
  72.         return of((BigDecimal)getFormat().parse(parts[0]), parts[1]);
  73.       }

  74.     @Nonnull
  75.     public Money add (@Nonnull final Money other)
  76.       {
  77.         checkCurrencies(other);
  78.         return of(amount.add(other.amount), currency);
  79.       }

  80.     @Nonnull
  81.     public Money subtract (@Nonnull final Money other)
  82.       {
  83.         checkCurrencies(other);
  84.         return of(amount.subtract(other.amount), currency);
  85.       }

  86.     /* @Nonnegative */
  87.     public double divided (@Nonnull final Money other)
  88.       {
  89.         checkCurrencies(other);
  90.         // Can fail with ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
  91. //        return amount.divide(other.amount).doubleValue();
  92.         return amount.doubleValue() / other.amount.doubleValue();
  93.       }

  94.     @Nonnull
  95.     public static DecimalFormat getFormat()
  96.       {
  97.         final var symbols = new DecimalFormatSymbols();
  98.         symbols.setDecimalSeparator('.');
  99.         final var pattern = "###0.00";
  100.         final var decimalFormat = new DecimalFormat(pattern, symbols);
  101.         decimalFormat.setParseBigDecimal(true);

  102.         return decimalFormat;
  103.       }

  104.     @Override
  105.     public int compareTo (@Nonnull final Money other)
  106.       {
  107.         checkCurrencies(other);
  108.         return this.amount.compareTo(other.amount);
  109.       }

  110.     public boolean isEqualTo (@Nonnull final Money other)
  111.       {
  112.         return compareTo(other) == 0;
  113.       }

  114.     public boolean greaterThan (@Nonnull final Money other)
  115.       {
  116.         return compareTo(other) > 0;
  117.       }

  118.     public boolean lowerThan (@Nonnull final Money other)
  119.       {
  120.         return compareTo(other) < 0;
  121.       }

  122.     @Override @Nonnull
  123.     public String toString()
  124.       {
  125.         return String.format("%s %s", getFormat().format(amount), currency);
  126.       }

  127.     private void checkCurrencies (@Nonnull final Money other)
  128.       {
  129.         if (!this.currency.equals(other.currency))
  130.           {
  131.             throw new IllegalArgumentException(String.format("Currency mismatch: %s vs %s", this.currency, other.currency));
  132.           }
  133.       }
  134.   }