use PECS principle in TreeTraverser
See original GitHub issueCurrently, TreeTraverser<T>
has a method Iterable<T> children(T root)
. Since for traversal, the returned Iterable
is only used as a producer, the return type can be Iterable<? extends T>
(and the using()
static function can take a Function<T, ? extends Iterable<? extends T>>
).
The use case is a tree model where at some level only leaf nodes exist, so there is for example a List<LeafNode>
(where LeafNode
extends T). With the current implementation, this has to be copied into a new list in order to compile (or using some ugly unsafe double-casting).
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (7 by maintainers)
Top Results From Across the Web
What is PECS (Producer Extends Consumer Super)?
This principle states that: Use an extends wildcard when you only get values out of a structure. Use a super wildcard when you...
Read more >Java Generics PECS - Producer Extends Consumer Super
Yesterday, I was going through some java collection APIs and I found two methods primarily used for adding elements into a collection.
Read more >Java: Producer Extends, Consumer Super - Medium
It is easy to remember which wildcard to use at which occasion using the word acronym PECS, Producer Extends, Consumer Super.
Read more >Java Generics PECS – Producer Extends Consumer Super
In this article, we'll explore the usage of Java Generics when it comes to producing and consuming collections.
Read more >Generics PECS - [OOP & Java #11] - DEV Community
We will use the following code for ease of reference and discussion. // assignment List<? exten... Tagged with computerscience, java.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kevinb9n, Personally I feel that users who test with such fine granularity have to accept the extra brittleness, certainly for an API that is in beta. After all the fix is trivial (just change the variable type). Of course, they could also just pass a Java8 method reference to TreeTraverser.using(), and then test the method directly.
Indeed, my uses cases were along the lines of the example that @perceptron8 has shown. Another possibility (besides having the wildcard in the superclass’s return type for
getChildren()
) is that the function to map parent to children usesinstanceof
and a (clean, safe) cast to distinguish between different types, some of which may return a specific type of children.Regarding the use of
Set
vsIterable
: the fact that one needs to copy the children in case the model doesn’t return aSet
is one thing that worries me a bit. The fact that one will need to copy them specifically into aLinkedHashSet
(or similarSet
implementation) in order to preserve the iteration order of the model, is another thing. Uses cases where the order is important: DOM traversal, UI component hierarchy, …