001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 the ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package eu.cqse.check.framework.scanner;
018
019import java.util.List;
020
021import org.conqat.lib.commons.assertion.CCSMAssert;
022import org.conqat.lib.commons.collections.ILookahead;
023import org.conqat.lib.commons.error.NeverThrownRuntimeException;
024
025/**
026 * {@link ILookahead} on Lists of ITokens
027 */
028public class TokenListLookahead implements ILookahead<ETokenType, NeverThrownRuntimeException> {
029
030        /** Underlying list */
031        private List<IToken> list;
032
033        /** Current position in list */
034        private int index;
035
036        /** Set list and position */
037        public TokenListLookahead(List<IToken> tokens, int index) {
038                CCSMAssert.isFalse(index < 0, "Index must not be negative but was: " + index);
039
040                this.list = tokens;
041                this.index = index;
042        }
043
044        /** {@inheritDoc} */
045        @Override
046        public ETokenType lookahead(int lookahead) {
047                CCSMAssert.isFalse(lookahead < 0, "Lookahead must not be negative but was: " + lookahead);
048
049                int local = index + lookahead;
050                if (local >= list.size()) {
051                        return null;
052                }
053                return list.get(local).getType();
054        }
055}