Imagine hierarchical path stored in ordered List and its validation
List<String> path = Lists.newArrayList("path","to","end");
Iterator<String> iterator = path.iterator();
while (iterator.hasNext()) {
if (iterator.next() == null) {
while (iterator.hasNext()) {
if (iterator.next() != null) {
throw new Exception("Invalid path");
}
}
}
}
The algorithm is pretty straightforward.
Only leafs or nodes with all its descendant must be null.
Valid paths:
"path", "to", "end"
"path", "to", null
"path", null, null
null, null, null
Invalid paths:
"path", null, "end"
null, null, "end"
null, "to", "end"
null, "to", null
I am looking for way to rewrite it in a functional style.
nullis those places?Tupleclass, for example).