51 lines
1.4 KiB
Java
51 lines
1.4 KiB
Java
package com.github.dtschust.zork.parser.dom;
|
|
|
|
import org.w3c.dom.Element;
|
|
import org.w3c.dom.Node;
|
|
import org.w3c.dom.NodeList;
|
|
|
|
import java.util.AbstractList;
|
|
import java.util.Objects;
|
|
import java.util.RandomAccess;
|
|
|
|
public class DOMElementList extends AbstractList<DOMElement> implements RandomAccess {
|
|
private final NodeList list;
|
|
|
|
private DOMElementList(final NodeList l) {
|
|
list = l;
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object o) {
|
|
if (this == o) return true;
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
if (!super.equals(o)) return false;
|
|
DOMElementList that = (DOMElementList) o;
|
|
return list.equals(that.list);
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return Objects.hash(super.hashCode(), list);
|
|
}
|
|
|
|
static DOMElementList byTagName(final Element parent, final String name) {
|
|
return new DOMElementList(parent.getElementsByTagName(name));
|
|
}
|
|
|
|
@Override
|
|
public DOMElement get(int index) {
|
|
final Node e = list.item(index);
|
|
if (!(e instanceof Element)) {
|
|
// the contract of getElementsByTagName states that it returns a list of Element objects
|
|
throw new IllegalStateException("unreachable");
|
|
}
|
|
return DOMElement.of((Element) e);
|
|
}
|
|
|
|
@Override
|
|
public int size() {
|
|
return list.getLength();
|
|
}
|
|
}
|