Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 980626b

Browse files
committed
UastAbstractIter: refactor to use Option
Signed-off-by: Alexander Bezzubov <[email protected]>
1 parent 11845fb commit 980626b

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/main/scala/org/bblfsh/client/v2/libuast/Libuast.scala

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,40 @@ object Libuast {
1818
* It brides the gap between the contracts of a Scala iterator (.hasNext()/.next()) and
1919
* a native Libuast iterator (.next() == null at the end).
2020
* */
21-
abstract class UastAbstractIter[T](var node: T, var treeOrder: Int, var iter: Long, var ctx: Long)
21+
abstract class UastAbstractIter[T >:Null](var node: T, var treeOrder: Int, var iter: Long, var ctx: Long)
2222
extends Iterator[T] {
2323
private var closed = false
24-
private var lookedAhead = false
25-
private var nextNode: T = _
24+
private var nextNode: Option[T] = None
2625

27-
private def lookahead(): T = {
28-
lookedAhead = true
26+
private def lookahead(): Option[T] = {
2927
val node = nativeNext(iter)
3028
if (node == null) {
3129
close()
30+
None
31+
} else {
32+
Some(node)
3233
}
33-
node
3434
}
3535

3636
/** True only if the next element is not null */
3737
override def hasNext(): Boolean = {
3838
if (closed) {
3939
return false
4040
}
41-
if (lookedAhead) {
41+
if (nextNode.isDefined) {
4242
return true
4343
}
4444
nextNode = lookahead()
45-
nextNode != null
45+
nextNode.isDefined
4646
}
4747

4848
override def next(): T = {
4949
if (hasNext()) {
50-
lookedAhead = false
51-
return nextNode
50+
val next = nextNode.get
51+
nextNode = None
52+
return next
5253
}
53-
nextNode
54+
null
5455
}
5556

5657
def close() = {

0 commit comments

Comments
 (0)