    1   /*
    2    * Copyright 1999-2004 The Apache Software Foundation
    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 org.apache.commons.jxpath;
    17
    18  import java.util.HashMap;
    19
    20  /***
    21   * A basic implementation of the Variables interface that uses a HashMap.
    22   *
    23   * @author Dmitri Plotnikov
    24   * @version $Revision: 1.7 $ $Date: 2004/02/29 14:17:42 $
    25   */
    26  public class BasicVariables implements Variables {
    27
    28      /***
    29       * Contains the values of declared variables
    30       */
    31      private HashMap vars = new HashMap();
    32
    33      /***
    34       * Returns true if the variable has been defined, even if the
    35       * value of the variable is null.
    36       *
    37       * @param varName is a variable name without the "$" sign
    38       *
    39       * @return true if the variable is declared
    40       */
    41      public boolean isDeclaredVariable(String varName) {
    42          return vars.containsKey(varName);
    43      }
    44
    45      /***
    46       * Returns the value of the variable if it is defined,
    47       * otherwise, throws IllegalArgumentException
    48       *
    49       * @param varName is a variable name without the "$" sign
    50       *
    51       * @return the value of the variable
    52       */
    53      public Object getVariable(String varName) {
    54          // Note that a variable may be defined with a null value
    55
    56          if (vars.containsKey(varName)) {
    57              return vars.get(varName);
    58          }
    59
    60          throw new IllegalArgumentException(
    61              "No such variable: '" + varName + "'");
    62      }
    63
    64      /***
    65       * Defines a new variable with the specified value or modifies
    66       * the value of an existing variable.
    67       *
    68       * @param varName is a variable name without the "$" sign
    69       * @param value is the new value for the variable, which can be null
    70       */
    71      public void declareVariable(String varName, Object value) {
    72          vars.put(varName, value);
    73      }
    74
    75      /***
    76       * Removes an existing variable. May throw UnsupportedOperationException.
    77       *
    78       * @param varName is a variable name without the "$" sign
    79       */
    80      public void undeclareVariable(String varName) {
    81          vars.remove(varName);
    82      }
    83
    84      public String toString() {
    85          return vars.toString();
    86      }
    87  }