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 * Parses a negative infinity (-inf). The '-' is optional: if the infinity is at 20 * the left side of the range, than it is assumed to be negative infinity. See 21 * {@link http://nagiosplugins.org/rfc/new_threshold_syntax} 22 * 23 * Example Input : -inf..100 24 * 25 * Produced Output : ..100 and calls the 26 * {@link RangeConfig#setNegativeInfinity(boolean)} passing <code>true</code> 27 * 28 * @author Massimiliano Ziccardi 29 */ 30 class NegativeInfinityStage extends Stage { 31 32 /** 33 * The infinity sign. 34 */ 35 private static final String INFINITY = "inf"; 36 37 /** 38 * The negative infinity sign. 39 */ 40 private static final String NEG_INFINITY = "-inf"; 41 42 /** 43 * 44 */ 45 protected NegativeInfinityStage() { 46 super("negativeinfinity"); 47 } 48 49 /** 50 * Parses the threshold to remove the matched '-inf' or 'inf' string. 51 * 52 * No checks are performed against the passed in string: the object 53 * assumes that the string is correct since the {@link #canParse(String)} 54 * method <b>must</b> be called <b>before</b> this method. 55 * 56 * @param threshold 57 * The threshold chunk to be parsed 58 * @param tc 59 * The threshold config object. This object will be populated 60 * according to the passed in threshold. 61 * @see RangeConfig#setNegativeInfinity(boolean) 62 * @return the remaining part of the threshold 63 */ 64 @Override 65 public String parse(final String threshold, final RangeConfig tc) { 66 67 tc.setNegativeInfinity(true); 68 69 if (threshold.startsWith(INFINITY)) { 70 return threshold.substring(INFINITY.length()); 71 } else { 72 return threshold.substring(NEG_INFINITY.length()); 73 } 74 } 75 76 /** 77 * Tells the parser if this stage is able to parse the current remaining 78 * threshold part. 79 * 80 * @param threshold 81 * The threshold part to be parsed. 82 * @return <code>true</code> if this object can consume a part of the 83 * threshold 84 */ 85 @Override 86 public boolean canParse(final String threshold) { 87 if (threshold == null) { 88 return false; 89 } 90 return threshold.startsWith("inf") || threshold.startsWith("-inf"); 91 } 92 93 /** 94 * This method is used to generate the exception message. 95 * 96 * @return the token that this stage is waiting for. 97 */ 98 @Override 99 public String expects() { 100 return "[-]inf"; 101 } 102 }