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 50ccac0 commit 3242f09
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,33 @@ 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 {
if (index >= tokens.size) {
throw NoSuchElementException()
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.function.Consumer;
Expand Down Expand Up @@ -1308,6 +1310,11 @@ public void annotationInParameter(InputType inputType, StreamType streamType) th
String[] annotation = reader.getTypeAnnotations();
assertEquals(1, annotation.length);
assertEquals("foo", annotation[0]);
Iterator<String> annotationIterator = reader.iterateTypeAnnotations();
assertTrue(annotationIterator.hasNext());
assertEquals("foo", annotationIterator.next());
assertFalse(annotationIterator.hasNext());
assertThrows(NoSuchElementException.class, () -> annotationIterator.next());
assertNull(reader.next());
}
}
Expand Down

0 comments on commit 3242f09

Please sign in to comment.