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 java.math.BigDecimal;
19  
20  /**
21   * Builds the range object parsing the range passed inside the threshold
22   * definition.
23   * 
24   * A range can have the following format:
25   * <p>
26   * 
27   * [^](start..end)
28   * <p>
29   * 
30   * Where:
31   * <ul>
32   * <li>start and end must be defined
33   * <li>start and end match the regular expression /^[+-]?[0-9]+\.?[0-9]*$|^inf$/
34   * (ie, a numeric or "inf")
35   * <li>start &lt= end
36   * <li>if start = "inf", this is negative infinity. This can also be written as
37   * "-inf"
38   * <li>if end = "inf", this is positive infinity
39   * <li>endpoints are excluded from the range if () are used, otherwise endpoints
40   * are included in the range
41   * <li>alert is raised if value is within start and end range, unless ^ is used,
42   * in which case alert is raised if outside the range
43   * </ul>
44   * 
45   * @author Massimiliano Ziccardi
46   */
47  class Range extends RangeConfig {
48  
49  	/**
50  	 * The unparsed range string.
51  	 */
52  	private final String rangeString;
53  
54  	/**
55  	 * 
56  	 * @param range
57  	 *            The range to be parsed
58  	 * @throws RangeException
59  	 *             -
60  	 */
61  	public Range(final String range) throws RangeException {
62  		rangeString = range;
63  		parse(range);
64  	}
65  
66  	/**
67  	 * 
68  	 * @param range
69  	 *            The range to be parsed
70  	 * @throws RangeException
71  	 *             -
72  	 */
73  	private void parse(final String range) throws RangeException {
74  		RangeStringParser.parse(range, this);
75  	}
76  
77  	/**
78  	 * Multiply the value with the right multiplier based on the prefix.
79  	 * 
80  	 * @param value
81  	 *            The value
82  	 * @param prefix
83  	 *            The prefix
84  	 * @return The result
85  	 */
86  	private BigDecimal convert(final BigDecimal value, final Prefixes prefix) {
87  		if (prefix == null) {
88  			return value;
89  		}
90  
91  		return prefix.convert(value);
92  	}
93  
94  	/**
95  	 * Evaluates if the passed in value falls inside the range. The negation is
96  	 * ignored.
97  	 * 
98  	 * @param value
99  	 *            The value to evaluate
100 	 * @param prefix
101 	 *            Used to multiply the range boundaries.
102 	 * @return <code>true</code> if the value falls inside the range. The
103 	 *         negation ('^') is ignored.
104 	 */
105 	private boolean evaluate(final BigDecimal value, final Prefixes prefix) {
106 		if (value == null) {
107 			throw new NullPointerException("Passed in value can't be null");
108 		}
109 
110 		if (!isNegativeInfinity()) {
111 			switch (value.compareTo(convert(getLeftBoundary(), prefix))) {
112 			case 0:
113 				if (!isLeftInclusive()) {
114 					return false;
115 				}
116 				break;
117 			case -1:
118 				return false;
119 			default:
120 			}
121 		}
122 
123 		if (!isPositiveInfinity()) {
124 			switch (value.compareTo(convert(getRightBoundary(), prefix))) {
125 			case 0:
126 				if (!isRightInclusive()) {
127 					return false;
128 				}
129 				break;
130 			case 1:
131 				return false;
132 			default:
133 			}
134 		}
135 
136 		return true;
137 	}
138 
139 	/**
140 	 * @param value
141 	 *            The value to be evaluated.
142 	 * @return Whether the passed in value falls inside this range.
143 	 */
144 	public boolean isValueInside(final BigDecimal value) {
145 		return isValueInside(value, null);
146 	}
147 
148 	/**
149 	 * @param value
150 	 *            The value to be evaluated.
151 	 * @param prefix
152 	 *            Used to multiply the range boundaries.
153 	 * @return Whether the passed in value falls inside this range.
154 	 */
155 	public boolean isValueInside(final BigDecimal value, final Prefixes prefix) {
156 		boolean res = evaluate(value, prefix);
157 
158 		if (isNegate()) {
159 			return !res;
160 		} else {
161 			return res;
162 		}
163 	}
164 
165 	/**
166 	 * @param value
167 	 *            The value to be evaluated.
168 	 * @return Whether the passed in value falls inside this range.
169 	 */
170 	public boolean isValueInside(final int value) {
171 		return isValueInside(new BigDecimal(value));
172 	}
173 
174 	/**
175 	 * @param value
176 	 *            The value to be evaluated.
177 	 * @return Whether the passed in value falls inside this range.
178 	 */
179 	public boolean isValueInside(final long value) {
180 		return isValueInside(new BigDecimal(value));
181 	}
182 
183 	/**
184 	 * @return The unparsed range string.
185 	 */
186 	String getRangeString() {
187 		return rangeString;
188 	}
189 }