1
2
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
15
16
17
18
19
20 public final class Firstname implements Comparable<Firstname>, IValueObject
21 {
22
23
24
25
26
27
28
29
30 private static final Pattern FIRSTNAME_REGEXP = Pattern.compile("^[\\p{L}][\\p{L}-]*$");
31
32
33
34
35 private final String firstname;
36
37
38
39
40
41
42
43
44
45
46 private Firstname(final String firstname)
47 {
48 super();
49 Objects.requireNonNull(firstname, "firstname");
50 if ((firstname.length() < 1) || (firstname.length() > 32))
51 {
52 throw new IllegalArgumentException("Firstname with wrong length");
53 }
54 if (!Firstname.FIRSTNAME_REGEXP.matcher(firstname).matches())
55 {
56 throw new IllegalArgumentException("Firstname with wrong format");
57 }
58 this.firstname = firstname;
59 }
60
61
62
63
64
65
66
67
68 public static Firstname of(final String firstname)
69 {
70
71
72
73
74
75
76
77
78
79
80
81
82
83 return new Firstname(firstname);
84 }
85
86
87
88
89
90
91
92 @Override
93 public String stringValue()
94 {
95 return this.firstname;
96 }
97
98
99
100
101
102
103
104
105 @Override
106 public int hashCode()
107 {
108 return this.firstname.hashCode();
109 }
110
111
112
113
114
115
116
117
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
137
138
139
140
141
142
143
144
145 @Override
146 public String toString()
147 {
148 final var builder = new StringBuilder(21);
149 builder.append("Firstname[firstname=").append(this.firstname).append(']');
150 return builder.toString();
151 }
152
153
154
155
156
157
158
159
160
161 @Override
162 public int compareTo(final Firstname obj)
163 {
164 Objects.requireNonNull(obj, "obj");
165 return this.firstname.compareTo(obj.firstname);
166 }
167
168 }