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.ri.compiler;
018
019import org.apache.commons.jxpath.ri.EvalContext;
020import org.apache.commons.jxpath.ri.InfoSetUtil;
021
022/**
023 * A compile tree element containing a constant number or string.
024 *
025 * @author Dmitri Plotnikov
026 * @version $Revision: 652845 $ $Date: 2008-05-02 19:46:46 +0200 (Fr, 02 Mai 2008) $
027 */
028public class Constant extends Expression {
029
030    private Object value;
031
032    /**
033     * Create a new Constant.
034     * @param number constant
035     */
036    public Constant(Number number) {
037        this.value = number;
038    }
039
040    /**
041     * Create a new Constant.
042     * @param string constant
043     */
044    public Constant(String string) {
045        this.value = string;
046    }
047
048    public Object compute(EvalContext context) {
049        return value;
050    }
051
052    public Object computeValue(EvalContext context) {
053        return value;
054    }
055
056    /**
057     * Returns false
058     * @return false
059     */
060    public boolean isContextDependent() {
061        return false;
062    }
063
064    /**
065     * Returns false
066     * @return false
067     */
068    public boolean computeContextDependent() {
069        return false;
070    }
071
072    public String toString() {
073        if (value instanceof Number) {
074            return InfoSetUtil.stringValue(value);
075        }
076        return "'" + value + "'";
077    }
078}