View Javadoc
1   /*
2    * Copyright (C) 2020-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.validation.values;
5   
6   
7   import java.util.Objects;
8   import java.util.regex.Pattern;
9   
10  import de.powerstat.validation.generated.GeneratedISO4217;
11  import de.powerstat.validation.interfaces.IValueObject;
12  
13  
14  /**
15   * Currency ISO 4217 codes.
16   *
17   * Not DSGVO relevant.
18   *
19   * TODO Translations
20   */
21  public final class Currency implements Comparable<Currency>, IValueObject
22   {
23    /* *
24     * Cache for singletons.
25     */
26    // private static final Map<String, Currency> CACHE = new WeakHashMap<>();
27  
28    /**
29     * Currency regexp.
30     */
31    @SuppressWarnings("java:S5867")
32    private static final Pattern CURRENCY_REGEXP = Pattern.compile("^[A-Z]{3}$"); //$NON-NLS-1$
33  
34    /**
35     * ISO 4217 currency code.
36     */
37    private final String code;
38  
39  
40    /**
41     * Constructor.
42     *
43     * @param code ISO 4217 code
44     * @throws NullPointerException if code is null
45     * @throws IllegalArgumentException if code is not a known 4217 code
46     */
47    private Currency(final String code)
48     {
49      super();
50      Objects.requireNonNull(code, "code"); //$NON-NLS-1$
51      if (code.length() != 3)
52       {
53        throw new IllegalArgumentException("Length is not 3"); //$NON-NLS-1$
54       }
55      if (!Currency.CURRENCY_REGEXP.matcher(code).matches())
56       {
57        throw new IllegalArgumentException("Code contains illegal character"); //$NON-NLS-1$
58       }
59      if (!GeneratedISO4217.contains(code))
60       {
61        throw new IllegalArgumentException("Unknown ISO4217 code: " + code); //$NON-NLS-1$
62       }
63      this.code = code;
64     }
65  
66  
67    /**
68     * Currency factory.
69     *
70     * @param code 4217 code
71     * @return Currency object
72     */
73    public static Currency of(final String code)
74     {
75      /*
76      synchronized (Currency.class)
77       {
78        Currency obj = Currency.CACHE.get(code);
79        if (obj != null)
80         {
81          return obj;
82         }
83        obj = new Currency(code);
84        Currency.CACHE.put(code, obj);
85        return obj;
86       }
87      */
88      return new Currency(code);
89     }
90  
91  
92    /**
93     * Returns the value of this Currency as a ISO 4217 string.
94     *
95     * @return The text value represented by this object after conversion to type string.
96     */
97    @Override
98    public String stringValue()
99     {
100     return this.code;
101    }
102 
103 
104   /**
105    * Calculate hash code.
106    *
107    * @return Hash
108    * @see java.lang.Object#hashCode()
109    */
110   @Override
111   public int hashCode()
112    {
113     return this.code.hashCode();
114    }
115 
116 
117   /**
118    * Is equal with another object.
119    *
120    * @param obj Object
121    * @return true when equal, false otherwise
122    * @see java.lang.Object#equals(java.lang.Object)
123    */
124   @Override
125   public boolean equals(final Object obj)
126    {
127     if (this == obj)
128      {
129       return true;
130      }
131     if (!(obj instanceof Currency))
132      {
133       return false;
134      }
135     final Currency other = (Currency)obj;
136     return this.code.equals(other.code);
137    }
138 
139 
140   /**
141    * Returns the string representation of this Currency.
142    *
143    * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
144    *
145    * "Currency[code=EUR]"
146    *
147    * @return String representation of this Currency
148    * @see java.lang.Object#toString()
149    */
150   @Override
151   public String toString()
152    {
153     final var builder = new StringBuilder();
154     builder.append("Currency[code=").append(this.code).append(']'); //$NON-NLS-1$
155     return builder.toString();
156    }
157 
158 
159   /**
160    * Compare with another object.
161    *
162    * @param obj Object to compare with
163    * @return 0: equal; 1: greater; -1: smaller
164    * @see java.lang.Comparable#compareTo(java.lang.Object)
165    */
166   @Override
167   public int compareTo(final Currency obj)
168    {
169     Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
170     return this.code.compareTo(obj.code);
171    }
172 
173  }