feat: Optional при получении аттрибутов
This commit is contained in:
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user