feat: Optional при получении аттрибутов

This commit is contained in:
2024-02-28 17:33:12 +03:00
parent 2853382889
commit ff7f2b6df3
3 changed files with 33 additions and 14 deletions

View File

@@ -56,21 +56,30 @@ public class XmlElement implements Iterable<XmlElement> {
return element.hasAttribute(key);
}
public String getAttribute(String key) {
return element.getAttribute(key);
public Optional<String> getAttribute(String key) {
if (element.hasAttribute(key)) {
return Optional.of(element.getAttribute(key));
} else {
return Optional.empty();
}
}
public int getAttributeAsInt(String key) {
var value = element.getAttribute(key);
return StringUtils.isEmpty(value) ? 0 : Integer.parseInt(value);
public OptionalInt getAttributeAsInt(String key) {
if (element.hasAttribute(key)) {
return OptionalInt.of(Integer.parseInt(element.getAttribute(key)));
} else {
return OptionalInt.empty();
}
}
public boolean getAttributeAsBool(String key) {
var value = element.getAttribute(key);
return !StringUtils.isEmpty(value) && Boolean.parseBoolean(value);
public Optional<Boolean> getAttributeAsBool(String key) {
if (element.hasAttribute(key)) {
return Optional.of(Boolean.parseBoolean(element.getAttribute(key)));
} else {
return Optional.empty();
}
}
@SuppressWarnings("NullableProblems")
@Override
public Iterator<XmlElement> iterator() {
return new XmlElementIterator(element.getChildNodes());