org.sejda.conversion.EnumUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sejda-conversion Show documentation
Show all versions of sejda-conversion Show documentation
sejda core model conversion services
/*
* Created on Oct 4, 2011
* Copyright 2010 by Eduard Weissmann ([email protected]).
*
* 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 org.sejda.conversion;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.sejda.common.FriendlyNamed;
import org.sejda.model.exception.SejdaRuntimeException;
/**
* Utilities related to {@link Enum}s
*
* @author Eduard Weissmann
*
*/
public final class EnumUtils {
private EnumUtils() {
// no instances
}
/**
* @param enumClass
* @param displayName
* @return Returns the enum value matching the input displayName, belonging to the specified enum class
* Does not throw an exception if enum value is not found, returns null
*/
public static & FriendlyNamed> T valueOfSilently(Class enumClass, String displayName) {
for (T each : enumClass.getEnumConstants()) {
if (StringUtils.equalsIgnoreCase(each.getFriendlyName(), displayName)) {
return each;
}
}
return null;
}
/**
* @param enumClass
* @param displayName
* @param describedEnumClass
* @return Returns the enum value matching the input displayName, belonging to the specified enum class
* @throws SejdaRuntimeException
* if enum value is not found, mentioning the valid values in the message
*/
public static & FriendlyNamed> T valueOf(Class enumClass, String displayName,
String describedEnumClass) {
T result = valueOfSilently(enumClass, displayName);
if (result == null) {
throw new SejdaRuntimeException("Invalid value '" + displayName + "' for " + describedEnumClass
+ ". Valid values are '" + StringUtils.join(findValidValues(enumClass), "', '") + "'");
}
return result;
}
private static & FriendlyNamed> Collection findValidValues(Class enumClass) {
List result = new ArrayList();
for (FriendlyNamed each : enumClass.getEnumConstants()) {
result.add(each.getFriendlyName());
}
Collections.sort(result);
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy