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

co.streamx.fluent.extree.expression.NewArrayInitExpression Maven / Gradle / Ivy

The newest version!
package co.streamx.fluent.extree.expression;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import lombok.Getter;
import lombok.NonNull;

/**
 * Describes a one-dimensional array and initializing it from a list of elements.
 * 
 * 
 */

@Getter
public final class NewArrayInitExpression extends Expression {

    private final Class componentType;
    private final List initializers;

    NewArrayInitExpression(@NonNull Class type, int count) {
        this(type, Arrays.asList(new Expression[count]));
    }

    NewArrayInitExpression(@NonNull Class type, @NonNull List initializers) {
        super(ExpressionType.NewArrayInit, Array.newInstance(type, 0).getClass());
        this.componentType = type;
        this.initializers = initializers;
    }

    public int getCount() {
        return initializers.size();
    }

    @Override
    protected  T visit(ExpressionVisitor v) {
        return v.visit(this);
    }

    @Override
    public String toString() {
        StringBuilder b = new StringBuilder();
        b.append('{');
        b.append(initializers.stream().map(Expression::toString).collect(Collectors.joining(",")));
        b.append('}');
        return b.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy