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   * The configured threshold parser.
20   * 
21   * @author Massimiliano Ziccardi
22   */
23  final class RangeStringParser {
24  
25  	/**
26  	 * The root of the parsing state machine.
27  	 */
28  	private static final Stage ROOT_STAGE = configureParser();
29  
30  	/**
31  	 * Private default constructor.
32  	 */
33  	private RangeStringParser() {
34  
35  	}
36  
37  	/**
38  	 * Configures the state machine.
39  	 * 
40  	 * @return The configured state machine.
41  	 */
42  	private static Stage configureParser() {
43  		Stage startStage = new StartStage();
44  		Stage negativeInfinityStage = new NegativeInfinityStage();
45  		Stage positiveInfinityStage = new PositiveInfinityStage();
46  		NegateStage negateStage = new NegateStage();
47  		BracketStage.OpenBracketStage openBraceStage = new BracketStage.OpenBracketStage();
48  		NumberBoundaryStage.LeftBoundaryStage startBoundaryStage = new NumberBoundaryStage.LeftBoundaryStage();
49  		NumberBoundaryStage.RightBoundaryStage rightBoundaryStage = new NumberBoundaryStage.RightBoundaryStage();
50  		SeparatorStage separatorStage = new SeparatorStage();
51  		BracketStage.ClosedBracketStage closedBraketStage = new BracketStage.ClosedBracketStage();
52  
53  		startStage.addTransition(negateStage);
54  		startStage.addTransition(openBraceStage);
55  		startStage.addTransition(negativeInfinityStage);
56  		startStage.addTransition(startBoundaryStage);
57  
58  		negateStage.addTransition(negativeInfinityStage);
59  		negateStage.addTransition(openBraceStage);
60  		negateStage.addTransition(startBoundaryStage);
61  
62  		openBraceStage.addTransition(startBoundaryStage);
63  		startBoundaryStage.addTransition(separatorStage);
64  		negativeInfinityStage.addTransition(separatorStage);
65  
66  		separatorStage.addTransition(positiveInfinityStage);
67  		separatorStage.addTransition(rightBoundaryStage);
68  
69  		rightBoundaryStage.addTransition(closedBraketStage);
70  
71  		return startStage;
72  	}
73  
74  	/**
75  	 * Parses the threshold.
76  	 * 
77  	 * @param range
78  	 *            The threshold to be parsed
79  	 * @param tc
80  	 *            The configuration
81  	 * @throws RangeException
82  	 *             -
83  	 */
84  	public static void parse(final String range, final RangeConfig tc)
85  			throws RangeException {
86  		if (range == null) {
87  			throw new RangeException("Null range specified");
88  		}
89  		ROOT_STAGE.parse(range, tc);
90  		checkBoundaries(tc);
91  	}
92  
93  	/**
94  	 * Checks that right boundary is greater than left boundary.
95  	 * 
96  	 * @param rc
97  	 *            The range configuration
98  	 * @throws RangeException
99  	 *             If right < left
100 	 */
101 	private static void checkBoundaries(final RangeConfig rc)
102 			throws RangeException {
103 		if (rc.isNegativeInfinity()) {
104 			// No other checks necessary. Negative infinity is less than any
105 			// number
106 			return;
107 		}
108 
109 		if (rc.isPositiveInfinity()) {
110 			// No other checks necessary. Positive infinity is greater than any
111 			// number
112 			return;
113 		}
114 
115 		if (rc.getLeftBoundary().compareTo(rc.getRightBoundary()) > 0) {
116 			throw new RangeException(
117 					"Left boundary must be less than right boundary (left:"
118 							+ rc.getLeftBoundary() + ", right:"
119 							+ rc.getRightBoundary() + ")");
120 		}
121 	}
122 }