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

se.l4.lect.internal.tokens.BranchNode Maven / Gradle / Ivy

package se.l4.lect.internal.tokens;

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

import se.l4.lect.tokens.Token;

/**
 * Node that branches out and allows matches to be done in several branches.
 *
 * @author Andreas Holstenson
 *
 */
public class BranchNode
	extends MatcherNode
{
	private final MatcherNode[] branches;

	public BranchNode(List branches)
	{
		this.branches = branches.toArray(new MatcherNode[branches.size()]);
	}

	@Override
	public void setNext(MatcherNode next)
	{
		for(MatcherNode node : branches)
		{
			node.getTail().setNext(next);
		}
	}

	@Override
	boolean match(Token token, Consumer branchReceiver)
	{
		boolean matched = true;
		for(MatcherNode node : branches)
		{
			if(! node.match(token, branchReceiver))
			{
				matched = false;
			}
		}

		return matched;
	}

	@Override
	public String toString()
	{
		StringBuilder b = new StringBuilder();
		b.append('[');

		for(MatcherNode node : branches)
		{
			if(b.length() > 1) b.append(' ');

			b.append(node.toString());
			node = node.next;
		}

		b.append(']');
		return b.toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy