My understanding is this ask of mine is NOT possible in a straight forward way. but I want to find a solution that works.
Here is how I get an Iterable for NamedNodeMap(javax package);
private static Iterable<Node> iterableNamedNodeMap(NamedNodeMap namedNodeMap) {
return () -> new Iterator<Node>() {
private int index = 0;
@Override
public boolean hasNext() {
return index < namedNodeMap.getLength();
}
@Override
public Node next() {
if (!hasNext())
throw new NoSuchElementException();
return namedNodeMap.item(index++);
}
};
}
And here is the iterable for NodeList(javax)
private static Iterable<Node> iterableNamedNodeMap(NodeList nodeList) {
return () -> new Iterator<Node>() {
private int index = 0;
@Override
public boolean hasNext() {
return index < nodeList.getLength();
}
@Override
public Node next() {
if (!hasNext())
throw new NoSuchElementException();
return nodeList.item(index++);
}
};
}
Since they are pretty much identical except for the parameters, I was hoping for something like this, which of-course is not right. Both NodeList and NamedNodeMap does not implement a common interface. so what is the best way to do here.
private static <T extends NodeList | NamedNodeMap> Iterable<Node> iterableNamedNodeMap(T in) {
return () -> new Iterator<Node>() {
private int index = 0;
@Override
public boolean hasNext() {
return index < in.getLength();
}
@Override
public Node next() {
if (!hasNext())
throw new NoSuchElementException();
return in.item(index++);
}
};