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