View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jxpath;
18  
19  import java.util.HashMap;
20  
21  /**
22   * A basic implementation of the Variables interface that uses a HashMap.
23   *
24   * @author Dmitri Plotnikov
25   * @version $Revision: 652925 $ $Date: 2008-05-03 00:05:41 +0200 (Sa, 03 Mai 2008) $
26   */
27  public class BasicVariables implements Variables {
28      private static final long serialVersionUID = 2708263960832062725L;
29  
30      /**
31       * Contains the values of declared variables
32       */
33      private HashMap vars = new HashMap();
34  
35      /**
36       * Returns true if the variable has been defined, even if the
37       * value of the variable is null.
38       *
39       * @param varName is a variable name without the "$" sign
40       *
41       * @return true if the variable is declared
42       */
43      public boolean isDeclaredVariable(String varName) {
44          return vars.containsKey(varName);
45      }
46  
47      /**
48       * Returns the value of the variable if it is defined,
49       * otherwise, throws IllegalArgumentException
50       *
51       * @param varName is a variable name without the "$" sign
52       *
53       * @return the value of the variable
54       */
55      public Object getVariable(String varName) {
56          // Note that a variable may be defined with a null value
57  
58          if (vars.containsKey(varName)) {
59              return vars.get(varName);
60          }
61  
62          throw new IllegalArgumentException(
63              "No such variable: '" + varName + "'");
64      }
65  
66      /**
67       * Defines a new variable with the specified value or modifies
68       * the value of an existing variable.
69       *
70       * @param varName is a variable name without the "$" sign
71       * @param value is the new value for the variable, which can be null
72       */
73      public void declareVariable(String varName, Object value) {
74          vars.put(varName, value);
75      }
76  
77      /**
78       * Removes an existing variable. May throw UnsupportedOperationException.
79       *
80       * @param varName is a variable name without the "$" sign
81       */
82      public void undeclareVariable(String varName) {
83          vars.remove(varName);
84      }
85  
86      public String toString() {
87          return vars.toString();
88      }
89  }