Sort methods.
This commit is contained in:
parent
be1536fe56
commit
619a7024c4
|
@ -39,72 +39,149 @@ public class EnumUtils {
|
||||||
private static final String ENUM_CLASS_MUST_BE_DEFINED = "EnumClass must be defined.";
|
private static final String ENUM_CLASS_MUST_BE_DEFINED = "EnumClass must be defined.";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This constructor is public to permit tools that require a JavaBean
|
* Validate {@code enumClass}.
|
||||||
* instance to operate.
|
* @param <E> 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 <E extends Enum<E>> Class<E> asEnum(final Class<E> 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Gets the {@code Map} of enums by name.</p>
|
* Validate that {@code enumClass} is compatible with representation in a {@code long}.
|
||||||
*
|
|
||||||
* <p>This method is useful when you need a map of enums by name.</p>
|
|
||||||
*
|
|
||||||
* @param <E> the type of the enumeration
|
* @param <E> the type of the enumeration
|
||||||
* @param enumClass the class of the enum to query, not null
|
* @param enumClass to check
|
||||||
* @return the modifiable map of enum names to enums, never null
|
* @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 <E extends Enum<E>> Map<String, E> getEnumMap(final Class<E> enumClass) {
|
private static <E extends Enum<E>> Class<E> checkBitVectorable(final Class<E> enumClass) {
|
||||||
final Map<String, E> map = new LinkedHashMap<>();
|
final E[] constants = asEnum(enumClass).getEnumConstants();
|
||||||
for (final E e: enumClass.getEnumConstants()) {
|
Validate.isTrue(constants.length <= Long.SIZE, CANNOT_STORE_S_S_VALUES_IN_S_BITS,
|
||||||
map.put(e.name(), e);
|
Integer.valueOf(constants.length), enumClass.getSimpleName(), Integer.valueOf(Long.SIZE));
|
||||||
|
|
||||||
|
return enumClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Creates a long bit vector representation of the given array of Enum values.</p>
|
||||||
|
*
|
||||||
|
* <p>This generates a value that is usable by {@link EnumUtils#processBitVector}.</p>
|
||||||
|
*
|
||||||
|
* <p>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.</p>
|
||||||
|
*
|
||||||
|
* @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 <E> 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 <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final E... values) {
|
||||||
|
Validate.noNullElements(values);
|
||||||
|
return generateBitVector(enumClass, Arrays.asList(values));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Creates a long bit vector representation of the given subset of an Enum.</p>
|
||||||
|
*
|
||||||
|
* <p>This generates a value that is usable by {@link EnumUtils#processBitVector}.</p>
|
||||||
|
*
|
||||||
|
* <p>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.</p>
|
||||||
|
*
|
||||||
|
* @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 <E> 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 <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final Iterable<? extends E> 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Gets the {@code List} of enums.</p>
|
* <p>Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed.</p>
|
||||||
*
|
*
|
||||||
* <p>This method is useful when you need a list of enums rather than an array.</p>
|
* <p>This generates a value that is usable by {@link EnumUtils#processBitVectors}.</p>
|
||||||
*
|
*
|
||||||
* @param <E> the type of the enumeration
|
* <p>Use this method if you have more than 64 values in your Enum.</p>
|
||||||
* @param enumClass the class of the enum to query, not null
|
*
|
||||||
* @return the modifiable list of enums, never null
|
* @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 <E> 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 <E extends Enum<E>> List<E> getEnumList(final Class<E> enumClass) {
|
@SafeVarargs
|
||||||
return new ArrayList<>(Arrays.asList(enumClass.getEnumConstants()));
|
public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final E... values) {
|
||||||
|
asEnum(enumClass);
|
||||||
|
Validate.noNullElements(values);
|
||||||
|
final EnumSet<E> 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Checks if the specified name is a valid enum for the class.</p>
|
* <p>Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed.</p>
|
||||||
*
|
*
|
||||||
* <p>This method differs from {@link Enum#valueOf} in that checks if the name is
|
* <p>This generates a value that is usable by {@link EnumUtils#processBitVectors}.</p>
|
||||||
* a valid enum without needing to catch the exception.</p>
|
|
||||||
*
|
*
|
||||||
* @param <E> the type of the enumeration
|
* <p>Use this method if you have more than 64 values in your Enum.</p>
|
||||||
* @param enumClass the class of the enum to query, not null
|
*
|
||||||
* @param enumName the enum name, null returns false
|
* @param enumClass the class of the enum we are working with, not {@code null}
|
||||||
* @return true if the enum name is valid, otherwise false
|
* @param values the values we want to convert, not {@code null}, neither containing {@code null}
|
||||||
|
* @param <E> 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 <E extends Enum<E>> boolean isValidEnum(final Class<E> enumClass, final String enumName) {
|
public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final Iterable<? extends E> values) {
|
||||||
return getEnum(enumClass, enumName) != null;
|
asEnum(enumClass);
|
||||||
}
|
Validate.notNull(values);
|
||||||
|
final EnumSet<E> condensed = EnumSet.noneOf(enumClass);
|
||||||
/**
|
for (final E constant : values) {
|
||||||
* <p>Checks if the specified name is a valid enum for the class.</p>
|
Validate.isTrue(constant != null, NULL_ELEMENTS_NOT_PERMITTED);
|
||||||
*
|
condensed.add(constant);
|
||||||
* <p>This method differs from {@link Enum#valueOf} in that checks if the name is
|
}
|
||||||
* a valid enum without needing to catch the exception
|
final long[] result = new long[(enumClass.getEnumConstants().length - 1) / Long.SIZE + 1];
|
||||||
* and performs case insensitive matching of the name.</p>
|
for (final E value : condensed) {
|
||||||
*
|
result[value.ordinal() / Long.SIZE] |= 1L << (value.ordinal() % Long.SIZE);
|
||||||
* @param <E> the type of the enumeration
|
}
|
||||||
* @param enumClass the class of the enum to query, not null
|
ArrayUtils.reverse(result);
|
||||||
* @param enumName the enum name, null returns false
|
return result;
|
||||||
* @return true if the enum name is valid, otherwise false
|
|
||||||
* @since 3.8
|
|
||||||
*/
|
|
||||||
public static <E extends Enum<E>> boolean isValidEnumIgnoreCase(final Class<E> enumClass, final String enumName) {
|
|
||||||
return getEnumIgnoreCase(enumClass, enumName) != null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -188,117 +265,65 @@ public class EnumUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Creates a long bit vector representation of the given subset of an Enum.</p>
|
* <p>Gets the {@code List} of enums.</p>
|
||||||
*
|
*
|
||||||
* <p>This generates a value that is usable by {@link EnumUtils#processBitVector}.</p>
|
* <p>This method is useful when you need a list of enums rather than an array.</p>
|
||||||
*
|
*
|
||||||
* <p>Do not use this method if you have more than 64 values in your Enum, as this
|
* @param <E> the type of the enumeration
|
||||||
* would create a value greater than a long can hold.</p>
|
* @param enumClass the class of the enum to query, not null
|
||||||
*
|
* @return the modifiable list of enums, never null
|
||||||
* @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 <E> 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 <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final Iterable<? extends E> values) {
|
public static <E extends Enum<E>> List<E> getEnumList(final Class<E> enumClass) {
|
||||||
checkBitVectorable(enumClass);
|
return new ArrayList<>(Arrays.asList(enumClass.getEnumConstants()));
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed.</p>
|
* <p>Gets the {@code Map} of enums by name.</p>
|
||||||
*
|
*
|
||||||
* <p>This generates a value that is usable by {@link EnumUtils#processBitVectors}.</p>
|
* <p>This method is useful when you need a map of enums by name.</p>
|
||||||
*
|
*
|
||||||
* <p>Use this method if you have more than 64 values in your Enum.</p>
|
* @param <E> the type of the enumeration
|
||||||
*
|
* @param enumClass the class of the enum to query, not null
|
||||||
* @param enumClass the class of the enum we are working with, not {@code null}
|
* @return the modifiable map of enum names to enums, never null
|
||||||
* @param values the values we want to convert, not {@code null}, neither containing {@code null}
|
|
||||||
* @param <E> 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 <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final Iterable<? extends E> values) {
|
public static <E extends Enum<E>> Map<String, E> getEnumMap(final Class<E> enumClass) {
|
||||||
asEnum(enumClass);
|
final Map<String, E> map = new LinkedHashMap<>();
|
||||||
Validate.notNull(values);
|
for (final E e: enumClass.getEnumConstants()) {
|
||||||
final EnumSet<E> condensed = EnumSet.noneOf(enumClass);
|
map.put(e.name(), e);
|
||||||
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];
|
return map;
|
||||||
for (final E value : condensed) {
|
|
||||||
result[value.ordinal() / Long.SIZE] |= 1L << (value.ordinal() % Long.SIZE);
|
|
||||||
}
|
|
||||||
ArrayUtils.reverse(result);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Creates a long bit vector representation of the given array of Enum values.</p>
|
* <p>Checks if the specified name is a valid enum for the class.</p>
|
||||||
*
|
*
|
||||||
* <p>This generates a value that is usable by {@link EnumUtils#processBitVector}.</p>
|
* <p>This method differs from {@link Enum#valueOf} in that checks if the name is
|
||||||
|
* a valid enum without needing to catch the exception.</p>
|
||||||
*
|
*
|
||||||
* <p>Do not use this method if you have more than 64 values in your Enum, as this
|
* @param <E> the type of the enumeration
|
||||||
* would create a value greater than a long can hold.</p>
|
* @param enumClass the class of the enum to query, not null
|
||||||
*
|
* @param enumName the enum name, null returns false
|
||||||
* @param enumClass the class of the enum we are working with, not {@code null}
|
* @return true if the enum name is valid, otherwise false
|
||||||
* @param values the values we want to convert, not {@code null}
|
|
||||||
* @param <E> 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 <E extends Enum<E>> boolean isValidEnum(final Class<E> enumClass, final String enumName) {
|
||||||
public static <E extends Enum<E>> long generateBitVector(final Class<E> enumClass, final E... values) {
|
return getEnum(enumClass, enumName) != null;
|
||||||
Validate.noNullElements(values);
|
|
||||||
return generateBitVector(enumClass, Arrays.asList(values));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed.</p>
|
* <p>Checks if the specified name is a valid enum for the class.</p>
|
||||||
*
|
*
|
||||||
* <p>This generates a value that is usable by {@link EnumUtils#processBitVectors}.</p>
|
* <p>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.</p>
|
||||||
*
|
*
|
||||||
* <p>Use this method if you have more than 64 values in your Enum.</p>
|
* @param <E> the type of the enumeration
|
||||||
*
|
* @param enumClass the class of the enum to query, not null
|
||||||
* @param enumClass the class of the enum we are working with, not {@code null}
|
* @param enumName the enum name, null returns false
|
||||||
* @param values the values we want to convert, not {@code null}, neither containing {@code null}
|
* @return true if the enum name is valid, otherwise false
|
||||||
* @param <E> the type of the enumeration
|
* @since 3.8
|
||||||
* @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
|
|
||||||
*/
|
*/
|
||||||
@SafeVarargs
|
public static <E extends Enum<E>> boolean isValidEnumIgnoreCase(final Class<E> enumClass, final String enumName) {
|
||||||
public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumClass, final E... values) {
|
return getEnumIgnoreCase(enumClass, enumName) != null;
|
||||||
asEnum(enumClass);
|
|
||||||
Validate.noNullElements(values);
|
|
||||||
final EnumSet<E> 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -346,34 +371,9 @@ public class EnumUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate that {@code enumClass} is compatible with representation in a {@code long}.
|
* This constructor is public to permit tools that require a JavaBean
|
||||||
* @param <E> the type of the enumeration
|
* instance to operate.
|
||||||
* @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
|
|
||||||
*/
|
*/
|
||||||
private static <E extends Enum<E>> Class<E> checkBitVectorable(final Class<E> enumClass) {
|
public EnumUtils() {
|
||||||
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 <E> 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 <E extends Enum<E>> Class<E> asEnum(final Class<E> 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue