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.GeneratedISO3166A2;
11  import de.powerstat.validation.interfaces.IValueObject;
12  
13  
14  /**
15   * Country - ISO 3166-1 codes.
16   *
17   * Not DSGVO relevant.
18   *
19   * TODO Translations
20   */
21  public final class Country implements Comparable<Country>, IValueObject
22   {
23    /* *
24     * Cache for singletons.
25     */
26    // private static final Map<String, Country> CACHE = new WeakHashMap<>();
27  
28    /**
29     * Country regexp.
30     */
31    @SuppressWarnings("java:S5867")
32    private static final Pattern COUNTRY_REGEXP = Pattern.compile("^[A-Z]{2}$"); //$NON-NLS-1$
33  
34    /**
35     * Alpha-2 country code.
36     */
37    private final String alpha2;
38  
39  
40    /**
41     * Constructor.
42     *
43     * @param alpha2 Alpha-2 code
44     * @throws NullPointerException if code is null
45     * @throws IllegalArgumentException if code is not a known alpha-2 code
46     */
47    private Country(final String alpha2)
48     {
49      super();
50      Objects.requireNonNull(alpha2, "alpha2"); //$NON-NLS-1$
51      if (alpha2.length() != 2)
52       {
53        throw new IllegalArgumentException("Length is not 2"); //$NON-NLS-1$
54       }
55      if (!Country.COUNTRY_REGEXP.matcher(alpha2).matches())
56       {
57        throw new IllegalArgumentException("alpha2 contains illegal character"); //$NON-NLS-1$
58       }
59      if (!GeneratedISO3166A2.contains(alpha2))
60       {
61        throw new IllegalArgumentException("Unknown ISO3166 Alpha-2 code: " + alpha2); //$NON-NLS-1$
62       }
63      this.alpha2 = alpha2;
64     }
65  
66  
67    /**
68     * Country factory.
69     *
70     * @param alpha2 Alpha-2 code
71     * @return Country object
72     */
73    public static Country of(final String alpha2)
74     {
75      /*
76      synchronized (Country.class)
77       {
78        Country obj = Country.CACHE.get(alpha2);
79        if (obj != null)
80         {
81          return obj;
82         }
83        obj = new Country(alpha2);
84        Country.CACHE.put(alpha2, obj);
85        return obj;
86       }
87      */
88      return new Country(alpha2);
89     }
90  
91  
92    /**
93     * Returns the value of this Country as a ISO 3166-1 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.alpha2;
101    }
102 
103 
104   /**
105    * Get country name in english language.
106    *
107    * @return Country name in english language
108    */
109   public String getEnglishCountryName()
110    {
111     return GeneratedISO3166A2.getName(this.alpha2);
112    }
113 
114 
115   /**
116    * Calculate hash code.
117    *
118    * @return Hash
119    * @see java.lang.Object#hashCode()
120    */
121   @Override
122   public int hashCode()
123    {
124     return this.alpha2.hashCode();
125    }
126 
127 
128   /**
129    * Is equal with another object.
130    *
131    * @param obj Object
132    * @return true when equal, false otherwise
133    * @see java.lang.Object#equals(java.lang.Object)
134    */
135   @Override
136   public boolean equals(final Object obj)
137    {
138     if (this == obj)
139      {
140       return true;
141      }
142     if (!(obj instanceof Country))
143      {
144       return false;
145      }
146     final Country other = (Country)obj;
147     return this.alpha2.equals(other.alpha2);
148    }
149 
150 
151   /**
152    * Returns the string representation of this Country.
153    *
154    * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
155    *
156    * "Country[alpha=DE]"
157    *
158    * @return String representation of this Country
159    * @see java.lang.Object#toString()
160    */
161   @Override
162   public String toString()
163    {
164     final var builder = new StringBuilder();
165     builder.append("Country[alpha2=").append(this.alpha2).append(']'); //$NON-NLS-1$
166     return builder.toString();
167    }
168 
169 
170   /**
171    * Compare with another object.
172    *
173    * @param obj Object to compare with
174    * @return 0: equal; 1: greater; -1: smaller
175    * @see java.lang.Comparable#compareTo(java.lang.Object)
176    */
177   @Override
178   public int compareTo(final Country obj)
179    {
180     Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
181     return this.alpha2.compareTo(obj.alpha2);
182    }
183 
184  }