From 619a7024c4a808fbd3cde9d8545959d77a94717b Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Mon, 28 Oct 2019 14:30:14 -0400 Subject: [PATCH] Sort methods. --- .../org/apache/commons/lang3/EnumUtils.java | 328 +++++++++--------- 1 file changed, 164 insertions(+), 164 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/EnumUtils.java b/src/main/java/org/apache/commons/lang3/EnumUtils.java index 88b2c3c53..75b052a9f 100644 --- a/src/main/java/org/apache/commons/lang3/EnumUtils.java +++ b/src/main/java/org/apache/commons/lang3/EnumUtils.java @@ -39,72 +39,149 @@ public class EnumUtils { private static final String ENUM_CLASS_MUST_BE_DEFINED = "EnumClass must be defined."; /** - * This constructor is public to permit tools that require a JavaBean - * instance to operate. + * 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 */ - public EnumUtils() { + 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 {@code Map} of enums by name.

- * - *

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

- * + * Validate that {@code enumClass} is compatible with representation in a {@code long}. * @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 + * @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 or has more than 64 values + * @since 3.0.1 */ - public static > Map getEnumMap(final Class enumClass) { - final Map map = new LinkedHashMap<>(); - for (final E e: enumClass.getEnumConstants()) { - map.put(e.name(), e); + private static > Class checkBitVectorable(final Class enumClass) { + final E[] constants = asEnum(enumClass).getEnumConstants(); + Validate.isTrue(constants.length <= Long.SIZE, CANNOT_STORE_S_S_VALUES_IN_S_BITS, + Integer.valueOf(constants.length), enumClass.getSimpleName(), Integer.valueOf(Long.SIZE)); + + return enumClass; + } + + /** + *

Creates a long bit vector representation of the given array of Enum values.

+ * + *

This generates a value that is usable by {@link EnumUtils#processBitVector}.

+ * + *

Do not use this method if you have more than 64 values in your Enum, as this + * would create a value greater than a long can hold.

+ * + * @param enumClass the class of the enum we are working with, not {@code null} + * @param values the values we want to convert, not {@code null} + * @param the type of the enumeration + * @return a long whose value provides a binary representation of the given set of enum values. + * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null} + * @throws IllegalArgumentException if {@code enumClass} is not an enum class or has more than 64 values + * @since 3.0.1 + * @see #generateBitVectors(Class, Iterable) + */ + @SafeVarargs + public static > long generateBitVector(final Class enumClass, final E... values) { + Validate.noNullElements(values); + return generateBitVector(enumClass, Arrays.asList(values)); + } + + /** + *

Creates a long bit vector representation of the given subset of an Enum.

+ * + *

This generates a value that is usable by {@link EnumUtils#processBitVector}.

+ * + *

Do not use this method if you have more than 64 values in your Enum, as this + * would create a value greater than a long can hold.

+ * + * @param enumClass the class of the enum we are working with, not {@code null} + * @param values the values we want to convert, not {@code null}, neither containing {@code null} + * @param the type of the enumeration + * @return a long whose value provides a binary representation of the given set of enum values. + * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null} + * @throws IllegalArgumentException if {@code enumClass} is not an enum class or has more than 64 values, + * or if any {@code values} {@code null} + * @since 3.0.1 + * @see #generateBitVectors(Class, Iterable) + */ + public static > long generateBitVector(final Class enumClass, final Iterable values) { + checkBitVectorable(enumClass); + Validate.notNull(values); + long total = 0; + for (final E constant : values) { + Validate.isTrue(constant != null, NULL_ELEMENTS_NOT_PERMITTED); + total |= 1L << constant.ordinal(); } - return map; + return total; } /** - *

Gets the {@code List} of enums.

+ *

Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed.

* - *

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

+ *

This generates a value that is usable by {@link EnumUtils#processBitVectors}.

* - * @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 + *

Use this method if you have more than 64 values in your Enum.

+ * + * @param enumClass the class of the enum we are working with, not {@code null} + * @param values the values we want to convert, not {@code null}, neither containing {@code null} + * @param the type of the enumeration + * @return a long[] whose values provide a binary representation of the given set of enum values + * with least significant digits rightmost. + * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null} + * @throws IllegalArgumentException if {@code enumClass} is not an enum class, or if any {@code values} {@code null} + * @since 3.2 */ - public static > List getEnumList(final Class enumClass) { - return new ArrayList<>(Arrays.asList(enumClass.getEnumConstants())); + @SafeVarargs + public static > long[] generateBitVectors(final Class enumClass, final E... values) { + asEnum(enumClass); + Validate.noNullElements(values); + final EnumSet condensed = EnumSet.noneOf(enumClass); + Collections.addAll(condensed, values); + final long[] result = new long[(enumClass.getEnumConstants().length - 1) / Long.SIZE + 1]; + for (final E value : condensed) { + result[value.ordinal() / Long.SIZE] |= 1L << (value.ordinal() % Long.SIZE); + } + ArrayUtils.reverse(result); + return result; } /** - *

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

+ *

Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed.

* - *

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

+ *

This generates a value that is usable by {@link EnumUtils#processBitVectors}.

* - * @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 + *

Use this method if you have more than 64 values in your Enum.

+ * + * @param enumClass the class of the enum we are working with, not {@code null} + * @param values the values we want to convert, not {@code null}, neither containing {@code null} + * @param the type of the enumeration + * @return a long[] whose values provide a binary representation of the given set of enum values + * with least significant digits rightmost. + * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null} + * @throws IllegalArgumentException if {@code enumClass} is not an enum class, or if any {@code values} {@code null} + * @since 3.2 */ - 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 - * @since 3.8 - */ - public static > boolean isValidEnumIgnoreCase(final Class enumClass, final String enumName) { - return getEnumIgnoreCase(enumClass, enumName) != null; + public static > long[] generateBitVectors(final Class enumClass, final Iterable values) { + asEnum(enumClass); + Validate.notNull(values); + final EnumSet condensed = EnumSet.noneOf(enumClass); + for (final E constant : values) { + Validate.isTrue(constant != null, NULL_ELEMENTS_NOT_PERMITTED); + condensed.add(constant); + } + final long[] result = new long[(enumClass.getEnumConstants().length - 1) / Long.SIZE + 1]; + for (final E value : condensed) { + result[value.ordinal() / Long.SIZE] |= 1L << (value.ordinal() % Long.SIZE); + } + ArrayUtils.reverse(result); + return result; } /** @@ -188,117 +265,65 @@ public class EnumUtils { } /** - *

Creates a long bit vector representation of the given subset of an Enum.

+ *

Gets the {@code List} of enums.

* - *

This generates a value that is usable by {@link EnumUtils#processBitVector}.

+ *

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

* - *

Do not use this method if you have more than 64 values in your Enum, as this - * would create a value greater than a long can hold.

- * - * @param enumClass the class of the enum we are working with, not {@code null} - * @param values the values we want to convert, not {@code null}, neither containing {@code null} - * @param the type of the enumeration - * @return a long whose value provides a binary representation of the given set of enum values. - * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null} - * @throws IllegalArgumentException if {@code enumClass} is not an enum class or has more than 64 values, - * or if any {@code values} {@code null} - * @since 3.0.1 - * @see #generateBitVectors(Class, Iterable) + * @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 > long generateBitVector(final Class enumClass, final Iterable values) { - checkBitVectorable(enumClass); - Validate.notNull(values); - long total = 0; - for (final E constant : values) { - Validate.isTrue(constant != null, NULL_ELEMENTS_NOT_PERMITTED); - total |= 1L << constant.ordinal(); - } - return total; + public static > List getEnumList(final Class enumClass) { + return new ArrayList<>(Arrays.asList(enumClass.getEnumConstants())); } /** - *

Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed.

+ *

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

* - *

This generates a value that is usable by {@link EnumUtils#processBitVectors}.

+ *

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

* - *

Use this method if you have more than 64 values in your Enum.

- * - * @param enumClass the class of the enum we are working with, not {@code null} - * @param values the values we want to convert, not {@code null}, neither containing {@code null} - * @param the type of the enumeration - * @return a long[] whose values provide a binary representation of the given set of enum values - * with least significant digits rightmost. - * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null} - * @throws IllegalArgumentException if {@code enumClass} is not an enum class, or if any {@code values} {@code null} - * @since 3.2 + * @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 > long[] generateBitVectors(final Class enumClass, final Iterable values) { - asEnum(enumClass); - Validate.notNull(values); - final EnumSet condensed = EnumSet.noneOf(enumClass); - for (final E constant : values) { - Validate.isTrue(constant != null, NULL_ELEMENTS_NOT_PERMITTED); - condensed.add(constant); + public static > Map getEnumMap(final Class enumClass) { + final Map map = new LinkedHashMap<>(); + for (final E e: enumClass.getEnumConstants()) { + map.put(e.name(), e); } - final long[] result = new long[(enumClass.getEnumConstants().length - 1) / Long.SIZE + 1]; - for (final E value : condensed) { - result[value.ordinal() / Long.SIZE] |= 1L << (value.ordinal() % Long.SIZE); - } - ArrayUtils.reverse(result); - return result; + return map; } /** - *

Creates a long bit vector representation of the given array of Enum values.

+ *

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

* - *

This generates a value that is usable by {@link EnumUtils#processBitVector}.

+ *

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

* - *

Do not use this method if you have more than 64 values in your Enum, as this - * would create a value greater than a long can hold.

- * - * @param enumClass the class of the enum we are working with, not {@code null} - * @param values the values we want to convert, not {@code null} - * @param the type of the enumeration - * @return a long whose value provides a binary representation of the given set of enum values. - * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null} - * @throws IllegalArgumentException if {@code enumClass} is not an enum class or has more than 64 values - * @since 3.0.1 - * @see #generateBitVectors(Class, Iterable) + * @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 */ - @SafeVarargs - public static > long generateBitVector(final Class enumClass, final E... values) { - Validate.noNullElements(values); - return generateBitVector(enumClass, Arrays.asList(values)); + public static > boolean isValidEnum(final Class enumClass, final String enumName) { + return getEnum(enumClass, enumName) != null; } /** - *

Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed.

+ *

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

* - *

This generates a value that is usable by {@link EnumUtils#processBitVectors}.

+ *

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.

* - *

Use this method if you have more than 64 values in your Enum.

- * - * @param enumClass the class of the enum we are working with, not {@code null} - * @param values the values we want to convert, not {@code null}, neither containing {@code null} - * @param the type of the enumeration - * @return a long[] whose values provide a binary representation of the given set of enum values - * with least significant digits rightmost. - * @throws NullPointerException if {@code enumClass} or {@code values} is {@code null} - * @throws IllegalArgumentException if {@code enumClass} is not an enum class, or if any {@code values} {@code null} - * @since 3.2 + * @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 + * @since 3.8 */ - @SafeVarargs - public static > long[] generateBitVectors(final Class enumClass, final E... values) { - asEnum(enumClass); - Validate.noNullElements(values); - final EnumSet condensed = EnumSet.noneOf(enumClass); - Collections.addAll(condensed, values); - final long[] result = new long[(enumClass.getEnumConstants().length - 1) / Long.SIZE + 1]; - for (final E value : condensed) { - result[value.ordinal() / Long.SIZE] |= 1L << (value.ordinal() % Long.SIZE); - } - ArrayUtils.reverse(result); - return result; + public static > boolean isValidEnumIgnoreCase(final Class enumClass, final String enumName) { + return getEnumIgnoreCase(enumClass, enumName) != null; } /** @@ -346,34 +371,9 @@ public class EnumUtils { } /** - * Validate that {@code enumClass} is compatible with representation in a {@code long}. - * @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 or has more than 64 values - * @since 3.0.1 + * This constructor is public to permit tools that require a JavaBean + * instance to operate. */ - private static > Class checkBitVectorable(final Class enumClass) { - final E[] constants = asEnum(enumClass).getEnumConstants(); - Validate.isTrue(constants.length <= Long.SIZE, CANNOT_STORE_S_S_VALUES_IN_S_BITS, - Integer.valueOf(constants.length), enumClass.getSimpleName(), Integer.valueOf(Long.SIZE)); - - return enumClass; - } - - /** - * 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; + public EnumUtils() { } }