001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.duplications.token; 021 022 import java.util.Iterator; 023 import java.util.LinkedList; 024 import java.util.List; 025 import java.util.ListIterator; 026 027 public class TokenQueue implements Iterable<Token> { 028 029 private final LinkedList<Token> tokenQueue; 030 031 public TokenQueue(List<Token> tokenList) { 032 tokenQueue = new LinkedList<Token>(tokenList); 033 } 034 035 public TokenQueue() { 036 tokenQueue = new LinkedList<Token>(); 037 } 038 039 /** 040 * Retrieves, but does not remove, token from this queue. 041 * 042 * @return token from this queue, or <tt>null</tt> if this queue is empty. 043 */ 044 public Token peek() { 045 return tokenQueue.peek(); 046 } 047 048 /** 049 * Retrieves and removes token from this queue. 050 * 051 * @return token from this queue, or <tt>null</tt> if this queue is empty. 052 */ 053 public Token poll() { 054 return tokenQueue.poll(); 055 } 056 057 public int size() { 058 return tokenQueue.size(); 059 } 060 061 public void add(Token token) { 062 tokenQueue.addLast(token); 063 } 064 065 public boolean isNextTokenValue(String expectedValue) { 066 Token nextToken = tokenQueue.peek(); 067 if (nextToken == null) { 068 // queue is empty 069 return false; 070 } 071 return nextToken.getValue().equals(expectedValue); 072 } 073 074 public Iterator<Token> iterator() { 075 return tokenQueue.iterator(); 076 } 077 078 public void pushForward(List<Token> matchedTokenList) { 079 ListIterator<Token> iter = matchedTokenList.listIterator(matchedTokenList.size()); 080 while (iter.hasPrevious()) { 081 tokenQueue.addFirst(iter.previous()); 082 } 083 } 084 085 }