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 * When the threshold parser is executing this stage it means it is expecting an
20 * open or closed bracket.
21 *
22 * @author Massimiliano Ziccardi
23 */
24 abstract class BracketStage extends Stage {
25
26 /**
27 * The bracket to search for (open or closed).
28 */
29 private final String bracket;
30
31 /**
32 * Builds the bracket stage.
33 *
34 * @param stageName
35 * The name of the stage
36 * @param matchedBracket
37 * The bracket to search for
38 */
39 protected BracketStage(final String stageName, final char matchedBracket) {
40 super(stageName);
41 bracket = "" + matchedBracket;
42 }
43
44 /**
45 * Parses the threshold to remove the matched braket.
46 *
47 * No checks are performed against the passed in string: the object
48 * assumes that the string is correct since the {@link #canParse(String)}
49 * method <b>must</b> be called <b>before</b> this method.
50 *
51 * @param threshold
52 * The threshold to parse
53 * @param tc
54 * The threshold config object. This object will be populated
55 * according to the passed in threshold.
56 * @return the remaining part of the threshold
57 */
58 @Override
59 public String parse(final String threshold, final RangeConfig tc) {
60 configure(tc);
61 return threshold.substring(1);
62 }
63
64 /**
65 * Tells the parser if this stage is able to parse the current remaining
66 * threshold part.
67 *
68 * @param threshold
69 * The threshold part to be parsed.
70 * @return <code>true</code> if this object can consume a part of the
71 * threshold
72 */
73 @Override
74 public boolean canParse(final String threshold) {
75 if (threshold == null) {
76 return false;
77 }
78
79 return threshold.startsWith(bracket);
80 }
81
82 /**
83 * This method is used to generate the exception message.
84 *
85 * @return the token that this stage is waiting for.
86 */
87 @Override
88 public String expects() {
89 return bracket;
90 }
91
92 /**
93 * Sets the {@link RangeConfig} object as not
94 * left/right inclusive.
95 *
96 * @param rc The range configuration
97 */
98 protected abstract void configure(final RangeConfig rc);
99
100 /**
101 * This class implements a 'closed brace stage'. It consumes, if present, a
102 * closed brace at the beginning of the received threshold chunk.
103 *
104 * @author Massimiliano Ziccardi
105 */
106 public static class ClosedBracketStage extends BracketStage {
107
108 /**
109 *
110 */
111 public ClosedBracketStage() {
112 super("closedbracket", ')');
113 }
114
115 /**
116 * Closed bracket can be the end of the range.
117 * @return <code>true</code>
118 */
119 public final boolean isLeaf() {
120 return true;
121 }
122
123 /**
124 * Sets the {@link RangeConfig} object as not
125 * right inclusive.
126 *
127 * @param rc The range configuration
128 */
129 protected final void configure(final RangeConfig rc) {
130 rc.setRightInclusive(false);
131 }
132 }
133
134 /**
135 * This class implements a 'open brace stage'. It consumes, if present, an
136 * open brace at the beginning of the received threshold chunk.
137 *
138 * Example Input : (0..100
139 *
140 * Produced Output : 0..100 and calls the
141 * {@link RangeConfig#setLeftInclusive(boolean)} passing
142 * <code>true</code>
143 *
144 * @author Massimiliano Ziccardi
145 */
146 public static class OpenBracketStage extends BracketStage {
147
148 /**
149 *
150 */
151 public OpenBracketStage() {
152 super("openbracket", '(');
153 }
154
155 /**
156 * Sets the {@link RangeConfig} object as not
157 * left inclusive.
158 *
159 * @param rc The range configuration
160 */
161 protected final void configure(final RangeConfig rc) {
162 rc.setLeftInclusive(false);
163 }
164
165 }
166
167 }