001package io.jstach.jstachio;
002
003import java.io.IOException;
004
005/**
006 * Renders models of type {@code T} by writing to an Appendable. <em>Implementations
007 * should be generally stateless and threadsafe.</em>
008 *
009 * @param <T> the model type
010 */
011public interface Renderer<T> {
012
013        /**
014         * Renders the passed in model.
015         * @param <A> output type
016         * @param <E> error type
017         * @param model a model assumed never to be <code>null</code>.
018         * @param appendable the output to write to.
019         * @return the output passed in returned for convenience.
020         * @throws E if there is an error writing to the output
021         */
022        public <A extends Output<E>, E extends Exception> A execute(T model, A appendable) throws E;
023
024        /**
025         * Renders the passed in model.
026         * @param model a model assumed never to be <code>null</code>.
027         * @param appendable the appendable to write to.
028         * @throws IOException if there is an error writing to the appendable
029         */
030        default void execute(T model, Appendable appendable) throws IOException {
031                execute(model, Output.of(appendable));
032        }
033
034        /**
035         * A convenience method that does not throw {@link IOException} when using
036         * StringBuilder.
037         * @param model a model assumed never to be null.
038         * @param sb should never be null.
039         * @return the passed in {@link StringBuilder}.
040         */
041        default StringBuilder execute(T model, StringBuilder sb) {
042                return execute(model, Output.of(sb)).getBuffer();
043        }
044
045        /**
046         * Convenience method that directly renders the model as a String.
047         * @param model never null.
048         * @return the rendered model.
049         */
050        default String execute(T model) {
051                StringBuilder sb = new StringBuilder();
052                return execute(model, sb).toString();
053        }
054
055}