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 */
017
018package org.apache.commons.jxpath;
019
020import java.util.HashMap;
021
022/**
023 * A basic implementation of the Variables interface that uses a HashMap.
024 */
025public class BasicVariables implements Variables {
026
027    private static final long serialVersionUID = 2708263960832062725L;
028
029    /**
030     * Contains the values of declared variables
031     */
032    private final HashMap vars = new HashMap();
033
034    /**
035     * Constructs a new instance.
036     */
037    public BasicVariables() {
038        // empty
039    }
040
041    /**
042     * Defines a new variable with the specified value or modifies the value of an existing variable.
043     *
044     * @param varName is a variable name without the "$" sign
045     * @param value   is the new value for the variable, which can be null
046     */
047    @Override
048    public void declareVariable(final String varName, final Object value) {
049        vars.put(varName, value);
050    }
051
052    /**
053     * Returns the value of the variable if it is defined, otherwise, throws IllegalArgumentException
054     *
055     * @param varName is a variable name without the "$" sign
056     * @return the value of the variable
057     */
058    @Override
059    public Object getVariable(final String varName) {
060        // Note that a variable may be defined with a null value
061        if (vars.containsKey(varName)) {
062            return vars.get(varName);
063        }
064        throw new IllegalArgumentException("No such variable: '" + varName + "'");
065    }
066
067    /**
068     * Returns true if the variable has been defined, even if the value of the variable is null.
069     *
070     * @param varName is a variable name without the "$" sign
071     * @return true if the variable is declared
072     */
073    @Override
074    public boolean isDeclaredVariable(final String varName) {
075        return vars.containsKey(varName);
076    }
077
078    @Override
079    public String toString() {
080        return vars.toString();
081    }
082
083    /**
084     * Removes an existing variable. May throw UnsupportedOperationException.
085     *
086     * @param varName is a variable name without the "$" sign
087     */
088    @Override
089    public void undeclareVariable(final String varName) {
090        vars.remove(varName);
091    }
092}