001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.jxpath;
018
019import java.util.HashMap;
020
021/**
022 * A basic implementation of the Variables interface that uses a HashMap.
023 *
024 * @author Dmitri Plotnikov
025 * @version $Revision: 652925 $ $Date: 2008-05-03 00:05:41 +0200 (Sa, 03 Mai 2008) $
026 */
027public class BasicVariables implements Variables {
028    private static final long serialVersionUID = 2708263960832062725L;
029
030    /**
031     * Contains the values of declared variables
032     */
033    private HashMap vars = new HashMap();
034
035    /**
036     * Returns true if the variable has been defined, even if the
037     * value of the variable is null.
038     *
039     * @param varName is a variable name without the "$" sign
040     *
041     * @return true if the variable is declared
042     */
043    public boolean isDeclaredVariable(String varName) {
044        return vars.containsKey(varName);
045    }
046
047    /**
048     * Returns the value of the variable if it is defined,
049     * otherwise, throws IllegalArgumentException
050     *
051     * @param varName is a variable name without the "$" sign
052     *
053     * @return the value of the variable
054     */
055    public Object getVariable(String varName) {
056        // Note that a variable may be defined with a null value
057
058        if (vars.containsKey(varName)) {
059            return vars.get(varName);
060        }
061
062        throw new IllegalArgumentException(
063            "No such variable: '" + varName + "'");
064    }
065
066    /**
067     * Defines a new variable with the specified value or modifies
068     * the value of an existing variable.
069     *
070     * @param varName is a variable name without the "$" sign
071     * @param value is the new value for the variable, which can be null
072     */
073    public void declareVariable(String varName, Object value) {
074        vars.put(varName, value);
075    }
076
077    /**
078     * Removes an existing variable. May throw UnsupportedOperationException.
079     *
080     * @param varName is a variable name without the "$" sign
081     */
082    public void undeclareVariable(String varName) {
083        vars.remove(varName);
084    }
085
086    public String toString() {
087        return vars.toString();
088    }
089}