### Question:
PositionFilter extends TokenFilter { @Override public Token next(Token nextToken) throws IOException { Token newToken = input.next(nextToken); if (position.equals(FIRST_POSITION)) { if (token != null) return null; else { token = newToken; return newToken; } } else if (position.equals(LAST_POSITION)) { Token lastToken = null; while (newToken != null) { lastToken = newToken; newToken = input.next(nextToken); } return lastToken; } else return newToken; } PositionFilter(TokenStream input, Integer position); @Override Token next(Token nextToken); Integer getPosition(); final static Integer FIRST_POSITION; final static Integer LAST_POSITION; }### Answer:
@Test public void testNext() throws Exception{ Collection<Token> tokens = new ArrayList<Token>(); tokens.add(newToken("token1", 0, 6)); tokens.add(newToken("token1", 7, 13)); tokens.add(newToken("token2", 14, 20)); tokens.add(newToken("token2", 21, 27)); tokens.add(newToken("token3", 28, 33)); tokens.add(newToken("token4", 34, 40)); TokenStream tokenStream = new CollectionTokenStream(tokens); TokenFilter filter = new PositionFilter(tokenStream, PositionFilter.FIRST_POSITION); Token nextToken = new Token(); filter.next(nextToken); assertNotNull(nextToken); assertEquals("token1", new String(nextToken.termBuffer(), 0, nextToken.termLength())); nextToken = filter.next(nextToken); assertNull(nextToken); nextToken = new Token(); filter = new PositionFilter(tokenStream, PositionFilter.LAST_POSITION); nextToken = filter.next(nextToken); assertNotNull(nextToken); assertEquals("token4", new String(nextToken.termBuffer(), 0, nextToken.termLength())); nextToken = filter.next(nextToken); assertNull(nextToken); }