View Javadoc

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