You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Hans-Peter Stoerr (Jira)" <ji...@apache.org> on 2019/12/10 19:31:00 UTC

[jira] [Created] (COLLECTIONS-741) Signature of IteratorUtils.objectGraphIterator is wrong

Hans-Peter Stoerr created COLLECTIONS-741:
---------------------------------------------

             Summary: Signature of IteratorUtils.objectGraphIterator is wrong
                 Key: COLLECTIONS-741
                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-741
             Project: Commons Collections
          Issue Type: Bug
    Affects Versions: 4.4
            Reporter: Hans-Peter Stoerr


With the current signature
 Iterator<E> objectGraphIterator(E root, Transformer<? super E,? extends E> transformer)
 it is impossible to create any other iterator than an Iterator<Object> since the signature for the transformer just doesn't fit. (This problem extends to the underlying [ObjectGraphIterator|[https://commons.apache.org/proper/commons-collections/javadocs/api-4.4/org/apache/commons/collections4/iterators/ObjectGraphIterator.html]], too.) ) In the [example from the javadoc|[https://commons.apache.org/proper/commons-collections/javadocs/api-4.4/org/apache/commons/collections4/IteratorUtils.html#objectGraphIterator-E-org.apache.commons.collections4.Transformer-]], you would want to return an Iterator<Leaf>, but the root is of type forest (compare the full source in the [ObjectGraphIteratorTest|[https://git-wip-us.apache.org/repos/asf?p=commons-collections.git;a=blob;f=src/test/java/org/apache/commons/collections4/iterators/ObjectGraphIteratorTest.java;h=caff4ed312400c2a9e397c55edff2a5839981a68;hb=HEAD]]), the transformer takes either Forest, Tree, Branch or Leaf as argument and returns Leaf or Iterators of Tree, Branch or Leaf. There just is no common bound for these types. I'm afraid there is no way to get some type safety into this signature. I suggest to just relax it to

public static <E> Iterator<E> objectGraphIterator(final Object root,
 final Transformer<Object, Object> transformer)

since Object is the bound for all those arguments, anyway. (In many cases you would have the root and the first type variable the same type E, but the second type variable of the transformer would have to allow for both E and Iterator<E>, and thus be Object. So I'm not sure whether it makes any sense to make any restriction here.)

BTW: While that function has been useful for me, I'm a bit unhappy with it, anyway, since I see no acceptable way to use it to return all nodes of a tree, including the inner nodes, with it. At least not without serious hacks like wrapping the returned nodes in an Object[1] array.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)