1
2
3
4 package de.powerstat.validation.values.strategies;
5
6
7 import java.util.Map;
8 import java.util.Objects;
9 import java.util.concurrent.ConcurrentHashMap;
10
11 import de.powerstat.validation.containers.NTuple4;
12 import de.powerstat.validation.values.EMail;
13
14
15
16
17
18 public class UsernameConfigurableStrategy implements IUsernameStrategy
19 {
20
21
22
23
24
25
26
27
28 private static final Map<NTuple4<Integer, Integer, String, HandleEMail>, UsernameConfigurableStrategy> CACHE = new ConcurrentHashMap<>();
29
30
31
32
33 private final int minLength;
34
35
36
37
38 private final int maxLength;
39
40
41
42
43 private final String regexp;
44
45
46
47
48
49
50 private final HandleEMail emailHandling;
51
52
53
54
55
56 public enum HandleEMail
57 {
58
59
60
61 EMAIL_DENIED(0),
62
63
64
65
66 EMAIL_REQUIRED(1),
67
68
69
70
71 EMAIL_POSSIBLE(2);
72
73
74
75
76
77 private final int action;
78
79
80
81
82
83
84
85 HandleEMail(final int action)
86 {
87 this.action = action;
88 }
89
90
91
92
93
94
95
96 public int getAction()
97 {
98 return this.action;
99 }
100
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114 protected UsernameConfigurableStrategy(final int minLength, final int maxLength, final String regexp, final HandleEMail emailHandling)
115 {
116 super();
117 Objects.requireNonNull(regexp, "regexp");
118 Objects.requireNonNull(emailHandling, "emailHandling");
119 if (minLength < 0)
120 {
121 throw new IllegalArgumentException("minLength must be >= 0");
122 }
123 if (maxLength < minLength)
124 {
125 throw new IllegalArgumentException("maxLength >= minLength");
126 }
127 if ((regexp.charAt(0) != '^') || !regexp.endsWith("$"))
128 {
129 throw new IllegalArgumentException("regexp does not start with ^ or ends with $");
130 }
131 this.minLength = minLength;
132 this.maxLength = maxLength;
133 this.regexp = regexp;
134 this.emailHandling = emailHandling;
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149 public static IUsernameStrategy of(final int minLength, final int maxLength, final String regexp, final HandleEMail emailHandling)
150 {
151 final NTuple4<Integer, Integer, String, HandleEMail> tuple = NTuple4.of(minLength, maxLength, regexp, emailHandling);
152 synchronized (UsernameConfigurableStrategy.class)
153 {
154 UsernameConfigurableStrategy obj = UsernameConfigurableStrategy.CACHE.get(tuple);
155 if (obj != null)
156 {
157 return obj;
158 }
159 obj = new UsernameConfigurableStrategy(minLength, maxLength, regexp, emailHandling);
160 UsernameConfigurableStrategy.CACHE.put(tuple, obj);
161 return obj;
162 }
163 }
164
165
166
167
168
169
170
171
172
173 @Override
174 public boolean validationStrategy(final String username)
175 {
176 if ((username.length() < this.minLength) || (username.length() > this.maxLength))
177 {
178 throw new IllegalArgumentException("To short or long for an username");
179 }
180 if (!username.matches(this.regexp))
181 {
182 throw new IllegalArgumentException("Username contains illegal character");
183 }
184
185 boolean checkEMail = false;
186 try
187 {
188 EMail.of(username);
189 checkEMail = true;
190 }
191 catch (final IllegalArgumentException ignore)
192 {
193
194 }
195 if ((this.emailHandling == HandleEMail.EMAIL_REQUIRED) && !checkEMail)
196 {
197 throw new IllegalArgumentException("Username must be an email address");
198 }
199 if ((this.emailHandling == HandleEMail.EMAIL_DENIED) && checkEMail)
200 {
201 throw new IllegalArgumentException("EMail address is not allowed as username");
202 }
203 return checkEMail;
204 }
205
206 }