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
The newest version!
/*
* Created on Oct 4, 2011
* Copyright 2010 by Eduard Weissmann ([email protected]).
*
* This file is part of the Sejda source code
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
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.model.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;
}
}