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.util.tokens;
018
019/**
020 * Matches, if the given subpattern does not match at the position before the
021 * current position in the token stream.
022 * 
023 * NOTE: passing a {@link TokenPattern} as the subpattern to this class will not
024 * result in an exception, but it will not yield the intuitive result. The given
025 * pattern will be matched against the token stream, starting with the token
026 * preceding the current token. It is therefore discouraged to pass a
027 * {@link TokenPattern} to this function as it makes understanding the pattern
028 * difficult.
029 */
030public class NotPrecededByPattern extends ZeroLengthTokenPatternBase {
031
032        /**
033         * The matcher that should not match at the position before the current
034         * position.
035         */
036        private final TokenPatternBase matcher;
037
038        /** Constructor. */
039        public NotPrecededByPattern(TokenPatternBase matcher) {
040                this.matcher = matcher;
041        }
042
043        /** {@inheritDoc} */
044        @Override
045        protected boolean conditionApplies(TokenStream stream) {
046                if (stream.moveBack() == null) {
047                        return true;
048                }
049                return matcher.matches(stream) == null;
050        }
051
052        /** {@inheritDoc} */
053        @Override
054        public String toString() {
055                return "NotPrecededBy " + matcher.toString();
056        }
057}