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