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.util.HashMap;
19  import java.util.Map;
20  import java.util.Set;
21  
22  /**
23   * Base class for all the parsing stages. A parsing stage is composed of a name
24   * and a set of named transitions. This way we can configure the parser as a
25   * state machine.
26   *
27   * @author Massimiliano Ziccardi
28   */
29  abstract class Stage {
30      /**
31       * The stage name.
32       */
33      private final String name;
34  
35      /**
36       * The sets of stage transitions.
37       */
38      private Map<String, Stage> nextStagesMap = new HashMap<String, Stage>();
39  
40      /**
41       * @param stageName
42       *            The stage name
43       */
44      protected Stage(final String stageName) {
45          name = stageName;
46      }
47  
48      /**
49       * Adds a possible transition to this stage.
50       *
51       * @param stage
52       *            The transition
53       */
54      public void addTransition(final Stage stage) {
55          nextStagesMap.put(stage.name, stage);
56      }
57  
58      /**
59       * Returns the name of this stage.
60       *
61       * @return The name of this stage
62       */
63      public String getName() {
64          return name;
65      }
66  
67      /**
68       * Returns a named transition of this stage.
69       *
70       * @param stageName
71       *            The requested transition
72       * @return The transition if found. Null otherwise.
73       */
74      public Stage getTransition(final String stageName) {
75          return nextStagesMap.get(stageName);
76      }
77  
78      /**
79       * Returns the list of the possible transitions from this stage.
80       *
81       * @return the list of the possible transitions from this stage
82       */
83      public Set<String> getTransitionNames() {
84          return nextStagesMap.keySet();
85      }
86  
87      /**
88       * A stage is a leaf if no transition are possible from here..
89       *
90       * @return <code>true</code> if no transitions are available from here.
91       */
92      public boolean isLeaf() {
93          return this.nextStagesMap.isEmpty();
94      }
95  
96      /**
97       * Consumes a part of the threshold and configure the
98       * {@link RangeConfig} object according to the swallowed part of the
99       * threshold.
100      *
101      * @param threshold
102      *            The threshold to consume.
103      * @param tc
104      *            The threshold configuration object
105      * @return The remaining unparsed part of the threshold
106      * @throws RangeException
107      *             -
108      */
109     public abstract String parse(final String threshold, RangeConfig tc)
110             throws RangeException;
111 
112     /**
113      * Tells if the current stage is able to parse the given threshold.
114      * @param threshold The threshold to be parsed
115      * @return The remaining tokens
116      */
117     public abstract boolean canParse(String threshold);
118 
119     /**
120      * @return The list of tokens expected by this stage.
121      */
122     public abstract String expects();
123 }