001package io.jstach.jstachio.formatters; 002 003import java.net.URI; 004import java.net.URL; 005 006import org.eclipse.jdt.annotation.Nullable; 007 008import io.jstach.jstache.JStacheFormatter; 009import io.jstach.jstache.JStacheFormatterTypes; 010import io.jstach.jstachio.Appender; 011import io.jstach.jstachio.Formatter; 012import io.jstach.jstachio.Output; 013 014/** 015 * Default formatters. 016 * 017 * Unlike the mustache spec it will throw a NPE trying to format null objects. 018 * 019 * @author agentgt 020 */ 021@JStacheFormatter 022@JStacheFormatterTypes(types = { URI.class, URL.class }) 023public interface DefaultFormatter extends Formatter { 024 025 /** 026 * {@inheritDoc} Will throw an NPE if parameter o is <code>null</code>. 027 */ 028 @Override 029 default <A extends Output<E>, E extends Exception> void format(Appender downstream, A a, String path, Class<?> c, 030 @Nullable Object o) throws E { 031 if (o == null) { 032 throw new NullPointerException("null at: '" + path + "'"); 033 } 034 else { 035 downstream.append(a, String.valueOf(o)); 036 } 037 } 038 039 /** 040 * {@inheritDoc} Will throw an NPE if parameter s is <code>null</code>. 041 */ 042 @Override 043 default <A extends Output<E>, E extends Exception> void format(Appender downstream, A a, String path, 044 @Nullable String s) throws E { 045 if (s == null) { 046 throw new NullPointerException("null at: '" + path + "'"); 047 } 048 downstream.append(a, s); 049 } 050 051 /** 052 * Provides the default formatter for static lookup. 053 * @return the default formatter singleton 054 */ 055 public static Formatter provider() { 056 return DefaultFormatterSingleton.DEFAULT_FORMATTER; 057 } 058 059 /** 060 * Provides the default formatter for static lookup. 061 * @return the default formatter singleton 062 */ 063 public static Formatter of() { 064 return DefaultFormatterSingleton.DEFAULT_FORMATTER; 065 } 066 067} 068 069enum DefaultFormatterSingleton implements DefaultFormatter { 070 071 DEFAULT_FORMATTER; 072 073}