All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.qbicc.graph.literal.ArrayLiteral Maven / Gradle / Ivy

There is a newer version: 0.77.0
Show newest version
package org.qbicc.graph.literal;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import org.qbicc.graph.Value;
import org.qbicc.type.ArrayType;

/**
 * A literal array.  This is not a Java array object literal (use {@code ObjectLiteral}).
 */
public final class ArrayLiteral extends Literal {
    private final List values;
    private final ArrayType type;
    private final int hashCode;

    ArrayLiteral(final ArrayType type, final List values) {
        this.values = values;
        this.type = type;
        hashCode = Objects.hash(type, values);
    }

    public List getValues() {
        return values;
    }

    public ArrayType getType() {
        return type;
    }

    public  R accept(final LiteralVisitor visitor, final T param) {
        return visitor.visit(param, this);
    }

    @Override
    public Value extractElement(LiteralFactory lf, Value index) {
        if (index instanceof IntegerLiteral il) {
            final int realIndex = il.intValue();
            if (0 <= realIndex && realIndex < values.size()) {
                return values.get(realIndex);
            }
        }
        return null;
    }

    public boolean isZero() {
        return false;
    }

    public boolean equals(final Literal other) {
        return other instanceof ArrayLiteral && equals((ArrayLiteral) other);
    }

    public boolean equals(final ArrayLiteral other) {
        return this == other || other != null && hashCode == other.hashCode && values.equals(other.values) && type.equals(other.type);
    }

    public int hashCode() {
        return hashCode;
    }

    public StringBuilder toString(StringBuilder target) {
        target.append('[');
        Iterator iterator = values.iterator();
        if (iterator.hasNext()) {
            target.append(iterator.next());
            while (iterator.hasNext()) {
                target.append(',');
                target.append(iterator.next());
            }
        }
        target.append(']');
        return target;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy