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.jexl2.parser;
018
019import java.util.Collections;
020import java.util.Map;
021
022public final class ASTMapLiteral extends JexlNode implements JexlNode.Literal<Object> {
023    /** The type literal value. */
024    Map<?,?> map = null;
025    /** Whether this array is constant or not. */
026    boolean constant = false;
027
028    ASTMapLiteral(int id) {
029        super(id);
030    }
031
032    ASTMapLiteral(Parser p, int id) {
033        super(p, id);
034    }
035
036
037    /** {@inheritDoc} */
038    @Override
039    public void jjtClose() {
040        if (children == null || children.length == 0) {
041            map = Collections.EMPTY_MAP;
042            constant = true;
043        } else {
044            constant = isConstant();
045        }
046    }
047
048    /**
049     *  Gets the literal value.
050     * @return the array literal
051     */
052    public Object getLiteral() {
053        return map;
054    }
055
056    /**
057     * Sets the literal value only if the descendants of this node compose a constant
058     * @param literal the literal array value
059     * @throws IllegalArgumentException if literal is not an array or null
060     */
061    public void setLiteral(Object literal) {
062        if (constant) {
063            if (!(literal instanceof Map<?,?>)) {
064                throw new IllegalArgumentException(literal.getClass() + " is not an array");
065            }
066            this.map = (Map<?,?>) literal;
067        }
068    }
069
070    /** {@inheritDoc} */
071    @Override
072    public Object jjtAccept(ParserVisitor visitor, Object data) {
073        return visitor.visit(this, data);
074    }
075}