org.conqat.lib.commons.enums.EnumUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-lib-commons Show documentation
Show all versions of teamscale-lib-commons Show documentation
Provides common utility functions
/*
* Copyright (c) CQSE GmbH
*
* 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.conqat.lib.commons.enums;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Set;
import org.conqat.lib.commons.assertion.CCSMAssert;
import org.conqat.lib.commons.collections.CollectionUtils;
/**
* Utility class for enumerations.
*/
public class EnumUtils {
/**
* This works like {@link Enum#valueOf(java.lang.Class, java.lang.String)} but returns
* null
if constant wasn't found instead of throwing an
* IllegalArgumentException
.
*
* @param enumType
* Enumeration class
* @param constantName
* name of the constant
* @return the matching constant or null
if not found
*/
public static > T valueOf(Class enumType, String constantName) {
try {
T constant = Enum.valueOf(enumType, constantName);
return constant;
} catch (IllegalArgumentException ex) {
return null;
}
}
/**
* Returns an {@link EnumSet} by converting the given constants given as Strings to enums of the
* specified type.
*/
public static > EnumSet valuesOf(Class enumType, Collection constantNames) {
EnumSet result = EnumSet.noneOf(enumType);
for (String constant : constantNames) {
result.add(Enum.valueOf(enumType, constant));
}
return result;
}
/**
* Works like {@link #valueOf(Class, String)} but ignores case.
*
* Worst case runtime is O('number of constants in enum').
*/
public static > T valueOfIgnoreCase(Class enumType, String constantName) {
T[] constants = enumType.getEnumConstants();
CCSMAssert.isNotNull(constants);
for (T constant : constants) {
if (constant.name().equalsIgnoreCase(constantName)) {
return constant;
}
}
return null;
}
/**
* Returns an array containing the names of the enum element. Ordering is same as element ordering
* in enum.
*/
public static > String[] names(Class enumType) {
T[] constants = enumType.getEnumConstants();
CCSMAssert.isNotNull(constants);
String[] result = new String[constants.length];
for (int i = 0; i < constants.length; i++) {
result[i] = constants[i].name();
}
return result;
}
/**
* Merges an EnumSet and a list of enum values into one EnumSet.
*/
@SafeVarargs
public static > EnumSet mergeSets(Set set, T... values) {
EnumSet newSet = EnumSet.copyOf(set);
newSet.addAll(Arrays.asList(values));
return newSet;
}
/**
* Returns a new EnumSet that excludes the given enum values.
*/
@SafeVarargs
public static > EnumSet copyOfExcept(Set set, T... values) {
EnumSet newSet = EnumSet.copyOf(set);
CollectionUtils.removeAll(newSet, Arrays.asList(values));
return newSet;
}
}