View Javadoc
1   /*
2    * Copyright (C) 2022-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.interfaces.IValueObject;
11  
12  
13  /**
14   * Firstname.
15   *
16   * DSGVO relevant.
17   *
18   * @see <a href="https://de.wikipedia.org/wiki/Vorname_(Deutschland)">Vorname</a>
19   */
20  public final class Firstname implements Comparable<Firstname>, IValueObject
21   {
22    /* *
23     * Cache for singletons.
24     */
25    // private static final Map<String, Firstname> CACHE = new WeakHashMap<>();
26  
27    /**
28     * Firstname regexp.
29     */
30    private static final Pattern FIRSTNAME_REGEXP = Pattern.compile("^[\\p{L}][\\p{L}-]*$"); //$NON-NLS-1$
31  
32    /**
33     * Firstname.
34     */
35    private final String firstname;
36  
37  
38    /**
39     * Constructor.
40     *
41     * @param firstname Firstname (maximum 32 characters)
42     *
43     * @throws NullPointerException if firstname is null
44     * @throws IllegalArgumentException if firstname contains unsupported characters or is to long or short
45     */
46    private Firstname(final String firstname)
47     {
48      super();
49      Objects.requireNonNull(firstname, "firstname"); //$NON-NLS-1$
50      if ((firstname.length() < 1) || (firstname.length() > 32))
51       {
52        throw new IllegalArgumentException("Firstname with wrong length"); //$NON-NLS-1$
53       }
54      if (!Firstname.FIRSTNAME_REGEXP.matcher(firstname).matches())
55       {
56        throw new IllegalArgumentException("Firstname with wrong format"); //$NON-NLS-1$
57       }
58      this.firstname = firstname;
59     }
60  
61  
62    /**
63     * Firstname factory.
64     *
65     * @param firstname Firstname
66     * @return Firstname object
67     */
68    public static Firstname of(final String firstname)
69     {
70      /*
71      synchronized (Firstname.class)
72       {
73        Firstname obj = Firstname.CACHE.get(firstname);
74        if (obj != null)
75         {
76          return obj;
77         }
78        obj = new Firstname(firstname);
79        Firstname.CACHE.put(firstname, obj);
80        return obj;
81       }
82      */
83      return new Firstname(firstname);
84     }
85  
86  
87    /**
88     * Returns the value of this Firstname as a string.
89     *
90     * @return The text value represented by this object after conversion to type string.
91     */
92    @Override
93    public String stringValue()
94     {
95      return this.firstname;
96     }
97  
98  
99    /**
100    * Calculate hash code.
101    *
102    * @return Hash
103    * @see java.lang.Object#hashCode()
104    */
105   @Override
106   public int hashCode()
107    {
108     return this.firstname.hashCode();
109    }
110 
111 
112   /**
113    * Is equal with another object.
114    *
115    * @param obj Object
116    * @return true when equal, false otherwise
117    * @see java.lang.Object#equals(java.lang.Object)
118    */
119   @Override
120   public boolean equals(final Object obj)
121    {
122     if (this == obj)
123      {
124       return true;
125      }
126     if (!(obj instanceof Firstname))
127      {
128       return false;
129      }
130     final Firstname other = (Firstname)obj;
131     return this.firstname.equals(other.firstname);
132    }
133 
134 
135   /**
136    * Returns the string representation of this Firstname.
137    *
138    * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
139    *
140    * "Firstname[firstname=Kai]"
141    *
142    * @return String representation of this Firstname
143    * @see java.lang.Object#toString()
144    */
145   @Override
146   public String toString()
147    {
148     final var builder = new StringBuilder(21);
149     builder.append("Firstname[firstname=").append(this.firstname).append(']'); //$NON-NLS-1$
150     return builder.toString();
151    }
152 
153 
154   /**
155    * Compare with another object.
156    *
157    * @param obj Object to compare with
158    * @return 0: equal; 1: greater; -1: smaller
159    * @see java.lang.Comparable#compareTo(java.lang.Object)
160    */
161   @Override
162   public int compareTo(final Firstname obj)
163    {
164     Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
165     return this.firstname.compareTo(obj.firstname);
166    }
167 
168  }