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.shallowparser.util;
018
019import java.util.Arrays;
020import java.util.Comparator;
021import java.util.EnumSet;
022import java.util.List;
023
024import org.conqat.lib.commons.collections.CollectionUtils;
025
026import eu.cqse.check.framework.scanner.ELanguage;
027import eu.cqse.check.framework.scanner.ETokenType;
028import eu.cqse.check.framework.scanner.IToken;
029import eu.cqse.check.framework.scanner.ScannerUtils;
030
031/** Utilities for token related tests. */
032public class TokenTestUtils {
033
034        /**
035         * An empty list of integers, used to represent an empty sequence of indices.
036         */
037        public static final List<Integer> NO_INDICES = CollectionUtils.emptyList();
038
039        /** An empty list of token types. */
040        public static final List<ETokenType> NO_TYPES = CollectionUtils.emptyList();
041
042        /**
043         * {@link Comparator} for ITokens that compares {@link IToken#getOffset()},
044         * {@link IToken#getType()}, and {@link IToken#getText()} (in that order).
045         */
046        public static final Comparator<IToken> TYPE_TEXT_OFFSET_COMPARATOR = Comparator.comparing(IToken::getOffset)
047                        .thenComparing(IToken::getType).thenComparing(IToken::getText);
048
049        /** Tokenizes the input string to a token stream of the given language. */
050        public static List<IToken> tokenize(String input, ELanguage language) {
051                return ScannerUtils.getTokens(input, language);
052        }
053
054        /** Creates an enum set from the given token types. */
055        public static EnumSet<ETokenType> asEnumSet(ETokenType... sequence) {
056                EnumSet<ETokenType> set = EnumSet.noneOf(ETokenType.class);
057                set.addAll(Arrays.asList(sequence));
058                return set;
059        }
060}