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

com.tenxerconsulting.swagger.doclet.translator.FirstNotNullTranslator Maven / Gradle / Ivy

The newest version!
package com.tenxerconsulting.swagger.doclet.translator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.common.base.Function;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.FieldDoc;
import com.sun.javadoc.MethodDoc;
import com.sun.javadoc.Parameter;
import com.sun.javadoc.Type;

/**
 * The FirstNotNullTranslator represents a chain of translators.
 * It will iterate through each one in the chain and return the results of the first translator
 * that does not return null.
 * @version $Id$
 */
public class FirstNotNullTranslator implements Translator {

	private final List chain;

	/**
	 * This creates a FirstNotNullTranslator with no links
	 */
	public FirstNotNullTranslator() {
		this.chain = new ArrayList();
	}

	/**
	 * This adds another translator link to the chain of translators
	 * @param link The translator to add
	 * @return this
	 */
	public FirstNotNullTranslator addNext(Translator link) {
		this.chain.add(link);
		return this;
	}

	/**
	 * {@inheritDoc}
	 * @see com.tenxerconsulting.swagger.doclet.translator.Translator#typeName(com.sun.javadoc.Type, boolean, com.sun.javadoc.ClassDoc[])
	 */
	public OptionalName typeName(Type type, boolean useFqn, ClassDoc[] views) {
		return firstNotNullOf(new Function() {

			public OptionalName apply(Translator translator) {
				return translator.typeName(type, useFqn, views);
			}
		});
	}

	/**
	 * {@inheritDoc}
	 * @see com.tenxerconsulting.swagger.doclet.translator.Translator#parameterTypeName(boolean, com.sun.javadoc.Parameter, com.sun.javadoc.Type, boolean,
	 *      com.sun.javadoc.ClassDoc[])
	 */
	public OptionalName parameterTypeName(boolean multipart, Parameter parameter, Type paramType, boolean useFqn, ClassDoc[] views) {
		return firstNotNullOf(new Function() {

			public OptionalName apply(Translator translator) {
				return translator.parameterTypeName(multipart, parameter, paramType, useFqn, views);
			}
		});
	}

	/**
	 * {@inheritDoc}
	 * @see com.tenxerconsulting.swagger.doclet.translator.Translator#typeName(com.sun.javadoc.Type, boolean)
	 */
	public OptionalName typeName(Type type, boolean useFqn) {
		return firstNotNullOf(new Function() {

			public OptionalName apply(Translator translator) {
				return translator.typeName(type, useFqn);
			}
		});
	}

	/**
	 * {@inheritDoc}
	 * @see com.tenxerconsulting.swagger.doclet.translator.Translator#fieldName(com.sun.javadoc.FieldDoc)
	 */
	public OptionalName fieldName(final FieldDoc field) {
		return firstNotNullOf(new Function() {

			public OptionalName apply(Translator translator) {
				return translator.fieldName(field);
			}
		});
	}

	/**
	 * {@inheritDoc}
	 * @see com.tenxerconsulting.swagger.doclet.translator.Translator#methodName(com.sun.javadoc.MethodDoc)
	 */
	public OptionalName methodName(final MethodDoc method) {
		return firstNotNullOf(new Function() {

			public OptionalName apply(Translator translator) {
				return translator.methodName(method);
			}
		});
	}

	private OptionalName firstNotNullOf(Function function) {
		OptionalName name = null;
		Iterator iterator = this.chain.iterator();
		while ((name == null || name.isMissing()) && iterator.hasNext()) {
			name = function.apply(iterator.next());
		}
		return name;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy