Lucene TokenFilter: How to recognize the last token from TokenStream?
<p>For Lucene, I want to do some processing only on the last token of the tokenStream with tokenFilter. For example, given sentence "hello my world", only apply my processing to "world", not other tokens. </p>

<p>I can do by iterating the entire input of tokenStream first in order to get the offset of the last token, and then re-starting from the first token. Because I already know the offset of the last token, I can recognize whether the current token is a last token or not.</p>

<p>However, since looping twice is inefficient for sure, I want to do it with iterating tokenStream only once, but it seems like hard to find right way.</p>

<p>For example, suppose MyFilter looks like: (Sure, this MyFilter is basic structure of the TokenFilter). </p>

<pre><code>public class MyFilter extends TokenFilter{

    public MyFilter(TokenStream input){
        super(input);
    }

    @Override
    public boolean incrementToken() throws IOException {
        if (input.incrementToken()){

            /*
                if(current token is the last token):
                    Want to apply something only to the last token.
            */

            return true;
        }
        return false;
    }
}
</code></pre>

<p>How to recognize if the current token is the last one or not?</p>