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