Skip to content

Commit 3927a08

Browse files
committed
Fixed constructor, added null checks and added a fluent api helper for XMLElement
1 parent b444898 commit 3927a08

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/main/java/org/javawebstack/abstractdata/xml/XMLElement.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.LinkedHashMap;
55
import java.util.List;
66
import java.util.Map;
7+
import java.util.function.Consumer;
78
import java.util.stream.Collectors;
89

910
public class XMLElement implements XMLNode {
@@ -13,11 +14,15 @@ public class XMLElement implements XMLNode {
1314
private final List<XMLNode> childNodes = new ArrayList<>();
1415

1516
public XMLElement(String tagName) {
17+
if(tagName == null)
18+
throw new IllegalArgumentException("tagName can not be null");
1619
this.tagName = tagName;
1720
}
1821

1922
public XMLElement(String tagName, String text) {
20-
this(text);
23+
this(tagName);
24+
if(text == null)
25+
throw new IllegalArgumentException("text can not be null");
2126
text(text);
2227
}
2328

@@ -68,6 +73,12 @@ public XMLElement child(XMLNode childNode) {
6873
return this;
6974
}
7075

76+
public XMLElement onlyIf(boolean condition, Consumer<XMLElement> fn) {
77+
if(condition)
78+
fn.accept(this);
79+
return this;
80+
}
81+
7182
public Map<String, String> getAttributes() {
7283
return attributes;
7384
}

0 commit comments

Comments
 (0)