1 /******************************************************************************* 2 * Copyright (c) 2007, 2014 Massimiliano Ziccardi 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 *******************************************************************************/ 16 package it.jnrpe.utils.thresholds; 17 18 /** 19 * This stage handles the negate character ('^'). 20 * 21 * Example Input : ^(0..100 22 * 23 * Produced Output : (0..100 and calls the 24 * {@link RangeConfig#setNegate(boolean)} passing <code>true</code> 25 * 26 * @author Massimiliano Ziccardi 27 */ 28 class NegateStage extends Stage { 29 30 /** 31 * 32 */ 33 protected NegateStage() { 34 super("negate"); 35 } 36 37 /** 38 * Parses the threshold to remove the matched '^' char. 39 * 40 * No checks are performed against the passed in string: the object 41 * assumes that the string is correct since the {@link #canParse(String)} 42 * method <b>must</b> be called <b>before</b> this method. 43 44 * @param threshold 45 * The threshold chunk to be parsed 46 * @param tc 47 * The threshold config object. This object will be populated 48 * according to the passed in threshold. 49 * @return the remaining part of the threshold 50 */ 51 @Override 52 public String parse(final String threshold, final RangeConfig tc) { 53 tc.setNegate(true); 54 return threshold.substring(1); 55 } 56 57 /** 58 * Tells the parser if this stage is able to parse the current remaining 59 * threshold part. 60 * 61 * @param threshold 62 * The threshold part to be parsed. 63 * @return <code>true</code> if this object can consume a part of the 64 * threshold 65 */ 66 public boolean canParse(final String threshold) { 67 if (threshold == null) { 68 return false; 69 } 70 return threshold.startsWith("^"); 71 } 72 73 /** 74 * This method is used to generate the exception message. 75 * 76 * @return the token that this stage is waiting for. 77 */ 78 @Override 79 public String expects() { 80 return "^"; 81 } 82 }