Skip to content

Commit 3c3296e

Browse files
committed
Added an isEmpty method to the iterable returned by BufferedTokenizerExt because it's expected in some use cases, like: https://github.com/logstash-plugins/logstash-input-file/blob/55a4a7099f05f29351672417036c1342850c7adc/lib/filewatch/watched_file.rb#L250
1 parent 0bff894 commit 3c3296e

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

logstash-core/src/main/java/org/logstash/common/BufferedTokenizerExt.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,32 @@ public IRubyObject extract(final ThreadContext context, IRubyObject data) {
8383
Iterable<String> extractor = tokenizer.extract(data.asJavaString());
8484

8585
// return an iterator that does the encoding conversion
86-
return RubyUtil.toRubyObject((Iterable<CharSequence>) () -> new BufferedTokenizer.IteratorDecorator<>(extractor.iterator()) {
86+
Iterator<CharSequence> rubyStringAdpaterIterator = new BufferedTokenizer.IteratorDecorator<>(extractor.iterator()) {
8787
@Override
8888
public CharSequence next() {
8989
return toEncodedRubyString(context, iterator.next());
9090
}
91-
});
91+
};
92+
93+
return RubyUtil.toRubyObject(new IterableAdapterWithEmptyCheck(rubyStringAdpaterIterator));
94+
}
95+
96+
// Iterator to Iterable adapter with addition of isEmpty method
97+
public static class IterableAdapterWithEmptyCheck implements Iterable<CharSequence> {
98+
private final Iterator<CharSequence> origIterator;
99+
100+
public IterableAdapterWithEmptyCheck(Iterator<CharSequence> origIterator) {
101+
this.origIterator = origIterator;
102+
}
103+
104+
@Override
105+
public Iterator<CharSequence> iterator() {
106+
return origIterator;
107+
}
108+
109+
public boolean isEmpty() {
110+
return origIterator.hasNext();
111+
}
92112
}
93113

94114
private RubyString toEncodedRubyString(ThreadContext context, String input) {

0 commit comments

Comments
 (0)