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  import it.jnrpe.utils.BadThresholdException;
19  
20  /**
21   * Builds a {@link ThresholdsEvaluator} object.
22   *
23   * @author Massimiliano Ziccardi
24   *
25   */
26  public class ThresholdsEvaluatorBuilder {
27  
28      /**
29       * The threshold evaluator instance.
30       */
31      private ThresholdsEvaluator thresholds = new ThresholdsEvaluator();
32  
33      /**
34       * Default constructor.
35       */
36      public ThresholdsEvaluatorBuilder() {
37      }
38  
39      /**
40       * Adds a threshold to the threshold evaluator object.
41       *
42       * @param threshold
43       *            The threhsold text
44       * @return this
45       * @throws BadThresholdException
46       *             -
47       */
48      public final ThresholdsEvaluatorBuilder
49              withThreshold(final String threshold)
50                      throws BadThresholdException {
51          thresholds.addThreshold(new Threshold(threshold));
52          return this;
53      }
54  
55      /**
56       * This method allows to specify thresholds using the old format.
57       *
58       * @param metric The metric for which this threshold must be configured
59       * @param okRange The ok range (can be null)
60       * @param warnRange The warning range (can be null)
61       * @param critRange The critical range (can be null).
62       * @return this
63       * @throws BadThresholdException If the threshold can't be parsed.
64       */
65      public final ThresholdsEvaluatorBuilder
66              withLegacyThreshold(final String metric, final String okRange,
67                      final String warnRange, final String critRange)
68                      throws BadThresholdException {
69          LegacyRange ok = null, warn = null, crit = null;
70  
71          if (okRange != null) {
72              ok = new LegacyRange(okRange);
73          }
74          if (warnRange != null) {
75              warn = new LegacyRange(warnRange);
76          }
77          if (critRange != null) {
78              crit = new LegacyRange(critRange);
79          }
80  
81          thresholds.addThreshold(new LegacyThreshold(metric, ok, warn, crit));
82          return this;
83      }
84  
85      /**
86       * @return the threshold evaluator.
87       */
88      public final ThresholdsEvaluator create() {
89          return thresholds;
90      }
91  }