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

aQute.bnd.build.model.conversions.EnumFormatter Maven / Gradle / Ivy

There is a newer version: 7.0.0
Show newest version
package aQute.bnd.build.model.conversions;

/**
 * Formats an enum type. Outputs {@code null} when the value of the enum is
 * equal to a default value.
 * 
 * @param 
 * @author Neil Bartlett
 */
public class EnumFormatter> implements Converter {

	private final E defaultValue;

	/**
	 * Construct a new formatter with no default value, i.e. any non-null value
	 * of the enum will print that value.
	 * 
	 * @param enumType The enum type.
	 */
	public static > EnumFormatter create(Class enumType) {
		return new EnumFormatter(null);
	}

	/**
	 * Construct a new formatter with the specified default value.
	 * 
	 * @param enumType The enum type.
	 * @param defaultValue The default value, which will never be output.
	 */
	public static > EnumFormatter create(Class enumType, E defaultValue) {
		return new EnumFormatter(defaultValue);
	}

	private EnumFormatter(E defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String convert(E input) throws IllegalArgumentException {
		String result;
		if (input == defaultValue || input == null)
			result = null;
		else {
			result = input.toString();
		}
		return result;
	}

	@Override
	public String error(String msg) {
		return msg;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy