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

yakworks.commons.lang.EnumUtils Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 yakworks.commons.lang;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 

Utility library to provide helper methods for Java enums.

* *

#ThreadSafe#

* * @since 3.0 */ public class EnumUtils { private static final String NULL_ELEMENTS_NOT_PERMITTED = "null elements not permitted"; private static final String CANNOT_STORE_S_S_VALUES_IN_S_BITS = "Cannot store %s %s values in %s bits"; private static final String S_DOES_NOT_SEEM_TO_BE_AN_ENUM_TYPE = "%s does not seem to be an Enum type"; private static final String ENUM_CLASS_MUST_BE_DEFINED = "EnumClass must be defined."; /** * Validate {@code enumClass}. * @param the type of the enumeration * @param enumClass to check * @return {@code enumClass} * @throws NullPointerException if {@code enumClass} is {@code null} * @throws IllegalArgumentException if {@code enumClass} is not an enum class * @since 3.2 */ private static > Class asEnum(final Class enumClass) { Validate.notNull(enumClass, ENUM_CLASS_MUST_BE_DEFINED); Validate.isTrue(enumClass.isEnum(), S_DOES_NOT_SEEM_TO_BE_AN_ENUM_TYPE, enumClass); return enumClass; } /** *

Gets the enum for the class, returning {@code null} if not found.

* *

This method differs from {@link Enum#valueOf} in that it does not throw an exception * for an invalid enum name.

* * @param the type of the enumeration * @param enumClass the class of the enum to query, not null * @param enumName the enum name, null returns null * @return the enum, null if not found */ public static > E getEnum(final Class enumClass, final String enumName) { return getEnum(enumClass, enumName, null); } /** *

Gets the enum for the class, returning {@code defaultEnum} if not found.

* *

This method differs from {@link Enum#valueOf} in that it does not throw an exception * for an invalid enum name.

* * @param the type of the enumeration * @param enumClass the class of the enum to query, not null * @param enumName the enum name, null returns default enum * @param defaultEnum the default enum * @return the enum, default enum if not found * @since 3.10 */ public static > E getEnum(final Class enumClass, final String enumName, final E defaultEnum) { if (enumName == null) { return defaultEnum; } try { return Enum.valueOf(enumClass, enumName); } catch (final IllegalArgumentException ex) { return defaultEnum; } } /** *

Gets the enum for the class, returning {@code null} if not found.

* *

This method differs from {@link Enum#valueOf} in that it does not throw an exception * for an invalid enum name and performs case insensitive matching of the name.

* * @param the type of the enumeration * @param enumClass the class of the enum to query, not null * @param enumName the enum name, null returns null * @return the enum, null if not found * @since 3.8 */ public static > E getEnumIgnoreCase(final Class enumClass, final String enumName) { return getEnumIgnoreCase(enumClass, enumName, null); } /** *

Gets the enum for the class, returning {@code defaultEnum} if not found.

* *

This method differs from {@link Enum#valueOf} in that it does not throw an exception * for an invalid enum name and performs case insensitive matching of the name.

* * @param the type of the enumeration * @param enumClass the class of the enum to query, not null * @param enumName the enum name, null returns default enum * @param defaultEnum the default enum * @return the enum, default enum if not found * @since 3.10 */ public static > E getEnumIgnoreCase(final Class enumClass, final String enumName, final E defaultEnum) { if (enumName == null || !enumClass.isEnum()) { return defaultEnum; } for (final E each : enumClass.getEnumConstants()) { if (each.name().equalsIgnoreCase(enumName)) { return each; } } return defaultEnum; } /** *

Gets the {@code List} of enums.

* *

This method is useful when you need a list of enums rather than an array.

* * @param the type of the enumeration * @param enumClass the class of the enum to query, not null * @return the modifiable list of enums, never null */ public static > List getEnumList(final Class enumClass) { return new ArrayList<>(Arrays.asList(enumClass.getEnumConstants())); } /** *

Gets the {@code Map} of enums by name.

* *

This method is useful when you need a map of enums by name.

* * @param the type of the enumeration * @param enumClass the class of the enum to query, not null * @return the modifiable map of enum names to enums, never null */ public static > Map getEnumMap(final Class enumClass) { final Map map = new LinkedHashMap<>(); for (final E e: enumClass.getEnumConstants()) { map.put(e.name(), e); } return map; } /** *

Checks if the specified name is a valid enum for the class.

* *

This method differs from {@link Enum#valueOf} in that checks if the name is * a valid enum without needing to catch the exception.

* * @param the type of the enumeration * @param enumClass the class of the enum to query, not null * @param enumName the enum name, null returns false * @return true if the enum name is valid, otherwise false */ public static > boolean isValidEnum(final Class enumClass, final String enumName) { return getEnum(enumClass, enumName) != null; } /** *

Checks if the specified name is a valid enum for the class.

* *

This method differs from {@link Enum#valueOf} in that checks if the name is * a valid enum without needing to catch the exception * and performs case insensitive matching of the name.

* * @param the type of the enumeration * @param enumClass the class of the enum to query, not null * @param enumName the enum name, null returns false * @return true if the enum name is valid, otherwise false */ public static > boolean isValidEnumIgnoreCase(final Class enumClass, final String enumName) { return getEnumIgnoreCase(enumClass, enumName) != null; } /** * This constructor is public to permit tools that require a JavaBean * instance to operate. */ public EnumUtils() { } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy