    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.servlet;
    019
    020import org.apache.commons.jxpath.Variables;
    021
    022/**
    023 * Implementation of the Variables interface that provides access to a single object using a reserved name (keyword).
    024 */
    025public class KeywordVariables implements Variables {
    026
    027    private static final long serialVersionUID = 894145608741325442L;
    028
    029    /**
    030     * The keyword.
    031     */
    032    private final String keyword;
    033
    034    /**
    035     * The value.
    036     */
    037    private final Object value;
    038
    039    /**
    040     * Constructs a new KeywordVariables.
    041     *
    042     * @param keyword The keyword.
    043     * @param value  The value.
    044     */
    045    public KeywordVariables(final String keyword, final Object value) {
    046        if (keyword == null) {
    047            throw new IllegalArgumentException("keyword cannot be null");
    048        }
    049        this.keyword = keyword;
    050        this.value = value;
    051    }
    052
    053    @Override
    054    public void declareVariable(final String variable, final Object value) {
    055        throw new UnsupportedOperationException("Cannot declare new keyword variables.");
    056    }
    057
    058    @Override
    059    public Object getVariable(final String variable) {
    060        return isDeclaredVariable(variable) ? value : null;
    061    }
    062
    063    @Override
    064    public boolean isDeclaredVariable(final String variable) {
    065        return variable.equals(keyword);
    066    }
    067
    068    @Override
    069    public void undeclareVariable(final String variable) {
    070        throw new UnsupportedOperationException("Cannot undeclare keyword variables.");
    071    }
    072}