Skip to content

Commit

Permalink
Uses an iterator that adapts from SymbolToken to String in MacroEvalu…
Browse files Browse the repository at this point in the history
…atorAsIonReader.iterateTypeAnnotations instead of eagerly allocating a new collection.
  • Loading branch information
tgregg committed Jan 16, 2025
1 parent 16b3cd0 commit 8c5eabd
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,31 @@ class MacroEvaluatorAsIonReader(

override fun getTypeAnnotations(): Array<String>? = currentValueExpression?.annotations?.let { Array(it.size) { i -> it[i].assumeText() } }
override fun getTypeAnnotationSymbols(): Array<SymbolToken>? = currentValueExpression?.annotations?.toTypedArray()
// TODO: Make this into an iterator that unwraps the SymbolTokens as it goes instead of allocating a new list

private class SymbolTokenAsStringIterator(val tokens: List<SymbolToken>) : MutableIterator<String> {

var index = 0

override fun hasNext(): Boolean {
return index < tokens.size
}

override fun next(): String {
return tokens[index++].assumeText()
}

override fun remove() {
throw UnsupportedOperationException("This iterator does not support removal")
}

}

override fun iterateTypeAnnotations(): MutableIterator<String> {
return currentValueExpression?.annotations?.mapTo(mutableListOf()) { it.assumeText() }?.iterator()
?: return Collections.emptyIterator()
return if (currentValueExpression?.annotations?.isNotEmpty() == true) {
SymbolTokenAsStringIterator(currentValueExpression!!.annotations)
} else {
Collections.emptyIterator()
}
}

override fun isInStruct(): Boolean = containerStack.peek()?.container?.type == IonType.STRUCT
Expand Down

0 comments on commit 8c5eabd

Please sign in to comment.