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

com.laamella.sexpression.model.AtomList Maven / Gradle / Ivy

There is a newer version: 0.2
Show newest version
package com.laamella.sexpression.model;

import com.laamella.sexpression.visitor.PrinterVisitor;
import com.laamella.sexpression.visitor.Visitor;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class AtomList implements SExpression {
    public final List values = new ArrayList<>();
    public final List nodes = new ArrayList<>();

    public void add(SExpression sExpression) {
        values.add(sExpression);
    }

    public void add(CharSequence atom) {
        values.add(new Atom(atom));
    }

    @Override
    public String toString() {
        StringBuilder output = new StringBuilder();
        try {
            PrinterVisitor.TO_STRING.accept(this, output);
        } catch (Exception e) {
            return e.getMessage();
        }
        return output.toString();
    }

    @Override
    public  R visit(Visitor visitor, A arg) throws Exception {
        return visitor.accept(this, arg);
    }

    @Override
    public Otherwise whenList(Consumer action) {
        action.accept(this);
        return new Otherwise(false);
    }

    @Override
    public Otherwise whenAtom(Consumer action) {
        return new Otherwise(true);
    }

    @Override
    public Otherwise whenComment(Consumer action) {
        return new Otherwise(true);
    }

    @Override
    public boolean isAtom() {
        return false;
    }

    @Override
    public boolean isList() {
        return true;
    }

    @Override
    public boolean isComment() {
        return false;
    }


    @Override
    public Atom toAtom() {
        throw new IllegalStateException();
    }

    @Override
    public AtomList toList() {
        return this;
    }

    @Override
    public Comment toComment() {
        throw new IllegalStateException();
    }

    public boolean isAllAtoms() {
        for (SExpression e : values) {
            if (e.isList()) {
                return false;
            }
        }
        return true;
    }

    public boolean isEmpty() {
        return values.isEmpty();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy