hunterhacker/jdom — Issue #58: Make XPath XPath<T> with List<T> return types.
Author: rolfl
Created: 2012-01-17 13:34:13+00:00
State: open

Right, what about the generics and XPath return types....? Well, this is a complicated one, and I thought about it hard. The problem boils down to the fact that XPath expressions can return Boolean, Double, and String in addition to whatever JDOM nodes are selected. There is no common 'base' to selectNodes results other than 'Object'. Really!

This means that XPath has to be *able* to return List. There is no option.

This problem is what inspired a lot of the Filters class, because the List return type is not convenient, yet it can be coerced into something that *is* convenient. The Filter instances do full type (and other) checking on the values in the List and not only re-casts the generic type of the result, but it also 'silently' removes any content that cannot be coerced.

Thus my intention was that people would do things like:

```java
XPathFactory xpfactory = XPathFactory.newInstance();
XPath xpath = xpfactory.compile("//*");
List nodes = Filters.element().filter(xpath.selectNodes(document));
```

I can see that this model could be modified somewhat to put the filter in at the XPath compile time to become something like:

```java
XPathFactory xpfactory = XPathFactory.newInstance();
XPath xpath = xpfactory.compile("//*", Filter.element());
List nodes = xpath.selectNodes(document);
```

I think that is a valuable modification, and it is nice because the compile(String) would return XPath, and the compile(String, Filter) would return XPath. This would all still be backward compatible with JDOM 1.x