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

nl.nn.adapterframework.util.EnumUtils Maven / Gradle / Ivy

/*
   Copyright 2021 WeAreFrank!

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package nl.nn.adapterframework.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

import nl.nn.adapterframework.doc.DocumentedEnum;

/**
 * @author Niels Meijer
 */
public abstract class EnumUtils {

	public static > E parse(Class enumClass, String value) {
		return parse(enumClass, value, false);
	}

	public static > E parse(Class enumClass, String value, boolean fallbackToStandardEnumParsing) {
		return parse(enumClass, getFieldName(enumClass), value, fallbackToStandardEnumParsing);
	}

	private static String getFieldName(Class enumClass) {
		String className = org.springframework.util.ClassUtils.getUserClass(enumClass).getSimpleName();
		char[] c = className.toCharArray();
		c[0] = Character.toLowerCase(c[0]);
		return new String(c);
	}

	public static > E parse(Class enumClass, String fieldName, String value) {
		return parse(enumClass, fieldName, value, false);
	}

	public static > E parse(Class enumClass, String fieldName, String value, boolean fallbackToStandardEnumParsing) {
		if(DocumentedEnum.class.isAssignableFrom(enumClass)) {
			try {
				return parseDocumented(enumClass, fieldName, value);
			} catch (IllegalArgumentException e1) {
				if(fallbackToStandardEnumParsing) {
					try {
						return parseNormal(enumClass, fieldName, value);
					} catch (IllegalArgumentException e2) {
						e1.addSuppressed(e2);
						throw e1;
					}
				} else {
					throw e1;
				}
			}
		}
		return parseNormal(enumClass, fieldName, value);
	}

	protected static > E parseNormal(Class enumClass, String fieldName, String value) {
		E result = parseIgnoreCase(enumClass, value);
		if (result==null) {
			throw new IllegalArgumentException((fieldName!=null?"cannot set field ["+fieldName+"] to ":"")+"unparsable value ["+value+"]. Must be one of "+ getEnumList(enumClass));
		}
		return result;
	}

	/**
	 * Solely for DocumentedEnums !
	 */
	protected static > E parseDocumented(Class enumClass, String fieldName, String value) {
		return parseFromField(enumClass, fieldName, value, e -> ((DocumentedEnum)e).getLabel());
	}

	public static > E parseFromField(Class enumClass, String fieldName, String value, Function field) {
		List fieldValues = new ArrayList<>();
		for (E e: getEnumList(enumClass)) {
			String fieldValue = field.apply(e);
			if (fieldValue.equalsIgnoreCase(value)) {
				return e;
			}
			fieldValues.add(fieldValue);
		}
		throw new IllegalArgumentException((fieldName!=null?"cannot set field ["+fieldName+"] to ":"")+"unparsable value ["+value+"]. Must be one of "+ fieldValues);
	}

	public static > E parseFromField(Class enumClass, String fieldName, int value, Function field) {
		List fieldValues = new ArrayList<>();
		for (E e: getEnumList(enumClass)) {
			int fieldValue = field.apply(e);
			if (fieldValue==value) {
				return e;
			}
			fieldValues.add(fieldValue);
		}
		throw new IllegalArgumentException((fieldName!=null?"cannot set field ["+fieldName+"] to ":"")+"unparsable value ["+value+"]. Must be one of "+ fieldValues);
	}

	public static > List getEnumList(final Class enumClass) {
		return new ArrayList<>(Arrays.asList(enumClass.getEnumConstants()));
	}

	private static > E parseIgnoreCase(final Class enumClass, final String enumName) {
		if (enumName == null) {
			return null;
		}
		for (final E each : enumClass.getEnumConstants()) {
			if (each.name().equalsIgnoreCase(enumName)) {
				return each;
			}
		}
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy