package com.github.ewanld.visitor;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Represents an object that can be visited by a Visitor.
 */
public interface Visitable<T> {
	/**
	 * Traverse this object structure (depth-first).<br>
	 * This method should not be overriden.
	 */
	default VisitResult accept(T visitor) {
		final VisitResult result = visitorEnter(visitor);
		if (result == VisitResult.ABORT) return VisitResult.ABORT;

		if (result != VisitResult.SKIP_CHILDREN) {
			final Iterator<Visitable<T>> it = getVisitableChildren();
			while (it.hasNext()) {
				final Visitable<T> child = it.next();
				final VisitResult childResult = child.accept(visitor);
				if (childResult == VisitResult.ABORT) return VisitResult.ABORT;
				if (childResult == VisitResult.SKIP_SIBLINGS) break;
			}
		}
		visitorLeave(visitor);
		return result;
	}

	/**
	 * Traverse this object structure (breadth-first).<br>
	 * This method should not be overriden.
	 */
	default void accept_breadthFirst(T visitor) {
		final Queue<Visitable<T>> queue = new LinkedList<Visitable<T>>();
		queue.add(this);
		while (!queue.isEmpty()) {
			final Visitable<T> node = queue.remove();
			final VisitResult result = node.visitorEnter(visitor);
			if (result == VisitResult.ABORT) return;
			if (result == VisitResult.SKIP_SIBLINGS) queue.clear();
			if (result != VisitResult.SKIP_CHILDREN) node.getVisitableChildren().forEachRemaining(queue::add);
			node.visitorLeave(visitor);
		}
	}

	/**
	 * Return the child nodes of this object. The default implementation has no child nodes.
	 */
	default Iterator<Visitable<T>> getVisitableChildren() {
		return Collections.emptyIterator();
	}

	/**
	 * The implementation should always be the same:<br>
	 * {@code return visitor.enter(this);}
	 */
	VisitResult visitorEnter(T visitor);

	/**
	 * The implementation should always be the same:<br>
	 * {@code visitor.leave(this);}
	 */
	void visitorLeave(T visitor);

}

@@ -211,16 +211,69 @@
 sitor.%0D%0A
+ * %0D%0A * @param %3CT%3E%0D%0A *            The Visitor type.%0D%0A
  */%0D%0Apub
@@ -1986,32 +1986,62 @@
 r);%0D%0A%0D%0A%09/**%0D%0A%09 *
+ Required for double dispatch.
  The implementat

add javadoc