Skip to content

Commit 248931b

Browse files
committed
Share type and title validation logic
1 parent 50d75cb commit 248931b

1 file changed

Lines changed: 28 additions & 21 deletions

File tree

commonmark-ext-gfm-alerts/src/main/java/org/commonmark/ext/gfm/alerts/AlertsExtension.java

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public static class Builder {
128128
* Must not be null/empty or contain any null/empty keys or
129129
* values. Additionally, all alert types must be uppercase.
130130
* @return {@code this}
131+
* @see Builder#addCustomType(String, String)
131132
*/
132133
public Builder setAllowedTypes(Map<String, String> allowedTypes) {
133134
Objects.requireNonNull(allowedTypes, "allowedTypes must not be null");
@@ -136,18 +137,7 @@ public Builder setAllowedTypes(Map<String, String> allowedTypes) {
136137
}
137138

138139
for (Map.Entry<String, String> entry : allowedTypes.entrySet()) {
139-
var type = Objects.requireNonNull(entry.getKey(), "Types must not be null");
140-
if (type.isEmpty()) {
141-
throw new IllegalArgumentException("Types must not be empty");
142-
}
143-
if (!type.equals(type.toUpperCase(Locale.ROOT))) {
144-
throw new IllegalArgumentException("Types must be uppercase: " + type);
145-
}
146-
147-
var defaultTitle = Objects.requireNonNull(entry.getValue(), "Default titles must not be null: " + type);
148-
if (defaultTitle.isEmpty()) {
149-
throw new IllegalArgumentException("Default titles must not be empty: " + type);
150-
}
140+
validateTypeAndTitle(entry.getKey(), entry.getValue());
151141
}
152142

153143
this.allowedTypes = new HashMap<>(allowedTypes);
@@ -163,17 +153,10 @@ public Builder setAllowedTypes(Map<String, String> allowedTypes) {
163153
* @param type the alert type (must be uppercase)
164154
* @param title the default title for this alert type
165155
* @return {@code this}
156+
* @see Builder#setAllowedTypes(Map)
166157
*/
167158
public Builder addCustomType(String type, String title) {
168-
if (type == null || type.isEmpty()) {
169-
throw new IllegalArgumentException("Type must not be null or empty");
170-
}
171-
if (title == null || title.isEmpty()) {
172-
throw new IllegalArgumentException("Title must not be null or empty");
173-
}
174-
if (!type.equals(type.toUpperCase(Locale.ROOT))) {
175-
throw new IllegalArgumentException("Type must be uppercase: " + type);
176-
}
159+
validateTypeAndTitle(type, title);
177160
allowedTypes.put(type, title);
178161
return this;
179162
}
@@ -214,5 +197,29 @@ public Builder allowNestedAlerts(boolean allow) {
214197
public Extension build() {
215198
return new AlertsExtension(this);
216199
}
200+
201+
/**
202+
* Checks whether an alert type and default title are valid.
203+
*
204+
* @param type The type to validate:
205+
* <p>
206+
* - Must not be null or empty
207+
* - Must be uppercase
208+
* @param title The default title to validate. Must not be null or empty.
209+
*/
210+
private void validateTypeAndTitle(String type, String title) {
211+
Objects.requireNonNull(type, "Type must not be null");
212+
if (type.isEmpty()) {
213+
throw new IllegalArgumentException("Type must not be empty");
214+
}
215+
if (!type.equals(type.toUpperCase(Locale.ROOT))) {
216+
throw new IllegalArgumentException("Type must be uppercase: " + type);
217+
}
218+
219+
Objects.requireNonNull(title, "Default title must not be null: " + type);
220+
if (title.isEmpty()) {
221+
throw new IllegalArgumentException("Default title must not be empty: " + type);
222+
}
223+
}
217224
}
218225
}

0 commit comments

Comments
 (0)