Traverse tree with results. Maybe type in Java?
Posted
by
Angelo.Hannes
on Programmers
See other posts from Programmers
or by Angelo.Hannes
Published on 2014-06-10T16:59:43Z
Indexed on
2014/06/10
21:46 UTC
Read the original article
Hit count: 248
I need to check a tree's node state. It can either be OK
or NOT_OK
. But that state is dependent on its children. So for a node to be OK
, every of its children needs to be OK
. If one or more of its children is NOT_OK
the whole node is NOT_OK
. To determine the state of a node I need to aggregate some properties of the node.
So I thought of two possible implementations. And they are more or less covered in this question: Why is Option/Maybe considered a good idea and checked exceptions are not?
Exception
I could pass the properties up the recursion path, and throw an exception if something went wrong.
Maybe
I implement an Maybe
type and let it either hold an error or the aggregated properties. Maybe it is more an Either
.
I tend towards the last option. And I'm thinking of an enum with two objects. Where I can additionally set those aggregated properties.
Am I on the right track?
I'm not familiar with the new JDK8 functional stuff. But I'm stuck on JDK7 anyway, so please focus on JDK7.
© Programmers or respective owner